2024-06-02 14:02:44 | 我爱编程网
这样:
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对象的构造方法中启动一个定时器,每隔一秒检查该方向上的灯是否为绿,是则打印车辆集合和将集合中的第一辆车移除掉。
package cn.fu;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;我爱编程网
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Window.Type;
public class Login extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Login() {
setTitle("工具");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setToolTipText("");
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(121, 86, 194, 21);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblNewLabel = new JLabel("请输入10位数以内的字符串");
lblNewLabel.setBounds(145, 59, 194, 15);
contentPane.add(lblNewLabel);
JButton btnNewButton = new JButton("确定");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ca = textField.getText();
int n = ca.length();
if (n > 10) {
JOptionPane.showMessageDialog(null, "对不起,您输入的字符串长度超过10",
"错误提示", JOptionPane.ERROR_MESSAGE);
} else if (n >= 0 || n <= 10) {
JOptionPane.showMessageDialog(null, "字符串长度为" + n, "提示",
JOptionPane.PLAIN_MESSAGE);
}
}
});
btnNewButton.setBounds(172, 130, 93, 23);
contentPane.add(btnNewButton);
}
}
我爱编程网(https://www.52biancheng.com)小编还为大家带来java中程序输入输出以对话框的形式表现怎么做?的相关内容。
functionmal(){
alert('这是一个普通的提示框');
}
functionmpro(){
varval=prompt('这是一个可输入的提示框','这个参数为输入框默认值,可以不填哦');
//prompt会把输入框的值返回给你
}
functionmcon(){
varboo=confirm('这是一个可选择的提示框,3种提示方式,学会了吗?')
//confirm会返回你选择的选项,然后可以依据选择执行逻辑
if(boo){
alert('学会了,真聪明');
}else{
alert('再来一遍吧')
}
}
2025-02-01 20:24:39
2025-02-12 03:21:37
2025-02-10 15:19:48
2025-01-28 17:58:32
2024-11-22 05:08:01
2024-09-10 08:50:00