提问人:Jan_05 提问时间:2/5/2023 更新时间:2/5/2023 访问量:59
以红绿灯为例的静态方法和延迟 (java/jframe)
static methods and delay using the example of a traffic light (java/jframe)
问:
这应该是红绿灯的一个例子。 我的问题是我不知道如何使用延迟。(我试过了,搜索过,但没有任何帮助) 而且我不能使用“这个......”在“绿色,黄色,红色,黄色”方法中,因为它们是静态的。 该按钮应在“自动交通信号灯打开”和“关闭”之间切换。 请有人帮我解决这个问题。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;
public class AmpelAuto extends JFrame {
// Anfang Attribute
private JPanel jPanelOben = new JPanel(null, true);
private JPanel jPanelMitte = new JPanel(null, true);
private JPanel jPanelUnten = new JPanel(null, true);
private JButton Toggle = new JButton();
public boolean OnOff = true;
// Ende Attribute
//public int OnOff = 1;
public AmpelAuto() {
// Frame-Initialisierung
super();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 412;
int frameHeight = 543;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setTitle("AmpelAuto");
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
jPanelMitte.setBounds(144, 160, 120, 120);
jPanelMitte.setOpaque(true);
jPanelMitte.setBorder(BorderFactory.createBevelBorder(0, Color.BLACK, Color.BLACK));
jPanelMitte.setBackground(Color.WHITE);
cp.add(jPanelMitte);
jPanelUnten.setBounds(144, 296, 120, 120);
jPanelUnten.setOpaque(true);
jPanelUnten.setBorder(BorderFactory.createBevelBorder(0, Color.BLACK, Color.BLACK));
jPanelUnten.setBackground(Color.WHITE);
cp.add(jPanelUnten);
jPanelOben.setBounds(144, 24, 120, 120);
jPanelOben.setOpaque(true);
jPanelOben.setBorder(BorderFactory.createBevelBorder(0, Color.BLACK, Color.BLACK));
jPanelOben.setBackground(Color.WHITE);
cp.add(jPanelOben);
Toggle.setBounds(160, 448, 80, 24);
Toggle.setText("Off");
Toggle.setMargin(new Insets(2, 2, 2, 2));
Toggle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Toggle_ActionPerformed(evt);
}
});
Toggle.setBackground(Color.RED);
Toggle.setBorder(new javax.swing.border.LineBorder(Color.BLACK, 3, true));
cp.add(Toggle);
// Ende Komponenten
setVisible(true);
} // end of main
// Anfang Methoden
public static void Green() {
this.jPanelOben.setBackground(Color.WHITE); //green
this.jPanelMitte.setBackground(Color.WHITE);
this.jPanelUnten.setBackground(Color.GREEN);
System.out.println("Green");
}
public static void Yellow() {
this.jPanelOben.setBackground(Color.WHITE); //yellow
this.jPanelMitte.setBackground(Color.YELLOW);
this.jPanelUnten.setBackground(Color.WHITE);
System.out.println("Yellow");
}
public static void Red() {
this.jPanelOben.setBackground(Color.RED); //red
this.jPanelMitte.setBackground(Color.WHITE);
this.jPanelUnten.setBackground(Color.WHITE);
System.out.println("Red");
}
public static void YellowRed() {
this.jPanelOben.setBackground(Color.RED); //yellowRed
this.jPanelMitte.setBackground(Color.YELLOW);
this.jPanelUnten.setBackground(Color.WHITE);
System.out.println("YellowRed");
}
public void Toggle_ActionPerformed(ActionEvent evt) {
// TODO hier Quelltext einfügen
if (OnOff) {
OnOff = false;
this.Toggle.setBackground(Color.green);
this.Toggle.setText("On");
} else if (!OnOff) {
OnOff = true;
this.Toggle.setBackground(Color.RED);
this.Toggle.setText("Off");
} // end of if-else
} // end of Toggle_ActionPerformed
public static void Ampelphase(boolean OnOff) {
while (OnOff) {
Green();
try {
Thread.sleep(2000);
} catch(Exception e) {
throw new RuntimeException(e);
}
if (OnOff) {
Yellow();
if (OnOff) {
Red();
if (OnOff) {
YellowRed();
} else {
break;
} // end of if-else
} else {
break;
} // end of if-else
} else {
break;
} // end of if-else
} // end of while
}
public static void wait(int ms)
{
try
{
Thread.sleep(ms);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
public static void main(String[] args) {
new AmpelAuto();
}
// Ende Methoden
} // end of class AmpelAuto`
提前致谢
答: 暂无答案
评论
Thread.sleep
static
我有一种爱恨交织的关系 - 在你的上下文中,你不能使用,因为上下文没有关联的对象实例。相反,移除并学会在没有它的情况下生活this
static
static
Thread.sleep