gpt4 book ai didi

java - 将 JTextArea 添加到 TabbedPane

转载 作者:行者123 更新时间:2023-12-01 23:46:45 28 4
gpt4 key购买 nike

我试图让我的 JTextArea 显示在 llpPanel 的所有其他内容下。我的代码如下,其中包含我的代码显示内容的屏幕截图。在代码中,您将看到我已将 JTextArea 的尺寸设置为 (50, 50)。然后在 llpPanel 中我添加了 BorderLayout.PAGE_END。我还尝试过(而不是 PAGE_END)放置 CENTER 和 SOUTH。当我输入 SOUTH 时,它在程序的最底部显示一条白线,但你不能用它做任何事情。

enter image description here

    import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class TestApplication implements ActionListener {

public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
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 approvordecl = new JButton("Approve / Decline");

JTextArea logbox = new JTextArea(50, 50);

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);
llpPanel.add(logbox, BorderLayout.PAGE_END);

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
}
}

更新后的代码和屏幕截图如下...

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class TestApplication implements ActionListener {

public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);



JTextArea logbox = new JTextArea(50, 50);




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 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 newButtonPanel = new JPanel();
newButtonPanel.add(online);
newButtonPanel.add(offline);
newButtonPanel.add(status);
newButtonPanel.add(reboot);
newButtonPanel.add(account);
newButtonPanel.add(amount);
newButtonPanel.add(reset);
newButtonPanel.add(approvordecl);


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.setLayout(new BorderLayout());
llpPanel.add(newButtonPanel);
llpPanel.add(logbox, BorderLayout.PAGE_END);

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

}






}

screenshot2

最佳答案

JPanels 默认情况下使用 FlowLayout,因此应用 BorderLayout 约束(例如 PAGE_END)将不起作用。您需要设置面板​​的布局:

llpPanel.setLayout(new BorderLayout());

然后你会遇到组件在BorderLayout.CENTER位置移动的问题。解决方案是创建另一个 JPanel 作为 llpPanel 上除 logbox 之外的组件的容器。

JPanel newButtonPanel = new JPanel();
newButtonPanel.add(online);
...
llpPanel.add(newButtonPanel);
JScrollPane scrollPane = new JScrollPane(logbox) {
@Override
public java.awt.Dimension getPreferredSize() {
return new Dimension(500, 500);
};
};
llpPanel.add(scrollPane, BorderLayout.PAGE_END);

使用 JScrollPane 而不是直接将 JTextArea 添加到容器中。

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

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