【问题标题】:How to declare just one如何只申报一个
【发布时间】:2014-03-07 08:22:06
【问题描述】:

我有以下工作代码,其中有两个按钮作为事件侦听器。一个按钮获取并显示一个面板panel1,另一个按钮获取并显示另一个面板panel2,删除现有的显示面板。我为每个按钮创建了两个 actionPerformed 方法来执行彼此的任务。我只想做一个来缩短代码,但我不知道如何检测面板中的哪个按钮在编译时显示。任何帮助将不胜感激。

//Switching between panels (screens)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScreenDemo extends JFrame {
private JPanel panel1, panel2;
private JButton btn1, btn2;
JLabel label1 = new JLabel("Screen 1");
JLabel label2 = new JLabel("Screen 2");

public ScreenDemo() {
    createPanel(); //Created at Line 14
    addPanel(); //Created at Line 28
}

private void createPanel() {
    //Panel for first screen
    panel1 = new JPanel(new FlowLayout());
    btn1 = new JButton("Move to Screen 2");
    btn1.addActionListener(new addScreen1ButtonListener());

    //Panel for second screen
    panel2 = new JPanel(new FlowLayout());
    btn2 = new JButton("Move to Screen 1");
    btn2.addActionListener(new addScreen2ButtonListener());

}

private void addPanel() {
    panel1.add(label1);
    panel1.add(btn1);
    panel2.add(label2);
    panel2.add(btn2);

    add(panel1); //Add the first screen panel

}

class addScreen1ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent ae) {
        getContentPane().removeAll();
        getContentPane().add(panel2);
        repaint();
        printAll(getGraphics()); //Prints all content
    }
}

class addScreen2ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent ea) {
        getContentPane().removeAll();
        getContentPane().add(panel1);
        repaint();
        printAll(getGraphics()); //Prints all content
    }
}

public static void main(String[] args) {
    ScreenDemo screen = new ScreenDemo();
    screen.setTitle("Switching Screens");
    screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    screen.setSize(500, 500);
    screen.setVisible(true);
}
} 

【问题讨论】:

  • 对于一个空间中的多个组件,请使用CardLayout,如short example 所示。或者,考虑将组件放入JTabbedPane

标签: java swing jbutton actionlistener


【解决方案1】:

为您的班级实现 ActionListener,并尝试此代码

       ScreenDemo extends JFrame implements ActionListener
    ...
        btn1.addActionListener(this);
        btn2.addActionListener(this);
    ...
        @override
        public void actionPerformed(ActionEvent ae) {
            if(ae.getSource().equals(btn1)) {
            ...
            }
            else if(ae.getSource().equals(btn1)) {
            ...
            }
        }

【讨论】:

  • 我建议使用 ae.getSource().equals(btn1) 而不是 == 表示法,因为您是在比较对象,而不是原语。
  • 非常感谢@Thanh。我已经根据您的建议进行了修改。
  • @RichardH 首先,我认为您仍在创建一个单独的类来容纳您的 actionPerformed() 方法。这样可行。但是,如果您让主类ScreenDemo 以相同的方式实现ActionListener,则代码可能更具可读性。它只允许你使用一个类。这样,您可以将this 传递到您的动作侦听器方法中,看起来像:myButton.addActionListener(this);this 是一个 Java 关键字,它指的是您在其中使用该关键字的对象的实例。此外,将代码放在 SO 上的 cmets 中通常不是一个好主意,因为它很难阅读。
  • @RichardH 很高兴我能帮上忙。如果您可以在问题下方的另一部分中添加修改后的(并希望可以工作)代码,那么其他提出相同问题的人将不胜感激。谢谢! (欢迎来到 SO)
【解决方案2】:

你必须 implements 来自 ActionListener 的课程 比注册JButton 喜欢:btn.addActionListener(this);

现在如此接近,您只需要捕获事件并从调用它的位置获取源:

 public void actionPerformed(ActionEvent ae) {
        if(ae.getSource == btn1){
            //shuffle panel from btn1
        }
        if(ae.getSource == btn2) {
            //shuffle panel from btn2               
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2023-03-26
    • 2016-01-26
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多