gpt4 book ai didi

java - 让 JPanel 变得更大

转载 作者:太空宇宙 更新时间:2023-11-04 07:37:10 24 4
gpt4 key购买 nike

我正在努力让我的 JPanel 更大。我有三个选项卡,但当我运行程序时,并非所有选项卡都显示(除非我单击箭头)。我只需要知道如何使面板更大,这样我就不必单击箭头

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;


public class TestApplication {
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);

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");


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);

frame.pack();

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, null, "Low Level Protocol");
tabbedPane.addTab("POS",null, null, "Point Of Sale");
tabbedPane.addTab("Test", null, null, "Test");

JPanel tabsPanel = new JPanel();
tabsPanel.add(tabbedPane);
frame.add(tabsPanel);
tabsPanel.setPreferredSize(new Dimension(900, 900));
frame.add(tabsPanel, BorderLayout.CENTER);

frame.pack();



}






}

最佳答案

给你的 tabsPanel 一个合适的布局,即 BorderLayout:

  JPanel tabsPanel = new JPanel(new BorderLayout()); //!!

请注意,您不应将组件两次添加到 GUI:

  frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);

并且无需多次调用 pack()。
另外,您不应该调用 setSize(...) ,而最好重写关键组件的 getPreferredSize()

关于java - 让 JPanel 变得更大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16677158/

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