这样:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
public class MainClass extends JFrame {
public JComboBox box;
int flag = 0;
jpNewPanel jpNewPanel;
public static void main(String[] args) {
MainClass frame = new MainClass();
frame.setBounds(650,300,550,550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("信号灯");
frame.setVisible(true);
}
public MainClass() {
box = new JComboBox();
box.addItem("请选择");
box.addItem("红灯");
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = box.getSelectedIndex();
jpNewPanel.repaint();
}
});
box.addItem("黄灯");
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = box.getSelectedIndex();
jpNewPanel.repaint();
}
});
box.addItem("绿灯");
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = box.getSelectedIndex();
jpNewPanel.repaint();
}
});
add(box, BorderLayout.NORTH);
jpNewPanel = new jpNewPanel();
add(jpNewPanel, BorderLayout.CENTER);
}
class jpNewPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(150, 0, 120, 120);
if (flag == 1) {
g.setColor(Color.RED);
g.fillOval(150, 0, 120, 120);
} else if (flag == 2) {
g.setColor(Color.YELLOW);
g.fillOval(150, 0, 120, 120);
} else if (flag == 3) {
g.setColor(Color.GREEN);
g.fillOval(150, 0, 120, 120);
}
}
}
}
注意事项
每个Road对象都有一个name成员变量来代表方向,有一个vehicles成员变量来代表方向上的车辆集合。
在Road对象的构造方法中启动一个线程每隔一个随机的时间向vehicles集合中增加一辆车(用一个“路线名_id”形式的字符串进行表示)。
在Road对象的构造方法中启动一个定时器,每隔一秒检查该方向上的灯是否为绿,是则打印车辆集合和将集合中的第一辆车移除掉。
JAVA 编写一个带有窗口的应用程序
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.awt.Graphics;public class MainClass extends JFrame { public JComboBox box;int flag = 0;jpNewPanel jpNewPanel;public static void main(String[] args) { MainClass frame = new MainClass();frame.setBounds...
Java编写一个简单的窗口程序,流程是什么?
import javax.swing.*;\\x0d\\x0aimport java.awt.*;\\x0d\\x0aimport java.awt.event.*;\\x0d\\x0apublic class Test3 extends JFrame implements ActionListener{\\x0d\\x0a JMenu m;\\x0d\\x0a JMenuItem mi1,mi2;\\x0d\\x0a JMenuBar mb;\\x0d\\x0a \\x0d\\x0a public Test3()...
求用JAVA编写一个九九乘法表,并在创建的窗口中显示出来
import java.awt.*;public class AppletTest extends Applet { public void paint(Graphics g) { g.setColor (Color.GREEN);g.drawString("九九乘法表",50,20);g.setColor (Color.BLUE);g.drawRect(50, 40, 500, 200);g.setColor (Color.PINK);for(int i=1;i<=9;i++){ for(int j=...
java编写一个窗体应用程序,在窗体中安排两个文本框,一个标签,两个标记...
按照你的要求编写的Java程序如下:import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextField;public class Calculate...
编写一个图形用户界面的Java Application 程序要求如下图求求了很着急...
\/\/ 设置窗口标题 setTitle("Java Application");\/\/ 创建菜单栏 JMenuBar menuBar = new JMenuBar();\/\/ 创建文件菜单 JMenu fileMenu = new JMenu("文件");JMenuItem newMenuItem = new JMenuItem("新建");JMenuItem openMenuItem = new JMenuItem("打开");JMenuItem exitMenuItem = new ...
JAVA编写一个窗口(frame),要求窗口中有文本框,按钮,标签,单选框,复选...
checkbox = new JCheckBox("复选框一");radiobutton = new JRadioButton("单选框一");combobox = new JComboBox();textfield = new JTextField(100);Container c = this.getContentPane();c.setLayout(new FlowLayout());c.add(button);c.add(label);c.add(checkbox);c.add(radiobutton)...
请JAVA大神帮忙写一个窗口界面的程序,可以读写文件的,有一定的功能的就...
package test.xxl;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import javax.swing....
用java写一个窗口
private JButton but_1;private JButton but_2;private JButton but_3;public MyFrame(){ setSize(245, 260);setTitle("JLabel Demo");Toolkit tk = Toolkit.getDefaultToolkit();setLocation((tk.getScreenSize().height - this.HEIGHT)\/2,(tk.getScreenSize().width - this.WIDTH)\/2);thi...
编写swing程序,建立一个窗口类名为room窗口,标题为"房间预订管理系统...
lz 你好 具体代码如下:import java.awt.*;import java.awt.event.*;import javax.swing.*;public class RoomOrder extends JFrame{ private JLabel labName, labID, labSelect; private JTextField fieldName, fieldID; private JComboBox cbRoom; private JTextArea taResume; private...
用java编写一个窗口,在窗口的输入框里输入名字,新弹出一个窗口,出现...
import java.awt.event.*;import javax.swing.*;import java.util.*;class LoginWindow extends JDialog implements ActionListener { JPanel p1=new JPanel(); \/\/定义并建立面板 参考资料:http:\/\/hi.baidu.com\/wdxxin888\/blog\/item\/d86bb6ed8b32fd4e78f0558c.html ...