java 多线程+图形界面
为什么它们会闪烁同样的颜色,时间不应该是不一样没?怎样让他们闪不同的颜色,要同时闪.谢了!importjava.awt.*;importjava.awt.event.Ac...
为什么它们会闪烁同样的颜色,时间不应该是不一样没?
怎样让他们闪不同的颜色,要同时闪.谢了!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class PaintovalPane extends JPanel implements Runnable{
int x;
int y;
int r;
public PaintovalPane(int x,int y,int r ){
this.x = x ;
this.y = y ;
this.r = r ;
}
public void paintComponent(Graphics g) {
Random rd = new Random(System.currentTimeMillis());
super.paintComponent(g);
g.setColor(Color.getHSBColor(rd.nextInt(255),
rd.nextInt(255), rd.nextInt(255)));
g.fillOval(x, y, r, r);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
repaint();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class InputPane extends JPanel{
JTextField jtf1 = new JTextField("",5);
JTextField jtf2 = new JTextField("",5);
JLabel jl1 = new JLabel("行数");
JLabel jl2 = new JLabel("列数");
public InputPane(){
jl1.setBounds(0, 0, 50, 20);
jl2.setBounds(0, 25, 50, 20);
jtf1.addActionListener(new My_ActionListener1());
jtf2.addActionListener(new My_ActionListener1());
jtf1.setBounds(50, 0, 50, 20);
jtf2.setBounds(50, 25, 50, 20);
setLayout(null);
add(jl1);
add(jl2);
add(jtf1);
add(jtf2);
}
}
class My_ActionListener1 implements ActionListener{
String info;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
info = (String)e.getActionCommand();
System.out.println(info);
}
}
class PaintovalFrame extends JFrame{
public PaintovalFrame() {
setTitle("lamp");
setSize(700,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
InputPane ip = new InputPane();
ip.setBounds(250, 300, 200, 100);
contentPane.add(ip);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
PaintovalPane jp = new PaintovalPane(0,0,50);
jp.setBounds(50*i, 50*j, 50, 50);
contentPane.add(jp);
new Thread(jp).start();
}
}
}
}
public class Paintoval {
public static void main(String[] args)
{
JFrame f = new PaintovalFrame();
f.show();
}
} 展开
怎样让他们闪不同的颜色,要同时闪.谢了!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class PaintovalPane extends JPanel implements Runnable{
int x;
int y;
int r;
public PaintovalPane(int x,int y,int r ){
this.x = x ;
this.y = y ;
this.r = r ;
}
public void paintComponent(Graphics g) {
Random rd = new Random(System.currentTimeMillis());
super.paintComponent(g);
g.setColor(Color.getHSBColor(rd.nextInt(255),
rd.nextInt(255), rd.nextInt(255)));
g.fillOval(x, y, r, r);
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
repaint();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class InputPane extends JPanel{
JTextField jtf1 = new JTextField("",5);
JTextField jtf2 = new JTextField("",5);
JLabel jl1 = new JLabel("行数");
JLabel jl2 = new JLabel("列数");
public InputPane(){
jl1.setBounds(0, 0, 50, 20);
jl2.setBounds(0, 25, 50, 20);
jtf1.addActionListener(new My_ActionListener1());
jtf2.addActionListener(new My_ActionListener1());
jtf1.setBounds(50, 0, 50, 20);
jtf2.setBounds(50, 25, 50, 20);
setLayout(null);
add(jl1);
add(jl2);
add(jtf1);
add(jtf2);
}
}
class My_ActionListener1 implements ActionListener{
String info;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
info = (String)e.getActionCommand();
System.out.println(info);
}
}
class PaintovalFrame extends JFrame{
public PaintovalFrame() {
setTitle("lamp");
setSize(700,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
InputPane ip = new InputPane();
ip.setBounds(250, 300, 200, 100);
contentPane.add(ip);
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
PaintovalPane jp = new PaintovalPane(0,0,50);
jp.setBounds(50*i, 50*j, 50, 50);
contentPane.add(jp);
new Thread(jp).start();
}
}
}
}
public class Paintoval {
public static void main(String[] args)
{
JFrame f = new PaintovalFrame();
f.show();
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询