Java 将JPanel添加到ActionListener

Java 将JPanel添加到ActionListener,java,swing,jpanel,actionlistener,jradiobutton,Java,Swing,Jpanel,Actionlistener,Jradiobutton,我已经创建了一个JPanel,上面有我需要的所有JRadioButtons(称为PortSettings)。我还有一个按钮,叫做端口设置,当用户单击该按钮时,我需要JPanel出现并显示单选按钮。我曾尝试将JPanel添加到actionlistener,但没有成功。我的代码如下。我已从除portsettings按钮之外的其他按钮中删除了所有其他ActionListener。如果这个问题让人困惑,我很抱歉。很难解释我需要做什么。我已经上传了面板的外观图以及我的程序截图 import java.aw

我已经创建了一个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,但单击端口设置按钮时,什么也没有发生。另外,当我试图将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

}






}

只需将动作侦听器添加到所有按钮。 像这样:

yourButton.addActionListener(this);
对所有按钮都这样做

然后使用TestPalication类的actionPreformed方法执行以下操作:

@Override
public void actionPerformed(ActionEvent arg0) {
    ((JRadioButton) arg0.getSource()).setTitle("Clicked!");
}

您的问题有点让人困惑,但我希望这能澄清一点。

您的思路是正确的,但您不想将
PortSettings
面板添加到新的
JFrame
面板,而是添加到先前构建的面板的某个位置,分配给局部变量
frame
。所以你的行动听众应该

portsettings.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           frame.add(PortSettings, BorderLayout.SOUTH);
           frame.pack();
       }
   });
(这是假设您确实希望在该时刻将其添加到帧中,而不是从一开始就以不可见的方式添加并使其可见,如@Aleksei建议的那样。)

关于
final
的错误消息是因为您在(匿名)内部类中使用了
PortSettings
,即
ActionListener
。在我建议的修改中,
框架
,同样适用,因此您还需要修改其声明:

final JFrame frame = new JFrame();
原因是相当技术性的,而且现在离题了:就这么做吧

如果希望面板显示在单独的窗口中,则需要一个
JDialog
,而不是第二个
JFrame

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

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

可能是我不懂一些东西,但可能是在这个JFrame上制作一些带有按钮的JPanel,让它们可见或不可见会更好。当我单击按钮portsettings时,我需要打开面板端口设置。我刚刚贴了一张照片,上面是我布置好面板后的样子。