单击按钮后重复重新绘制面板
repaint the panel repeatedly after click a button
我正在尝试编写一个有 2 个按钮的程序,每当我按下第一个按钮时,正方形应该一遍又一遍地重新绘制,并在按下第二个按钮时改变它的颜色。
但它只是重新粉刷一次:(
如果有人可以提供帮助,我将不胜感激。
class Squre {
JFrame frame;
JButton button1;
JButton button2;
MyPanel panel;
public static void main(String[] args){
Squre s= new Squre ();
s.go();
}
public void go(){
frame = new JFrame();
panel= new MyPanel();
button1= new JButton();
button2= new JButton();
button1.setText("START");
//button1.setSize(30, 20);
frame.setVisible(true);
frame.setSize(700,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER ,panel);// add panel
frame .getContentPane().add(BorderLayout.WEST, button1);// add the west button
frame .getContentPane().add(BorderLayout.EAST, button2);//ADD THE EAST BUTTON
button1.addActionListener(new StrListener());
button2.setText("EXPLOSION");
button2.addActionListener(new ExpListener());
}
private class StrListener implements ActionListener{
public void actionPerformed(ActionEvent e){
do{
frame.repaint();
}
while(e.equals(button2)==true);
}
}
private class ExpListener implements ActionListener{
// @Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}class MyPanel extends JPanel{
public void paintComponent(Graphics g){
g.fillRect(0,0,this.getWidth(),this.getHeight());
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
Color rn=new Color(red, green, blue);
g.setColor(rn);
g.fillRect(250, 250, 50, 50);
}
}}
e.equals(button1) // event not equal to a button
e.equals(button1)
永远不会变为真,因为 event
不等于 button
。但是 repaint
运行 一次,因为它是一个 do while 循环.
你应该使用
e.getSource().equals(button1);
检查点击的按钮是否 button1
。
但即使您使用 e.getSource().equals(button1)
,您也不会看到颜色如您预期的那样变化。如果您 运行 为此在 EDT , you will block the EDT
thread .hence colors not get changed but if you put a sout
you will see that loop is running continually .you can use swing timer 中耗费了 while 循环。摇摆计时器不会阻止美国东部时间。
使用 Swing 计时器....
你应该导入 swing timer
//import javax.swing.Timer;
private class StrListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(button1)) {
Timer t = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
frame.repaint();
}
});
t.start();
}
}
}
我正在尝试编写一个有 2 个按钮的程序,每当我按下第一个按钮时,正方形应该一遍又一遍地重新绘制,并在按下第二个按钮时改变它的颜色。 但它只是重新粉刷一次:( 如果有人可以提供帮助,我将不胜感激。
class Squre {
JFrame frame;
JButton button1;
JButton button2;
MyPanel panel;
public static void main(String[] args){
Squre s= new Squre ();
s.go();
}
public void go(){
frame = new JFrame();
panel= new MyPanel();
button1= new JButton();
button2= new JButton();
button1.setText("START");
//button1.setSize(30, 20);
frame.setVisible(true);
frame.setSize(700,700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.CENTER ,panel);// add panel
frame .getContentPane().add(BorderLayout.WEST, button1);// add the west button
frame .getContentPane().add(BorderLayout.EAST, button2);//ADD THE EAST BUTTON
button1.addActionListener(new StrListener());
button2.setText("EXPLOSION");
button2.addActionListener(new ExpListener());
}
private class StrListener implements ActionListener{
public void actionPerformed(ActionEvent e){
do{
frame.repaint();
}
while(e.equals(button2)==true);
}
}
private class ExpListener implements ActionListener{
// @Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}class MyPanel extends JPanel{
public void paintComponent(Graphics g){
g.fillRect(0,0,this.getWidth(),this.getHeight());
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
Color rn=new Color(red, green, blue);
g.setColor(rn);
g.fillRect(250, 250, 50, 50);
}
}}
e.equals(button1) // event not equal to a button
e.equals(button1)
永远不会变为真,因为 event
不等于 button
。但是 repaint
运行 一次,因为它是一个 do while 循环.
你应该使用
e.getSource().equals(button1);
检查点击的按钮是否 button1
。
但即使您使用 e.getSource().equals(button1)
,您也不会看到颜色如您预期的那样变化。如果您 运行 为此在 EDT , you will block the EDT
thread .hence colors not get changed but if you put a sout
you will see that loop is running continually .you can use swing timer 中耗费了 while 循环。摇摆计时器不会阻止美国东部时间。
使用 Swing 计时器....
你应该导入 swing timer
//import javax.swing.Timer;
private class StrListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(button1)) {
Timer t = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
frame.repaint();
}
});
t.start();
}
}
}