Related
whenever I uncomment my JTextField or JComboBox they make all my swing elements disappear, I've tried resizing them and manipulating them in any way but I have no clue as to why the stuff is disappearing. They weren't on a previous program I wrote and they're wrote the exact same way. They elements even disappear if the two objects aren't even added to the panel.
Here's the code I have for my program. I don't really want to separate the 2 panels into their own Classes.
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
// Create window
JFrame window = new Window();
String[] optionsToChoose = {"Car Parts", "Fast Food", "Groceries", "Clothing", "Gas", "Store", "Entertainment"};
// Create first screen
JPanel startPanel = new JPanel();
startPanel.setLayout(null);
startPanel.setBounds(230, 230, 500, 250);
startPanel.setBackground(new Color(177, 177, 177));
JLabel header = new JLabel("CHOOSE AN ACTION");
header.setForeground(Color.white);
header.setFont(new Font("Verdana", Font.BOLD, 20));
header.setHorizontalAlignment(JLabel.CENTER);
header.setBounds(0, -25, 500, 150);
JButton add = new JButton("ADD");
add.setBackground(new Color(0, 109, 91));
add.setForeground(Color.white);
add.setFont(new Font("Verdana", Font.BOLD, 15));
add.setBounds(90, 125, 150, 50);
JButton view = new JButton("VIEW REPORT");
view.setBackground(new Color(167, 199, 231));
view.setForeground(Color.white);
view.setFont(new Font("Verdana", Font.BOLD, 15));
view.setBounds(270, 125, 150, 50);
startPanel.add(view);
startPanel.add(add);
startPanel.add(header);
startPanel.setVisible(false);
// Create second screen
JPanel addPanel = new JPanel();
addPanel.setBounds(230, 220, 500, 300);
addPanel.setLayout(null);
addPanel.setBackground(Color.black);
JLabel header2 = new JLabel("INPUT DATA BELOW:");
header2.setBounds(0, -25, 500, 150);
header2.setFont(new Font("Verdana", Font.BOLD, 20));
header2.setHorizontalAlignment(JLabel.CENTER);
header2.setForeground(Color.white);
JLabel costHeader = new JLabel("COST:");
costHeader.setForeground(Color.white);
costHeader.setFont(new Font("Verdana", Font.BOLD, 20));
costHeader.setBounds(90, 75, 150, 50);
JLabel typeHeader = new JLabel("TYPE:");
typeHeader.setBounds(300, 75, 150, 50);
typeHeader.setForeground(Color.WHITE);
typeHeader.setFont(new Font("Verdana", Font.BOLD, 20));
JTextField costInput = new JTextField("$0", 10);
costInput.setFont(new Font("Verdana", Font.PLAIN, 20));
costInput.setBounds(90, 150, 150, 50);
costInput.setForeground(Color.white);
JComboBox<String> jComboBox = new JComboBox<>(optionsToChoose);
jComboBox.setFont(new Font("Verdana", Font.PLAIN, 20));
jComboBox.setBounds(0, 0, 150, 50);
addPanel.add(jComboBox);
addPanel.add(costInput);
addPanel.add(typeHeader);
addPanel.add(header2);
addPanel.add(costHeader);
addPanel.setVisible(true);
// Add components
window.add(startPanel);
window.add(addPanel);
}
}
Here's the Window class as well:
import java.awt.Color;
import javax.swing.JFrame;
public class Window extends JFrame {
public Window() {
this.setTitle("Finance Tracker");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLayout(null);
//this.setLocationRelativeTo(null);
this.setSize(1000, 800);
this.setVisible(true);
this.getContentPane().setBackground(new Color(177, 177, 177));
}
}
If anyone has any idea as to why this could be happening, any help is appreciated. Thanks!
The drawing is basically a two-leaf window with sky and sun as if you're looking out that window.
I need to add buttons Day and Night. By clicking each button the color of sky changes to dark blue and light blue, the color of the sun changes to color of the moon correspondingly. I have added the buttons and they appear but I can't seem to wrap my head around the way where I should place my actionListener and how to get access to my g2 variable in order to change those colors.
public class DrawingOne extends JFrame{
public DrawingOne() {
Container container = getContentPane();
container.setBackground(new Color(224, 196, 214));
container.setLayout(new BorderLayout(20, 20));
JButton j1 = new JButton("Day");
JButton j2 = new JButton("Night");
container.add(j1, BorderLayout.SOUTH);
container.add(j2, BorderLayout.NORTH);
setSize(DEFAULT_WITDH, DEFAULT_HEIGHT);
DrawComponent c = new DrawComponent();
add(c);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static final int DEFAULT_WITDH = 700;
public static final int DEFAULT_HEIGHT = 500;
}
class DrawComponent extends JComponent {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//the base for window
Rectangle2D.Double rectangle2Drect2 = new Rectangle2D.Double(37, 26, 458, 280);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
//left half-window
rectangle2Drect2 = new Rectangle2D.Double(51, 40, 160, 250);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(new Color(135, 206, 235));
g2.fill(rectangle2Drect2);
//right half-window
rectangle2Drect2 = new Rectangle2D.Double(230, 40, 250, 250);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(new Color(135, 206, 235));
g2.fill(rectangle2Drect2);
//основа ручки
rectangle2Drect2 = new Rectangle2D.Double(215, 140, 10, 10);
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
//ручка
rectangle2Drect2 = new Rectangle2D.Double(217, 142, 4, 30);
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
//the sun
g2.setColor(Color.getHSBColor(197.4F, 42.6F, 92.2F));
g2.fillOval(100, 60, 80, 80);
//горшок
int[] xpoints = {230 + 60, 300 + 60, 285 + 61, 244 + 60};
int[] ypoints = {240, 240, 290, 290};
int npoints = 4;
g2.setColor(new Color(172, 86, 30));
g2.fillPolygon(xpoints, ypoints, npoints);
//ствол цветка
g2.setStroke(new BasicStroke(5));
g2.setColor(new Color(0, 128, 0));
g2.drawLine(325, 200, 325, 238);
//лепестки
g2.setColor(new Color(255, 192, 203));
g2.fillOval(317, 200, 22, 22);
g2.fillOval(317, 175, 22, 22);
g2.fillOval(317 - 15, 190, 22, 22);
g2.fillOval(317 + 13, 190, 22, 22);
// g2.fillOval(317,190,22,22);
//центр цветка
g2.setColor(new Color(255, 165, 0));
g2.fillOval(317, 190, 15, 15);
}
public static void main(String[] args) {
DrawingOne frame = new DrawingOne();
}
}
I made a night sky from your GUI.
I had to make quite a few changes to your code to get this to work.
I had to make the JComponent class an inner class, so it could reference DrawingOne class variables.
I had to move the main method in the DrawingOne class, so I could start the application.
I started the Java application with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.
I rearranged the order of your statements in the DrawingOne constructor, so they made more sense. I made the drawing panel your preferred size, rather than the JFrame. The size of the drawing panel is what's important, not the JFrame size.
I made the sky and sun colors DrawingOne class variables, so they could be changed. I wrote a couple of methods to change the colors.
I wrote an ActionListener class that changes the colors depending on which button you left-click.
Here's the complete runnable code.
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class DrawingOne extends JFrame {
private static final long serialVersionUID = 1L;
public static final int DEFAULT_WIDTH = 700;
public static final int DEFAULT_HEIGHT = 500;
public Color sunColor;
public Color skyColor;
public DrawComponent c;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new DrawingOne();
}
});
}
public DrawingOne() {
setDaySky();
Container container = getContentPane();
container.setBackground(new Color(224, 196, 214));
container.setLayout(new BorderLayout(20, 20));
container.setPreferredSize(
new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
ButtonListener listener = new ButtonListener();
JButton j1 = new JButton("Day");
j1.addActionListener(listener);
JButton j2 = new JButton("Night");
j2.addActionListener(listener);
container.add(j1, BorderLayout.SOUTH);
container.add(j2, BorderLayout.NORTH);
c = new DrawComponent();
container.add(c, BorderLayout.CENTER);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void setNightSky() {
this.sunColor = Color.getHSBColor(197.4F, 42.6F, 92.2F);
this.skyColor = new Color(7, 78, 107);
}
public void setDaySky() {
this.sunColor = Color.getHSBColor(197.4F, 42.6F, 92.2F);
this.skyColor = new Color(135, 206, 235);
}
public class DrawComponent extends JComponent {
private static final long serialVersionUID = 1L;
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// the base for window
Rectangle2D.Double rectangle2Drect2 =
new Rectangle2D.Double(37, 26, 458, 280);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
// left half-window
rectangle2Drect2 =
new Rectangle2D.Double(51, 40, 160, 250);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(skyColor);
g2.fill(rectangle2Drect2);
// right half-window
rectangle2Drect2 =
new Rectangle2D.Double(230, 40, 250, 250);
g2.setStroke(new BasicStroke(3));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(skyColor);
g2.fill(rectangle2Drect2);
// основа ручки
rectangle2Drect2 =
new Rectangle2D.Double(215, 140, 10, 10);
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
// ручка
rectangle2Drect2 =
new Rectangle2D.Double(217, 142, 4, 30);
g2.setStroke(new BasicStroke(2));
g2.setColor(Color.black);
g2.draw(rectangle2Drect2);
g2.setColor(Color.white);
g2.fill(rectangle2Drect2);
// the sun
g2.setColor(sunColor);
g2.fillOval(100, 60, 80, 80);
// горшок
int[] xpoints = { 230 + 60, 300 + 60, 285 + 61, 244 + 60 };
int[] ypoints = { 240, 240, 290, 290 };
int npoints = 4;
g2.setColor(new Color(172, 86, 30));
g2.fillPolygon(xpoints, ypoints, npoints);
// ствол цветка
g2.setStroke(new BasicStroke(5));
g2.setColor(new Color(0, 128, 0));
g2.drawLine(325, 200, 325, 238);
// лепестки
g2.setColor(new Color(255, 192, 203));
g2.fillOval(317, 200, 22, 22);
g2.fillOval(317, 175, 22, 22);
g2.fillOval(317 - 15, 190, 22, 22);
g2.fillOval(317 + 13, 190, 22, 22);
// g2.fillOval(317,190,22,22);
// центр цветка
g2.setColor(new Color(255, 165, 0));
g2.fillOval(317, 190, 15, 15);
}
}
public class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
if (button.getText().equals("Night")) {
setNightSky();
} else {
setDaySky();
}
c.repaint();
}
}
}
I'm creating a Java program that uses Swing to draw a face, and then I am using MouseListener to respond to mouse clicks to make one of the eyes blink. How would I make one of the eyes blink using MouseListener? The method paint(Graphics g) can only be created once with that name, so if I want to repeat it and edit it under the MouseListener code with one of the eyes turned into a line for blinking, how would I do that?
Here is my code so far:
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Sans extends JPanel {
public void paint(Graphics g) {
super.paintComponent(g);
setSize(650, 650);
g.drawOval(110, 250, 500, 275);
g.setColor(new Color(226, 222, 217));
g.fillOval(110, 250, 500, 275);
g.drawOval(475, 300, 75, 75);
g.setColor(new Color(74, 199, 226));
g.fillOval(475, 300, 75, 75);
g.drawOval(505, 330, 15, 15);
g.setColor(new Color(0, 0, 0));
g.fillOval(505, 330, 15, 15);
g.drawOval(175, 300, 75, 75);
g.setColor(new Color(0, 0, 0));
g.fillOval(175, 300, 75, 75);
g.drawOval(205, 330, 15, 15);
g.setColor(new Color(232, 255, 243));
g.fillOval(205, 330, 15, 15);
g.drawOval(350, 375, 20, 50);
g.setColor(new Color(0, 0, 0));
g.fillOval(350, 375, 20, 50);
g.drawArc(290, 360, 150, 150, 180, 180);
g.setColor(new Color(255, 255, 255));
g.fillArc(290, 360, 150, 150, 180, 180);
}
public static void main(String[] args) {
Font font = new Font("TimesRoman", Font.PLAIN, 15);
JFrame frame = new JFrame();
Sans spook = new Sans();
frame.add(spook);
frame.setSize(750, 750);
frame.setTitle("I'VE GOTTEN A TON OF WORK DONE TODAY. A SKELE-TON.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
public class BlinkHandler implements MouseListener {
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
}
This question already has answers here:
What is setBounds and how do I use it?
(7 answers)
Closed 3 years ago.
I am coding a program wich should contain multiple JButtons set next to each other. At first I wanted a 4 by 6 Grid. I have 3 classes: a Main clas containing the main method and a constructor call of the Gui class constructor. The Gui class should include all JObjects (JButtons) + the JFrame setup. Then there is a third ButtonPlacement class wich includes the setBounds method calls. As I ran the Code in Eclipse ervery Button in my Button Array was placed at the right position except the last one: btn[3][5]. It is as big as the whole JFrame.
class Main:
package pack1;
public class Main {
public static void main(String[] args) {
new Gui();
}
}
class Gui:
package pack1;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import pack1.ActionHandler;
import pack1.ButtonPlacement;
public class Gui {
static JFrame jf;
static JButton btn[][] = new JButton[4][6];
JButton btnReset;
public Gui() {
jf = new JFrame();
jf.setSize(500, 600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setResizable(false);
jf.setLocationRelativeTo(null);
for(int i = 0; i<4;++i) {
for(int j = 0; j<6; ++j) {
btn[i][j] = new JButton("btn" + i + j);
btn[i][j].setVisible(true);
btn[i][j].addActionListener(new ActionHandler());
btn[i][j].setFocusPainted(false);
btn[i][j].setContentAreaFilled(true);
btn[i][j].setBorder(null);
btn[i][j].setFont(new Font("Century Gothic", Font.PLAIN, 20));
jf.add(btn[i][j]);
}
}
ButtonPlacement.place();
jf.setVisible(true);
}
}
class ButtonPlacement:
package pack1;
import pack1.Gui;
public class ButtonPlacement {
public static void place() {
Gui.btn[0][0].setBounds(0, 140, 100, 60);
Gui.btn[1][0].setBounds(100, 140, 100, 60);
Gui.btn[2][0].setBounds(200, 140, 100, 60);
Gui.btn[3][0].setBounds(300, 140, 100, 60);
Gui.btn[0][1].setBounds(0, 200, 100, 60);
Gui.btn[1][1].setBounds(100, 200, 100, 60);
Gui.btn[2][1].setBounds(200, 200, 100, 60);
Gui.btn[3][1].setBounds(300, 200, 100, 60);
Gui.btn[0][2].setBounds(0, 260, 100, 60);
Gui.btn[1][2].setBounds(100, 260, 100, 60);
Gui.btn[2][2].setBounds(200, 260, 100, 60);
Gui.btn[3][2].setBounds(300, 260, 100, 60);
Gui.btn[0][3].setBounds(0, 320, 100, 60);
Gui.btn[1][3].setBounds(100, 320, 100, 60);
Gui.btn[2][3].setBounds(200, 320, 100, 60);
Gui.btn[3][3].setBounds(300, 320, 100, 60);
Gui.btn[0][4].setBounds(0, 380, 100, 60);
Gui.btn[1][4].setBounds(100, 380, 100, 60);
Gui.btn[2][4].setBounds(200, 380, 100, 60);
Gui.btn[3][4].setBounds(300, 380, 100, 60);
Gui.btn[0][5].setBounds(0, 440, 100, 60);
Gui.btn[1][5].setBounds(100, 440, 100, 60);
Gui.btn[2][5].setBounds(200, 440, 100, 60);
Gui.btn[3][5].setBounds(300, 440, 100, 60);
}
}
I expect the Button btn[3][5] at its position 300, 440 with its size 100, 60, but the button is as big as the JFrame.
Adding this to the class Gui let it work properly for me:
jf.setLayout(null);
I wrote program to draw wheel (circle with 6 segments) and each segment with different colour. and animate the wheel..
here is the code:
public class ExamWheel extends JFrame implements ActionListener{
JButton b_start = new JButton("Start");
JButton b_stop = new JButton("Stop");
Thread th;
Boolean doDraw = true;
public ExamWheel(){
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Wheel..");
//add(b_start);
this.setLayout (new FlowLayout());
this.add(b_stop);
b_start.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b_start)
doDraw=true;
}
public void paint(Graphics graphics) {
if (doDraw){
super.paint(graphics);
Graphics2D g = (Graphics2D) graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
try{
// draw the circle
for(int i=0; ; i=i+1){
g.setColor(Color.CYAN);
g.fillArc(50, 50, 300, 300, i+0, 60);
th.sleep(1);
g.setColor(Color.red);
g.fillArc(50, 50, 300, 300, i+60, 60);
th.sleep(1);
g.setColor(Color.green);
g.fillArc(50, 50, 300, 300, i+120, 60);
th.sleep(1);
g.setColor(Color.blue);
g.fillArc(50, 50, 300, 300, i+180, 60);
th.sleep(1);
g.setColor(Color.gray);
g.fillArc(50, 50, 300, 300, i+240, 60);
th.sleep(1);
g.setColor(Color.pink);
g.fillArc(50, 50, 300, 300, i+300, 60);
th.sleep(1);
}
}
catch(InterruptedException e){
Thread.currentThread().interrupt();
}
}
}
public static void main(String[] args) {
ExamWheel f = new ExamWheel();
}
}
Problems: it is infinite loop, and I can't stop it or close the Frame.
so I had idea:
I created Boolean variable (doDraw) with value true, and add JButton, when click the Button the variable will change to false, and in paint() method I'll use if condition at first of paint()
problem: I can't add JButton to the Frame with paint(), so what can I do?
Note: I tried to use paintComponent() but the loop (for with thread) doesn't work.
IT IS SOLVED
Thanks for Pete Kirham
I added Timer and replaced paint() by paintComponent()
public class ExamWheel extends JPanel implements ActionListener {
int i=0;
Timer tm = new Timer(10, this);
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D g = (Graphics2D) graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.CYAN);
g.fillArc(50, 50, 300, 300, i+0, 60);
g.setColor(Color.red);
g.fillArc(50, 50, 300, 300, i+60, 60);
g.setColor(Color.green);
g.fillArc(50, 50, 300, 300, i+120, 60);
g.setColor(Color.blue);
g.fillArc(50, 50, 300, 300, i+180, 60);
g.setColor(Color.gray);
g.fillArc(50, 50, 300, 300, i+240, 60);
g.setColor(Color.pink);
g.fillArc(50, 50, 300, 300, i+300, 60);
tm.start();
}
public void actionPerformed(ActionEvent e) {
i++;
repaint();
}
public static void main(String[] args) {
ExamWheel wh = new ExamWheel();
JFrame jf = new JFrame();
jf.setSize(500,500);
jf.setResizable(false);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setTitle("Wheel..");
jf.add(wh);
}
You need to return from paintComponent to allow the thread which runs the gui to do other stuff, like respond to button events or actually put the contents of the graphics onto the screen.
Use a timer to invalidate the component - see http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html and update the the animation based on the current time, for example current milliseconds modulo 5000 will give a cycle which repeats every five seconds.