将JPanel添加到ActionListener

时间:2013-05-28 19:04:09

标签: java swing jpanel actionlistener jradiobutton

PortSettings Panel screenshot

我创建了一个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时,它告诉我将最终放在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

}






}

2 个答案:

答案 0 :(得分:1)

你是在正确的轨道上,但你不想将你的PortSettings面板添加到新的JFrame,而是放在先前构建的面板的某个地方,分配给本地变量{{1} }。所以你的动作听众应该是

frame

(假设您实际上想要在那一刻将其添加到框架中,而不是从一开始就将其隐藏起来并将其变为可见,就像@Aleksei建议的那样。)

关于portsettings.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame.add(PortSettings, BorderLayout.SOUTH); frame.pack(); } }); 的错误消息是因为您在(匿名)内部类中使用final - 即PortSettings。在我提议的修改中,同样适用于ActionListener,因此您还需要调整其声明:

frame

之所以如此技术性很强,而且现在就是这样:只是这样做。

如果您希望面板显示在单独的窗口中,则需要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); } }); 课程,了解从对话框中获取更多功能的丰富选择。

答案 1 :(得分:0)

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

yourButton.addActionListener(this);

为所有按钮执行此操作。

然后使用TestPalication类的actionPreformed方法并执行任何操作:

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

你的问题有点令人困惑,但我希望这会澄清一点。