gpt4 book ai didi

java - 将 JPanel 添加到 ActionListener

转载 作者:行者123 更新时间:2023-12-01 14:32:02 25 4
gpt4 key购买 nike

PortSettings Panel screenshot

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

}






}

最佳答案

您的方向是正确的,但您不想将 PortSettings 面板添加到新的 JFrame 中,而是希望将其添加到之前构建的面板上的某处,并分配给局部变量frame。所以你的 Action 监听器应该是

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

(这是假设您实际上想在那一刻将其添加到框架中,而不是从一开始就将其不可见地添加到可见,就像 @Aleksei 建议的那样。)

有关 final 的错误消息是因为您在(匿名)内部类(即 ActionListener)中使用了 PortSettings。在我建议的修改中,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 类,了解从对话框中获取更多功能的丰富方法。

关于java - 将 JPanel 添加到 ActionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16799505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com