gpt4 book ai didi

java - 将 JPanel 添加到 JPanel

转载 作者:行者123 更新时间:2023-12-04 04:56:27 30 4
gpt4 key购买 nike

old
我在下面附上了我的代码,我的程序的屏幕截图,以及我希望标签看起来像的图形。我需要我的 JPanel textPanel 出现在 LLP 选项卡上的 JButton 下方。我试图将 textPanel 添加到 tabsPanel,但是当我这样做时,我的标签消失了。我需要 textPanel 横跨屏幕并填充 LLP 选项卡下方的空白区域。但我也不希望 textPanel 出现在其他选项卡上。显示的第一张图片是旧程序的样子。我被要求从头开始制作一个更好的程序。我无法在我的屏幕上获得输入输出日志。但正如您所看到的,它显示了我在“端口设置”​​选项中选择的内容。 Screen Shot of Program Drawing

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

}






}

最佳答案

将 Swing GUI 组合在一起并不是很好。我的建议是通过 Oracle Swing Tutorial并学习一些在黑客攻击时可以组合在一起的概念。了解可供您使用的 Swing 组件,并了解 Swing 布局管理器。特别是了解 Swing 布局管理器。

这是我制作的 GUI。

RBM Test Application

GUI 比乍一看要复杂得多。 JPanel 内部有很多 JPanel。我使用了几个 FLowLayouts、几个 BoxLayouts 和一个 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 组件。

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

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