//按回车键就可以
站在用户的角度思考问题,与客户深入沟通,找到桂林网站设计与桂林网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站制作、成都做网站、企业官网、英文网站、手机端网站、网站推广、空间域名、虚拟主机、企业邮箱。业务覆盖桂林地区。
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Print {
public static void main(String[] args) {
JFrame jFrame = new JFrame ();
final JLabel jLabel = new JLabel ("按回车键!");
jFrame.setLayout(null);
jLabel.setBounds(80,50,500,80);
jFrame.add(jLabel);
jFrame.setSize(200, 200);
jFrame.setLocation(200, 300);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.addKeyListener(new KeyAdapter () {
int n = 0;
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_ENTER) {
n++;
if (n == 1)
jLabel.setText("绿");
else if (n == 2) {
jLabel.setText("红");
} else if (n == 3) {
jLabel.setText("黄");
n = 0;
}
}
}
});
}
}
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Way extends JPanel {
JButton red;
public Way() {
red= new JButton("换灯");
setBackground(Color.yellow);
setSize(new Dimension(320, 260));
setPreferredSize(new Dimension(320, 260) );
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new FlowLayout());
red.setLayout(new FlowLayout());// 将单选按钮加入按钮面板
btnPanel.add(red);
add(red);
}
private void lightRed(Graphics g) {
g.setColor(Color.red);
g.fillOval(getWidth() / 2, 30, 15, 15);
g.setColor(Color.black);
g.fillOval(getWidth() / 2, 50, 15, 15);
g.fillOval(getWidth() / 2, 70, 15, 15);
}
private void lightYellow(Graphics g) {
g.setColor(Color.yellow);
g.fillOval(getWidth() / 2, 50, 15, 15);
g.setColor(Color.black);
g.fillOval(getWidth() / 2, 30, 15, 15);
g.fillOval(getWidth() / 2, 70, 15, 15);
}
private void lightGreen(Graphics g) {
g.setColor(Color.green);
g.fillOval(getWidth() / 2, 70, 15, 15);
g.setColor(Color.black);
g.fillOval(getWidth() / 2, 30, 15, 15);
g.fillOval(getWidth() / 2, 50, 15, 15);
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
Way a = new Way();
g.clearRect(0, 0, getWidth(), getHeight());
g.drawRect(getWidth() / 2, 30, 30, 80);
red.addActionListener(new ActionListener() {
int f1 = 0;
public void actionPerformed(ActionEvent e) {
Graphics g = getGraphics();
switch (f1) {
case 0:
a.lightRed(g);
break;
case 1:
a.lightYellow(g);
break;
case 2:
a.lightGreen(g);
break;
}
f1++;
if(f12) f1=0;
};
});
}
public static void main(String[] args) {
JFrame fr = new JFrame("邮件界面模拟");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击x结束程序
Container contentPane = fr.getContentPane();
// 得到窗口内容面板
contentPane.add(new Way());
fr.pack();
fr.setVisible(true); // 设置窗口可见
}
}大致帮你改了下,不知道符合不符合你的要求,有问题请追问
关键是启动一个线程控制颜色。代码如下。
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Signal extends Applet {
int width = 200, height = 240;
int w = 50, h = 50;
int x = (width - w) / 2, y1 = (height - h * 3) / 3, y2 = y1 + h, y3 = y2 + h;
Color c = Color.RED;
@Override
public void init() {
ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
if (c == Color.RED) {
c = Color.YELLOW;
} else if (c == Color.YELLOW) {
c = Color.GREEN;
} else if (c == Color.GREEN) {
c = Color.RED;
}
repaint();
}
}, 5, 5, TimeUnit.SECONDS);
}
@Override
public void paint(Graphics g) {
setBackground(Color.white);
// all gray
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x, y1, w, h);
g.fillOval(x, y2, w, h);
g.fillOval(x, y3, w, h);
if (c == Color.RED) {
g.setColor(Color.RED);
g.fillOval(x, y1, w, h);
} else if (c == Color.YELLOW) {
g.setColor(Color.YELLOW);
g.fillOval(x, y2, w, h);
} else if (c == Color.GREEN) {
g.setColor(Color.GREEN);
g.fillOval(x, y3, w, h);
}
}
}
网站栏目:红绿灯代码java c语言红绿灯代码
当前路径:https://www.cdcxhl.com/article34/ddiopse.html
成都网站建设公司_创新互联,为您提供商城网站、网站制作、App开发、微信小程序、电子商务、静态网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联