【问题标题】:How to open a Jframe which is in another class as a constructor with actionListener?如何使用actionListener打开另一个类中的Jframe作为构造函数?
【发布时间】:2014-11-20 16:13:38
【问题描述】:

基本上我有 1 个扩展 JFrame 并实现 ActionListener 的主类,并且我创建了一个作为我的主窗口的窗口。现在我想制作另一个窗口,当我单击主窗口上的按钮时会打开它。为此,我创建了另一个扩展 JFrame 的类并创建了另一个窗口。现在我不确定如何设置它,以便在我单击主窗口中的按钮时打开我创建的新窗口。我在那个主要课程中有我的 actionPerformed。我不确定什么语句会让我的窗口弹出。

【问题讨论】:

    标签: java swing


    【解决方案1】:

    您想要做的是在主窗口的“添加信用”按钮上设置一个监听器。也就是说,您想在您的 SlotMachine 类中实现 ActionListener(这是包含按钮的类,对吧?)。 您确实希望将此代码添加到您的 SlotMachine 类中,而不是添加到另一个类中:

    public void actionPerformed(ActionEvent e){
        String butonCommand = e.getActionCommand();
        if(butonCommand.equals("Add Credits")){
            // The add credits button has been fired. Make the other window visible
        }
    }
    

    您想让添加学分窗口可见,也就是说,您希望执行以下操作:

    addCreditsFrame.setVisible(true);
    

    如果您在主类中添加监听器,它需要能够访问必须可见的框架。您需要将一个新的 AddCreditsClass 发送到您的 SlotMachine 构造函数,以便他可以将其设置为可见。您不能使用 AddCreditsClass.setVisible(true) 因为您正在设置可见的框架,而不是整个班级。 也就是说,类似于:

    public class SlotMachine {
        AddCreditsClass otherframe;
        public SlotMachine(AddCreditsClass otherFrame) {
            this.otherFrame=otherFrame;
            // ...
        }
        public void actionPerformed(ActionEvent e){
            String butonCommand = e.getActionCommand();
            if(butonCommand.equals("Add Credits")){
                otherFrame.setVisible(true);
            }
        }
    }
    

    你会得到以下代码:

    public static void main(String... args) {
            SlotMachine mainFrame = new SlotMachine(new AddCreditsClass());
            mainFrame.setVisible(true); // The main window has to be visible at the beginning   
    
    public class AddCreditsClass extends JFrame implements ActionListener{
    
    public static final int WIDTH = 700;
    public static final int HEIGHT = 500;
    
    private static JLabel addCreditsLabel;
    
    public AddCreditsClass(){
        super("Add Credits");
        setSize(WIDTH,HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        setLayout(null);
        // setVisible(true); Do _not_ do this! You want the window to be hidden until you set it visible by yourself.
        }
        // No listener here, the button listener should be in the main class
    }
    

    在包含“添加信用”按钮的 SlotMachine 类中,您将添加我之前提到的侦听器:

    public void actionPerformed(ActionEvent e){
        String butonCommand = e.getActionCommand();
        if(butonCommand.equals("Add Credits")){
            addCreditsFrame.setVisible(true);
        }
    }
    

    呃!对不起,我没有看到你在我之前回答。我应该删除这个答案吗?

    【讨论】:

    • 我发现从不同来源阅读同一主题非常有用。所以不,绝对不是。 :)
    【解决方案2】:

    好吧,当单击按钮时,您希望调用 actionPerformed()。但是按钮在 SlotMachine 中,所以 actionPerformed() 也需要在 SlotMachine 类中。您没有发布 SlotMachine,但它看起来像这样:

    public class SlotMachine extends JFrame implements ActionListener {
        public static void main(String[] args) {
            SlotMachine mainFrame = new SlotMachine();
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setSize(640, 480);
            mainFrame.setLocationRelativeTo(null); // center on screen        
            mainFrame.setVisible(true);        
        }
    
        public SlotMachine() {
            setLayout(new BorderLayout());
            JButton button = new JButton("Open new window");
            button.setActionCommand("Add Credits");
            button.addActionListener(this);
            add(button, BorderLayout.CENTER);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            String butonCommand = e.getActionCommand();
            if (butonCommand.equals("Add Credits")) {
                AddCreditsClass addCredits = new AddCreditsClass();
                addCredits.setLocationRelativeTo(this);
                addCredits.setVisible(true);
            }
        }
    }
    

    在 SlotMachine 的构造函数中,我们添加一个按钮并为该按钮添加一个侦听器。因此,当按下该按钮时,将调用 actionPerformed()。正如您在 actionPerformed() 中看到的,我们创建了一个新的 AddCreditsClass 实例并使其可见。

    我们将大部分代码从 AddCreditsClass 移至 SlotMachine,但剩下的代码如下所示:

    public class AddCreditsClass extends JFrame {
        public static final int WIDTH = 700;
        public static final int HEIGHT = 500;
    
        private static JLabel addCreditsLabel;
    
        public AddCreditsClass() {
            super("Add Credits");
            setSize(WIDTH, HEIGHT);        
            setLayout(null);        
        }
    }
    

    【讨论】:

    • 这很有效,非常感谢。你能告诉我这个“addCreditsFrame.setLocationRelativeTo(this);”是做什么的吗?做什么?
    • 啊,忘记说了。它使窗口相对于另一个窗口居中。如果您执行 setLocationRelativeTo(null) ,它将在屏幕上居中。
    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 2012-06-22
    • 2017-12-31
    相关资源
    最近更新 更多