问题
I am trying to make a program with buttons, that when you click them, change the background color of the frame
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ColorFrame {
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton redButton = new JButton ("Red");
final JButton greenButton = new JButton ("Green");
final JButton blueButton = new JButton ("Blue");
class Listener extends JPanel implements ActionListener{
public void actionPerformed(ActionEvent event) {
Color color;
if (event.getSource() == redButton){
color = Color.red;
} else if (event.getSource() == greenButton){
color = Color.green;
} else {
color = Color.blue;
}
setBackground(color);
System.out.println(color);
repaint();
}
}
redButton.addActionListener(new Listener());
greenButton.addActionListener(new Listener());
blueButton.addActionListener(new Listener());
panel.add(new JButton ("Red"));
panel.add(new JButton ("Green"));
panel.add(new JButton ("Blue"));
frame.add(panel);
}
}
Yet when I click the buttons, nothing seems to happen and I think it might have something to do with the listeners not being activated for reason
回答1:
You define the buttons here:
final JButton redButton = new JButton ("Red");
final JButton greenButton = new JButton ("Green");
final JButton blueButton = new JButton ("Blue");
But then you add entirely new buttons to the actual panel so the buttons with the listeners attached are never added:
panel.add(new JButton ("Red"));
panel.add(new JButton ("Green"));
panel.add(new JButton ("Blue"));
You should add the buttons like this:
panel.add(redButton);
panel.add(greenButton);
panel.add(blueButton);
回答2:
Take a moment to visualise you setup...
You have a JFrame. This window has a JRootPane, which contains a JLayerdPane, which contains a the "content pane".
The content pane is generally the most top level component of a basic window.
On to this, you add a JPanel. JPanel is opaque by default. By default, the content pane uses a BorderLayout, this means that anything added to the default position will be placed in the CENTER position, filling the available space...
This means, you frame is covered by a JLayeredPane, content pane AND your JPanel. setBackground does not delegate to the content pane like some of the other methods, but, in your case, it wouldn't help, as the JPanel you add is now covering it...
In addition to LadyRacheya suggestions, you have two choices.
You can make the JPanel transparent...
JPanel panel = new JPanel();
panel.setOpaque(false);
And change the background color of the content pane...
getContentPane().setBackground(color);
OR you can simply change the background color of the JPanel....
final JPanel panel = new JPanel();
//...
panel.setBackground(color);
回答3:
Try this:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ColorFrame {
public static void main(String[] args){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton redButton = new JButton ("Red");
final JButton greenButton = new JButton ("Green");
final JButton blueButton = new JButton ("Blue");
class Listener extends JPanel implements ActionListener{
public void actionPerformed(ActionEvent event) {
Color color;
if (event.getSource() == redButton){
color = Color.red;
redButton.setBackground(color);
panel.setBackground(color);//To set panel background instead of frames background
} else if (event.getSource() == greenButton){
color = Color.green;
greenButton.setBackground(color);
panel.setBackground(color);
} else {
color = Color.blue;
blueButton.setBackground(color);
panel.setBackground(color);
}
setBackground(color);
System.out.println(color);
repaint();
}
}
redButton.addActionListener(new Listener());
greenButton.addActionListener(new Listener());
blueButton.addActionListener(new Listener());
panel.add(redButton);
panel.add(greenButton);
panel.add(blueButton);
frame.add(panel);
frame.setVisible(true);
}
}
回答4:
public class ColorFrame {
public JPanel panel;
public static void main(String[] args){
JFrame frame = new JFrame();
final JPanel panel = new JPanel();
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton redButton = new JButton ("Red");
final JButton greenButton = new JButton ("Green");
final JButton blueButton = new JButton ("Blue");
class Listener extends JPanel implements ActionListener{
public void actionPerformed(ActionEvent event) {
Color color;
if (event.getSource() == redButton){
redButton.setBackground(Color.RED);
panel.setBackground(Color.RED);
} else if (event.getSource() == greenButton){
greenButton.setBackground(Color.GREEN);
panel.setBackground(Color.GREEN);
} else {
blueButton.setBackground(Color.BLUE);
panel.setBackground(Color.BLUE);
}
setBackground(Color.WHITE);
System.out.println(Color.WHITE);
repaint();
}
}
redButton.addActionListener(new Listener());
greenButton.addActionListener(new Listener());
blueButton.addActionListener(new Listener());
panel.add(redButton);
panel.add(greenButton);
panel.add(blueButton);
frame.add(panel);
frame.setVisible(true);
}
来源:https://stackoverflow.com/questions/20959053/jframe-buttons-that-change-background-color-of-window