📄 trafficframe.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class trafficFrame extends SimpleFrame{
public trafficFrame(int width,int height) {
super(width,height);
setTitle("红绿灯");
ColorPanel panel=new ColorPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static void main(String[] args){
trafficFrame frame=new trafficFrame(400,300);
frame.setVisible(true);
}
}
class ColorPanel extends JPanel{
public ColorPanel(){
redButton= new JButton("红灯");
greenButton=new JButton("绿灯");
yellowButton=new JButton("黄灯");
add(redButton);
add(greenButton);
add(yellowButton);
redButton.addActionListener(new ColorActionListener());
yellowButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
setyellow();
}
});
greenButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
setgreen();
}
});
}
private void setyellow(){
backgroundColor=backgroundColor.brighter();
backgroundColor=new Color(255,255,0);
setBackground(backgroundColor);
}
private void setgreen(){
backgroundColor=backgroundColor.darker();
backgroundColor=new Color(0,255,0);
setBackground(backgroundColor);
}
private class ColorActionListener implements ActionListener{
public void actionPerformed(ActionEvent event){
setColor();
}
}
private void setColor(){
backgroundColor=new Color(255,0,0);
setBackground(backgroundColor);
}
private Color backgroundColor;
private JButton redButton;
private JButton greenButton;
private JButton yellowButton;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -