当前位置: 首页 > 知识库问答 >
问题:

向ActionListener添加JPanel

伯英锐
2023-03-14

我创建了一个JPanel,上面有我需要的所有JRadioButtons(称为PortSettings)。我还有一个按钮,叫做端口设置,当用户单击该按钮时,我需要JPanel上来显示单选按钮。我尝试将JPanel添加到actionlistener中,但没有效果。我的代码在下面。除了portsettings按钮之外,我已经从其他按钮中删除了所有其他ActionListener。如果这个问题让人困惑,我很抱歉。真的很难解释我需要做什么。我已经上传了一个面板的外观图,以及我的程序的屏幕截图。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;


public class TestApplication implements ActionListener {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(1000, 1000);
        frame.setTitle("RBA Test Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


    JTextArea text = new JTextArea();
    JLabel logLabel = new JLabel("Input/Output Log");



    JRadioButton apprve = new JRadioButton("Approve");
    JRadioButton decline = new JRadioButton("Decline");
    JRadioButton ethernet = new JRadioButton("Ethernet");
    JRadioButton rs = new JRadioButton("RS232");
    JRadioButton usbcdc = new JRadioButton("USB_CDC");
    JRadioButton usbhid = new JRadioButton("USB_HID");

    JButton next = new JButton("Next");
    JButton ok = new JButton("OK");
    JButton cancel = new JButton("Cancel");

    JPanel PortSettings = new JPanel();
    PortSettings.add(ethernet);
    PortSettings.add(rs);
    PortSettings.add(usbcdc);
    PortSettings.add(usbhid);
    PortSettings.add(next);
    PortSettings.add(cancel);


    JButton initialize = new JButton("Initialize");

    JButton connect = new JButton("Connect");

    JButton disconnect = new JButton("Disconnect");

    JButton shutdown = new JButton("Shut Down");


    JButton portsettings = new JButton("Port Settings");
     portsettings.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {




            }
        });


    JButton online = new JButton("Go Online");

    JButton offline = new JButton("Go Offline");

    JButton status = new JButton("Status");

    JButton reboot = new JButton("Reboot");


    JButton account = new JButton("Account");


    JButton amount = new JButton("Amount");

    JButton reset = new JButton("Reset");


    JButton approvordecl = new JButton("Approve / Decline");

    JButton test = new JButton("Test Button #1");

    JButton testing = new JButton("Test Button #2");

    JRadioButton button = new JRadioButton("Radio Button");

    JRadioButton button2 = new JRadioButton("Radio Button");

    JCheckBox checkbox = new JCheckBox("Check Box");

    JCheckBox checkbox2 = new JCheckBox("Check Box");


    JPanel testPanel = new JPanel();
    testPanel.add(button);
    testPanel.add(button2);
    testPanel.add(checkbox2);

    JPanel posPanel = new JPanel();
    posPanel.add(test);
    posPanel.add(testing);
    posPanel.add(checkbox);

    JPanel llpPanel = new JPanel();
    llpPanel.add(online);
    llpPanel.add(offline);
    llpPanel.add(status);
    llpPanel.add(reboot);
    llpPanel.add(account);
    llpPanel.add(amount);
    llpPanel.add(reset);
    llpPanel.add(approvordecl);

    JPanel textPanel = new JPanel(new BorderLayout());
    textPanel.add(logLabel);
    frame.add(logLabel); 


    JPanel buttonPanel = new JPanel();
    buttonPanel.add(initialize);
    buttonPanel.add(connect);
    buttonPanel.add(disconnect);
    buttonPanel.add(shutdown);
    buttonPanel.add(portsettings);
    frame.add(buttonPanel);
    frame.add(buttonPanel, BorderLayout.NORTH);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
    tabbedPane.addTab("Test", null, testPanel, "Test");

    JPanel tabsPanel = new JPanel(new BorderLayout());
    tabsPanel.add(tabbedPane);
    frame.add(tabsPanel, BorderLayout.CENTER);


    frame.pack();



}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}






} 

我尝试将JFrame添加到ActionListener,然后将JPanel添加到JFrame,但当我单击Port Settings按钮时什么也没有发生。此外,当我试图将JPanel添加到JFrame时,它告诉我将final放在JPanel PortSettings=new JPanel();前面。这是密码。

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;


public class TestApplication implements ActionListener {
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(1000, 1000);
    frame.setTitle("RBA Test Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);


    JTextArea text = new JTextArea();
    JLabel logLabel = new JLabel("Input/Output Log");



    JRadioButton apprve = new JRadioButton("Approve");
    JRadioButton decline = new JRadioButton("Decline");
    JRadioButton ethernet = new JRadioButton("Ethernet");
    JRadioButton rs = new JRadioButton("RS232");
    JRadioButton usbcdc = new JRadioButton("USB_CDC");
    JRadioButton usbhid = new JRadioButton("USB_HID");

    JButton next = new JButton("Next");
    JButton ok = new JButton("OK");
    JButton cancel = new JButton("Cancel");

    final JPanel PortSettings = new JPanel();
    PortSettings.add(ethernet);
    PortSettings.add(rs);
    PortSettings.add(usbcdc);
    PortSettings.add(usbhid);
    PortSettings.add(next);
    PortSettings.add(cancel);

    JPanel accountButton = new JPanel();
    accountButton.add(ok);
    accountButton.add(cancel);

    JPanel apprvordecl = new JPanel();
    apprvordecl.add(apprve);
    apprvordecl.add(decline);

    JPanel amountButton = new JPanel();
    amountButton.add(ok);
    amountButton.add(cancel);



    JButton initialize = new JButton("Initialize");

    JButton connect = new JButton("Connect");

    JButton disconnect = new JButton("Disconnect");

    JButton shutdown = new JButton("Shut Down");


    JButton portsettings = new JButton("Port Settings");
     portsettings.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFrame port = new JFrame("Port Settings");
                port.add(PortSettings);
                frame.setVisible(true);





            }
        });

    JButton online = new JButton("Go Online");

    JButton offline = new JButton("Go Offline");

    JButton status = new JButton("Status");

    JButton reboot = new JButton("Reboot");


    JButton account = new JButton("Account");


    JButton amount = new JButton("Amount");

    JButton reset = new JButton("Reset");


    JButton approvordecl = new JButton("Approve / Decline");

    JButton test = new JButton("Test Button #1");

    JButton testing = new JButton("Test Button #2");

    JRadioButton button = new JRadioButton("Radio Button");

    JRadioButton button2 = new JRadioButton("Radio Button");

    JCheckBox checkbox = new JCheckBox("Check Box");

    JCheckBox checkbox2 = new JCheckBox("Check Box");


    JPanel testPanel = new JPanel();
    testPanel.add(button);
    testPanel.add(button2);
    testPanel.add(checkbox2);

    JPanel posPanel = new JPanel();
    posPanel.add(test);
    posPanel.add(testing);
    posPanel.add(checkbox);

    JPanel llpPanel = new JPanel();
    llpPanel.add(online);
    llpPanel.add(offline);
    llpPanel.add(status);
    llpPanel.add(reboot);
    llpPanel.add(account);
    llpPanel.add(amount);
    llpPanel.add(reset);
    llpPanel.add(approvordecl);

    JPanel textPanel = new JPanel(new BorderLayout());
    textPanel.add(logLabel);
    frame.add(logLabel); 


    JPanel buttonPanel = new JPanel();
    buttonPanel.add(initialize);
    buttonPanel.add(connect);
    buttonPanel.add(disconnect);
    buttonPanel.add(shutdown);
    buttonPanel.add(portsettings);
    frame.add(buttonPanel);
    frame.add(buttonPanel, BorderLayout.NORTH);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
    tabbedPane.addTab("Test", null, testPanel, "Test");

    JPanel tabsPanel = new JPanel(new BorderLayout());
    tabsPanel.add(tabbedPane);
    frame.add(tabsPanel, BorderLayout.CENTER);


    frame.pack();



}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}






}

共有1个答案

别俊誉
2023-03-14

您的思路是正确的,但您不想将portsettings面板添加到新的JFrame中,而是添加到以前构建的JFrame中,分配给本地变量Frame。所以您的操作监听器应该是

portsettings.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           frame.add(PortSettings, BorderLayout.SOUTH);
           frame.pack();
       }
   });

(这是假设你真的想在那一刻把它添加到帧中,而不是像@Aleksei建议的那样,从一开始就不可见地添加它,然后把它变成可见的。)

关于final的错误消息是因为您在(匿名)内部类中使用了portsettings-即ActionListener。在我提议的修改中,frame也是如此,因此您还需要调整它的声明:

final JFrame frame = new JFrame();

原因是相当技术性的,现在离题了:就这么做吧。

如果您希望面板出现在单独的窗口中,则需要JDialog/code>,而不是第二个JFrame:

portsettings.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           JDialog dialog = new JDialog(frame);
           dialog.add(PortSettings);
           dialog.pack();
           dialog.setVisible(true);
       }
   });

查看JoptionPane类,了解从对话框中获得更多功能的丰富方法选择。

 类似资料:
  • 问题内容: 如何将actionListener添加到使用netbeans放置的现有jCalendar的jDayChooser组件中? 我只想在单击日期按钮时才触发事件。当jCalendar中的propertyChange甚至侦听jMonthChooser和jYearChooser时 使用Toedter的jCalendar的PS 问题答案: 或者,你可以侦听特定的,。 附录: 如何使它适用于 ? 同

  • 我有以下问题: 我有这样一个函数: 我的问题如下:我需要一个解决方案,可以通过addActionListener函数作为参数传递任何对象。列表可在此处找到:https://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html 有没有一种方法可以在没有大量实例的情况下解决这个问题? 谢啦

  • 我知道在Java中,可以使用在一定延迟后执行特定任务。这篇文章展示了如何在特定日期执行任务,但不是使用。例如,我有一个如下初始化的格式: 此外,我想执行在中的语句下声明的特定任务,例如: 如何初始化在特定日期在内和/或使用语句执行的,最好使用?

  • 嗨,我的问题是我不能添加按钮到动作听者,我会做一个菜单,我不知道为什么我成为一个错误在这里代码包lvl; }哦,我变成了一个错误,错误说不能在静态上下文中使用它,它是由credits.addActionListener(this)创建的;请帮助我

  • 问题内容: 如何将动作侦听器添加到这些按钮,以便可以从主要方法调用actionperformed它们,因此单击它们时可以在程序中调用它们? 问题答案: 两种方式: 1.在你的类中实现,然后使用; 稍后,你必须定义一个方法。但是,对多个按钮执行此操作可能会造成混淆,因为该方法将必须检查每个事件()的来源以查看其来自哪个按钮。 2.使用匿名内部类: 稍后,你必须定义。当你有多个按钮时,这样做效果更好,