我在下面附上了我的代码,我的程序的屏幕截图,以及我希望标签看起来像什么的图纸。我需要我的JPanel textPanel出现在LLP选项卡上的JButtons下面。我试图将textPanel添加到tabsPanel但是当我这样做时,我的标签会消失。我需要textPanel在屏幕上伸展并填充LLP选项卡下方的空白区域。但我也不希望textPanel出现在其他选项卡上。显示的第一张图片是旧程序的样子。我被要求从头开始做一个更好的计划。我无法在屏幕上显示输入输出日志。但正如您所看到的那样,它会显示我在“端口设置”选项中选择的内容。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.TextArea;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
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);
JLabel logLabel = new JLabel("Input/Output Log");
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");
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 apprvordecl = new JButton("Apprv/Decl");
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(apprvordecl);
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
}
}
答案 0 :(得分:3)
将Swing GUI合并在一起并不能很好地工作。我的建议是通过Oracle Swing Tutorial并学习一些在黑客攻击时可以组合在一起的概念。了解可供您使用的Swing组件,并了解Swing布局管理器。特别是了解Swing布局管理器。
这是我制作的GUI。
GUI比第一眼看起来更复杂。 JPanels里面有很多JPanels。我使用了一些FLowLayouts,一些BoxLayout和一个GridBagLayout。
以下是构成此GUI的主要JPanel的树形图。
Main JFrame
Main JPanel
Communication JPanel
First Button Row JPanel
Second Button Row JPanel
JTabbedPane
LLP Tab JPanel
Log JPanel
您应始终拥有一个主要的JPanel来保存所有其他组件。
这是Swing经验带来的一件事。您开始看到组成GUI各个部分的矩形。
我通常不会在一个类中编写这个复杂的GUI。我这次这样做了,所以我有一个SSCCE来发布这个解释。
研究代码,了解各种JPanel和Swing组件如何组合在一起。
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
public class RBMTestApplication implements Runnable {
private static final Insets SPACE_INSETS = new Insets(10, 10, 4, 10);
private JFrame frame;
private JPanel mainPanel;
private JPanel communicationPanel;
private JPanel cPanel1;
private JPanel cPanel2;
private JPanel llpPanel;
private JTabbedPane tabbedPane;
@Override
public void run() {
createFrame();
}
private void createFrame() {
frame = new JFrame();
frame.setTitle("RBM Test Application");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent event) {
exitProcedure();
}
});
createMainPanel();
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
}
public void exitProcedure() {
frame.dispose();
System.exit(0);
}
private void createMainPanel() {
mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
createCommunicationPanel();
mainPanel.add(communicationPanel);
createTabbedPane();
mainPanel.add(tabbedPane);
}
private void createCommunicationPanel() {
communicationPanel = new JPanel();
Border border = BorderFactory.createTitledBorder("Communication");
communicationPanel.setBorder(border);
communicationPanel.setLayout(new BoxLayout(communicationPanel,
BoxLayout.Y_AXIS));
cPanel1 = new JPanel();
JButton initializeButton = new JButton("Initialize");
cPanel1.add(initializeButton);
JButton connectButton = new JButton("Connect");
cPanel1.add(connectButton);
JButton disconnectButton = new JButton("Disconnect");
cPanel1.add(disconnectButton);
JButton shutdownButton = new JButton("Shutdown");
cPanel1.add(shutdownButton);
communicationPanel.add(cPanel1);
cPanel2 = new JPanel();
JButton portSettingsButton = new JButton("Port Settings");
portSettingsButton.setHorizontalAlignment(JButton.CENTER);
cPanel2.add(portSettingsButton);
communicationPanel.add(cPanel2);
}
private void createTabbedPane() {
tabbedPane = new JTabbedPane();
createLLPPanel();
tabbedPane.addTab("LLP", llpPanel);
// tabbedPane.addTab("POS", posPanel);
// tabbedPane.addTab("Tere", terePanel);
}
private void createLLPPanel() {
llpPanel = new JPanel();
llpPanel.setLayout(new GridBagLayout());
int gridy = 0;
JButton goOnlineButton = new JButton("Go online");
addComponent(llpPanel, goOnlineButton, 0, gridy, 1, 1, SPACE_INSETS,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JButton statusButton = new JButton("Status");
addComponent(llpPanel, statusButton, 1, gridy, 1, 1, SPACE_INSETS,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JButton pccountButton = new JButton("PC count");
addComponent(llpPanel, pccountButton, 2, gridy, 1, 1, SPACE_INSETS,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JButton resetButton = new JButton("Reset");
addComponent(llpPanel, resetButton, 3, gridy++, 1, 1, SPACE_INSETS,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JButton goOfflineButton = new JButton("Go offline");
addComponent(llpPanel, goOfflineButton, 0, gridy, 1, 1, SPACE_INSETS,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JButton rebootButton = new JButton("Reboot");
addComponent(llpPanel, rebootButton, 1, gridy, 1, 1, SPACE_INSETS,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JButton amountButton = new JButton("Amount");
addComponent(llpPanel, amountButton, 2, gridy, 1, 1, SPACE_INSETS,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JButton approveButton = new JButton("Approve / Decline");
addComponent(llpPanel, approveButton, 3, gridy++, 1, 1, SPACE_INSETS,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JPanel logPanel = new JPanel();
Border border = BorderFactory.createTitledBorder("Input / output log");
logPanel.setBorder(border);
JTextPane textPane = new JTextPane();
textPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.getViewport().setPreferredSize(new Dimension(400, 150));
logPanel.add(scrollPane);
addComponent(llpPanel, logPanel, 0, gridy++, 4, 1, SPACE_INSETS,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new RBMTestApplication());
}
}
您应该始终通过调用SwingUtilities invokeLater方法启动Swing应用程序。这可确保在Event Dispatch线程上创建和更新Swing组件。
答案 1 :(得分:0)
查看http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
您必须根据需要设置合适的LayoutManager,否则使用可能不适合的默认LayoutManager。
答案 2 :(得分:0)
你需要另一个嵌套面板,试试这个:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
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);
JLabel logLabel = new JLabel("Input/Output Log");
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");
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 apprvordecl = new JButton("Apprv/Decl");
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);
//CHANGED PART
JPanel llpPanel = new JPanel(new BorderLayout());
JPanel llpbuttonPanel = new JPanel();
llpbuttonPanel.add(online);
llpbuttonPanel.add(offline);
llpbuttonPanel.add(status);
llpbuttonPanel.add(reboot);
llpbuttonPanel.add(account);
llpbuttonPanel.add(amount);
llpbuttonPanel.add(reset);
llpbuttonPanel.add(apprvordecl);
llpPanel.add(llpbuttonPanel, BorderLayout.NORTH);
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel);
llpPanel.add(textPanel, BorderLayout.CENTER);
//END CHANGED PART
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
}
}
放入正确的文本区域:(您将要以不同的方式创建和引用文本区域)
//BEGIN CHANGE
JPanel llpPanel = new JPanel(new BorderLayout());
JPanel llpbuttonPanel = new JPanel();
llpbuttonPanel.add(online);
llpbuttonPanel.add(offline);
llpbuttonPanel.add(status);
llpbuttonPanel.add(reboot);
llpbuttonPanel.add(account);
llpbuttonPanel.add(amount);
llpbuttonPanel.add(reset);
llpbuttonPanel.add(apprvordecl);
llpPanel.add(llpbuttonPanel, BorderLayout.NORTH);
JPanel textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel, BorderLayout.NORTH);
JTextArea area = new JTextArea();
textPanel.add(area, BorderLayout.CENTER);
llpPanel.add(textPanel, BorderLayout.CENTER);
//END CHANGE