【问题标题】:Nesting actionPerformed()嵌套 actionPerformed()
【发布时间】:2013-03-28 12:46:35
【问题描述】:

我正在设计一个允许用户预订飞机航班的程序。程序第一次运行时,会打开一个带有两个按钮的 JFrame。根据单击的按钮,actionPerformed 会打开新的 JPanel 等。

我正在尝试在 JTabbedPane 中创建一个选项卡,该选项卡将显示两个按钮。单击任一按钮将导致不同的图像,但这部分代码已经在 actionPerformed 方法中。如何知道点击了哪个按钮?

这是我使用的方法:

protected JComponent makeImagePanel(String path1, String path2) 
{
    try{
        JPanel panel= new JPanel(false);
        JButton international= new JButton("International Flights");
        JButton domestic= new JButton("Domestic Flights");
        international.setActionCommand("login");
        domestic.setActionCommand("domestic");
        international.setEnabled(true);
        international.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    BufferedImage myPicture = ImageIO.read(new File(path1));
                    JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
                }
            });
        domestic.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    BufferedImage myPicture = ImageIO.read(new File(path2));
                    JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
                }
            });

这是我尝试实现它的地方:

JComponent reservation= makeImagePanel("international_1.gif", "domestic_seating.gif");
            overview.addTab ("Reserve a Flight", reservation);
            overview.setMnemonicAt(1, KeyEvent.VK_2);

提前感谢您的帮助!

【问题讨论】:

    标签: java image swing button user-interface


    【解决方案1】:

    ActionEvent 有一个 getSource() 方法,它将为您提供触发事件的对象。或者您可以设置全局变量以找出单击了哪个按钮。有很多方法可以做到。

    问候, 拉维

    【讨论】:

    • 谢谢!这帮助很大:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多