0

端口设置面板 截屏

我创建了一个 JPanel,上面有我需要的所有 JRadioButton(它称为 PortSettings)。我还有一个按钮,称为端口设置,当用户单击该按钮时,我需要 JPanel 出现并显示单选按钮。我试图将 JPanel 添加到 actionlistener 但它不起作用。我的代码如下。除了端口设置按钮之外,我已经从其他按钮中删除了所有其他 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

}






}
4

2 回答 2

1

你在正确的轨道上,但你不想将你的PortSettings面板添加到一个新的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,而不是第二个JFrame

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

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

于 2013-05-28T21:29:56.143 回答
0

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

yourButton.addActionListener(this);

对所有按钮执行此操作。

然后采取您的 TestPalication 类的 actionPreformed 方法并做任何事情:

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

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

于 2013-05-28T19:25:50.410 回答