gpt4 book ai didi

java - 制作并实现一个使用 JPanel 类制作交通灯的程序。

转载 作者:行者123 更新时间:2023-12-01 08:53:29 25 4
gpt4 key购买 nike

问题是设计和实现一个绘制交通灯并使用按钮改变信号灯的应用程序。从 JPanel 类派生绘图表面,并使用另一个面板来组织绘图表面和按钮。

我尝试处理它并想出了一个类来运行它,但我不太确定如何运行它的主要方法。

这是代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.applet.*;

公共(public)类 TrafficLight 扩展 JPanel 实现 ActionListener {

private Button lightRed, lightYellow, lightGreen;
private JPanel panel1;
private boolean lightRedClicked = false, lightYellowClicked = false, lightGreenClicked = false;

public void init() {

setLayout(new BorderLayout());
panel1 = new JPanel();

lightGreen = new Button("Go");
lightYellow = new Button("Wait");
lightRed = new Button("Stop");
panel1.add(lightRed);
panel1.add(lightYellow);
panel1.add(lightGreen);

add(panel1, BorderLayout.NORTH);
}
public void paint(Graphics g){
setBackground(Color.cyan);
g.setColor(Color.black);
g.fillRect(260,50,80,160);
g.setColor(Color.black);
g.fillRect(140,50,80,160);

if(lightRedClicked == true){
g.setColor(Color.red);
g.fillOval(280,60,40,40);
g.setColor(Color.black);
g.fillOval(280,110,40,40);
g.fillOval(280,160,40,40);
lightRedClicked = false;
}
if(lightYellowClicked == true) {
g.setColor(Color.yellow);
g.fillOval(280,110,40,40);
g.setColor(Color.black);
g.fillOval(280,60,40,40);
g.fillOval(280,160,40,40);
lightYellowClicked = false;
}

if(lightGreenClicked == true) {
g.setColor(Color.green);
g.fillOval(280,160,40,40);
g.setColor(Color.black);
g.fillOval(280,110,40,40);
g.fillOval(280,60,40,40);
lightGreenClicked = false;
}
}
public void actionPerformed(ActionEvent swicth) {
if(swicth.getSource() == lightGreen){
lightGreenClicked = true;
repaint();
}
if(swicth.getSource() == lightYellow){
lightYellowClicked = true;
repaint();
}
if(swicth.getSource() == lightRed){
lightRedClicked = true;
repaint();
}
}

}

最佳答案

试试这个

        public class JavaApplication5 {

public static void main(String[] args) {
JFrame f = new JFrame("Traffic Light");
f.add(new TrafficLight());
f.setSize(500, 500);
f.setVisible(true);
}
}

class TrafficLight extends JPanel implements ActionListener {

private Button lightRed, lightYellow, lightGreen;
private JPanel panel1;
private boolean lightRedClicked = false, lightYellowClicked = false, lightGreenClicked = false;
public TrafficLight(){
setLayout(new BorderLayout());
panel1 = new JPanel();

lightGreen = new Button("Go");
lightYellow = new Button("Wait");
lightRed = new Button("Stop");
panel1.add(lightRed);
panel1.add(lightYellow);
panel1.add(lightGreen);

add(panel1, BorderLayout.NORTH);
lightRed.addActionListener(this);
lightGreen.addActionListener(this);
lightYellow.addActionListener(this);
}
public void init() {


}
public void paint(Graphics g){
setBackground(Color.cyan);
g.setColor(Color.black);
g.fillRect(260,50,80,160);
g.setColor(new Color(255, 150, 150)); //light red
g.fillOval(280,60,40,40);
g.setColor(new Color(255, 255, 200)); //light yellow
g.fillOval(280,110,40,40);
g.setColor(new Color(150, 200, 150)); //light green
g.fillOval(280,160,40,40);

if(lightRedClicked == true){
g.setColor(Color.red);
g.fillOval(280,60,40,40);
lightRedClicked = false;
}
if(lightYellowClicked == true) {
g.setColor(Color.yellow);
g.fillOval(280,110,40,40);
lightYellowClicked = false;
}

if(lightGreenClicked == true) {
g.setColor(Color.green);
g.fillOval(280,160,40,40);
lightGreenClicked = false;
}
}
public void actionPerformed(ActionEvent swicth) {
if(swicth.getSource() == lightGreen){
lightGreenClicked = true;
repaint();
}
if(swicth.getSource() == lightYellow){
lightYellowClicked = true;
repaint();
}
if(swicth.getSource() == lightRed){
lightRedClicked = true;
repaint();
}
}


}

关于java - 制作并实现一个使用 JPanel 类制作交通灯的程序。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42213766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com