import javafx.scene.effect.ColorAdjust;
import oracle.jrockit.jfr.JFR;
import javax.swing.*;
import javax.xml.bind.JAXBContext;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class test {
public static void main(String[] args) {
EventQueue.invokeLater(()->{
JFrame frame=new ButtonFrame();
frame.setTitle("hello,world");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class ButtonFrame extends JFrame{
private JPanel buttonPanel;
private static final int DEFAULT_WIDTH=300;
private static final int DEFAULT_HEIGHT=200;
public ButtonFrame(){
JButton yellowButton=new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
buttonPanel = new JPanel();
buttonPanel.add(yellowButton);
buttonPanel.add(blueButton);
buttonPanel.add(redButton);
add(buttonPanel);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.red);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
class ColorAction implements ActionListener{
private Color backgroundColor;
public ColorAction(Color c){
backgroundColor=c;
}
public void actionPerformed(ActionEvent e) {
buttonPanel.setBackground(backgroundColor);
}
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史