
我已经附上了我的代码下面,我的程序的屏幕截图,以及我想要的标签看起来像什么绘图。我需要我的JPanel textPanel出现在标签LLP上的JButtons下面。我曾尝试将textPanel添加到tabsPanel中,但当我这样做时,我的标签就消失了。我需要textPanel在屏幕上伸展并填充LLP选项卡下面的空白处。但我也不希望textPanel出现在其他选项卡上。显示的第一张图片是旧程序的样子。我被要求从头开始做一个更好的程序。我无法在我的屏幕上获得输入输出日志。但正如您所看到的,它显示了我在Port Settings选项中选择的内容。


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
}
}发布于 2013-05-23 02:56:16
你需要另一个嵌套的面板,试试这个:
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发布于 2013-05-23 03:39:41
将Swing GUI组合在一起并不能很好地工作。我的建议是通过Oracle Swing Tutorial学习一些你可以在黑客攻击时放在一起的概念。了解可供您使用的Swing组件,并了解Swing布局管理器。尤其是对Swing布局管理器的了解。
这是我生成的GUI。

GUI比乍一看要复杂得多。JPanels内部有很多JPanels。我用了几个FLowLayouts,几个BoxLayouts和一个GridBagLayout。
下面是组成这个图形用户界面的主要JPanels的树形图。
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可以与这个解释一起发布。
研究代码,了解各种JPanels和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组件。
发布于 2013-05-23 02:55:47
看一看http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
您必须根据需要设置合适的LayoutManager,否则将使用可能不适合的默认LayoutManager。
https://stackoverflow.com/questions/16699061
复制相似问题