【问题标题】:Java Swing and ActionListenersJava Swing 和 ActionListener
【发布时间】:2014-10-03 02:20:25
【问题描述】:

我对使用 Swing 和在 Java 中创建 GUI 还是很陌生。我正在编写一个简单的测试代码,其中按钮的颜色在按下时会变为随机颜色。虽然它有效,但每次我按下按钮时,它都会最小化上一个窗口并打开一个新窗口,并且它们会不断堆积。我将如何做到这一点,这样就不会发生?发生这种情况是因为我在 actionPerformed 方法中创建了一个对象吗?我在那里创建一个对象的原因是将 Swing 类与我创建的单独的 Action 类链接起来,以便操纵按钮变量。

因此,我的两个问题是:

  • 如何防止出现多个窗口,而是让它们相互替换?
  • 有没有办法在同一个类中使用 ActionListener,它会让事情变得更容易吗?

非常感谢任何帮助!

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.*;




public class Swing extends JFrame{

    private JFrame f;
    private JLabel l;
    private JButton b;
    private JPanel p;



    public Swing(){
        test();
    }

    //*
    public JFrame getJFrame(){
        return f;
    }

    public JLabel getJLabel(){
        return l;
    }

    public JButton getJButton(){
        return b;
    }

    public JPanel getJPanel(){
        return p;
    }
    //*


    public void test(){

    // Frame Setup
    f = new JFrame("Frame");
    f.setVisible(true);
    f.setSize(500, 500);
    f.setResizable(true);
    f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    //


    // Panel Setup
    p = new JPanel();
    p.setVisible(true);
    //

    // Other
    b = new JButton("Button");
    l = new JLabel("Label");
    b.addActionListener(new Action());

    //

    // Additions
    p.add(b);
    p.add(l);
    f.add(p); // ***
    //

}







    public static void main(String[] args){

    Swing swing = new Swing();
    swing.test();




}
}

final class Action implements ActionListener{

    public void actionPerformed(ActionEvent e){
        Swing swingObject = new Swing(); // 

        JButton button = swingObject.getJButton(); // 
        button.setBackground(randomColor());

        }




public Color randomColor(){

    Random rn = new Random();
    ArrayList<Color> color = new ArrayList<Color>();
    color.add(Color.BLUE);
    color.add(Color.GREEN);
    color.add(Color.RED);
    color.add(Color.YELLOW);
    color.add(Color.PINK);
    color.add(Color.CYAN);
    color.add(Color.ORANGE);
    color.add(Color.MAGENTA);


    int s = color.size();
    int random = rn.nextInt(s);

    return color.get(random);

}
}

【问题讨论】:

  • 你应该尽量专注于每个帖子有一个问题。

标签: java swing jframe actionlistener


【解决方案1】:

从你的听众那里,你正在执行

Swing swingObject = new Swing();

这做了它应该做的:创建一个新的 Swing JFrame。你不想要一个新的 JFrame,所以不要调用它的构造函数。从侦听器中,只需获取触发事件的按钮,并更改其颜色:

JButton button = (JButton) e.getSource();
button.setBackground(randomColor());

您也可以在创建监听器时传递按钮进行修改:

class Action implements ActionListener{
    private JButton buttonToUpdate;

    public Action(JButton buttonToUpdate) { 
        this.buttonToUpdate = buttonToUpdate;
    }

    public void actionPerformed(ActionEvent e){
        buttonToUpdate.setBackground(randomColor());
    }
}

并且,要创建它:

b = new JButton("Button");
b.addActionListener(new Action(b));    

【讨论】:

  • 完美!非常感谢我有一种感觉,对象是众多窗口的原因,但我不知道绕过它的好方法,你向我展示了两种好方法,我选择了第二种方法,并在 Action 类中添加了一个构造函数.非常感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
相关资源
最近更新 更多