[英]Layout using Java Swing
我试图找出哪种是最好的布局。 我有一个通过将JPanel
扩展为以下对象而创建的grid
:
public class TestPane extends JPanel{
// code
}
我需要
编辑
这是该图的草稿。 抱歉,没有这么整洁。
关于使用哪种布局的任何建议。
请注意,标签和grid
随着算法的计算而不断更新。
编辑- grid
组件很大-所以目前我在想它必须在左边或右边-其余组件可以在它旁边的不同行中。
我开始将GridBagLayout与遇到的一些示例代码一起使用,但是不清楚如何向其中添加网格:
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
public class GridBagLayoutExample {
JFrame guiFrame;
public static void main(String[] args) {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new GridBagLayoutExample();
}
});
}
public GridBagLayoutExample()
{
guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame.setTitle("GridBagLayout Example");
guiFrame.setSize(600,300);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
//creating a border to highlight the component areas
Border outline = BorderFactory.createLineBorder(Color.black);
//create GribBagLayout and the GridBagLayout Constraints
GridBagLayout gridBag = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.BOTH;
JPanel compPanel = new JPanel();
compPanel.setLayout(gridBag);
cons.gridx = 2;
cons.gridy = 2;
JLabel randomLbl = new JLabel("In Xanadu did Kubla Khan, "
+ "A stately pleasure-dome decree");
randomLbl.setBorder(outline);
gridBag.setConstraints(randomLbl, cons);
compPanel.add(randomLbl);
cons.ipady = 100;
cons.ipadx = 100;
cons.weighty = 1.0;
cons.gridx = 0;
cons.gridy = 0;
JLabel tallLbl = new JLabel("Tall and Long");
tallLbl.setBorder(outline);
gridBag.setConstraints(tallLbl, cons);
compPanel.add(tallLbl);
cons.ipady = 50;
cons.ipadx = 100;
cons.weightx = 0;
cons.gridx = 0;
cons.gridy = 1;
JButton hello = new JButton("Hello");
gridBag.setConstraints(hello, cons);
compPanel.add(hello);
cons.ipady = 100;
cons.ipadx = 10;
cons.gridx = 1;
cons.gridy = 1;
JButton goodbye = new JButton("GoodBye");
gridBag.setConstraints(goodbye, cons);
compPanel.add(goodbye);
cons.weightx = 0;
cons.gridx = 0;
cons.gridy = 2;
JButton eh = new JButton("eh?");
gridBag.setConstraints(eh, cons);
compPanel.add(eh);
guiFrame.add(compPanel);
guiFrame.setVisible(true);
}
}
我也在考虑哪种方式可以使外观看起来更好。 我应该将网格与标签和文本框分开放置还是一起放置? 从设计的角度来看,哪个是推荐的?
花一点时间,将您的UI分解为职责范围...
在您建议的布局中,您有三个主要区域,例如...
您也有一个子组,例如...
在我看来,这带来了根据您的需要至少两个或三个布局管理器的可能性。 这通常称为复合布局。
从“网格”区域开始。
根据您的需要,主网格可以是GridBagLayout
或GridLayout
。 这将创建主GridPanel
。
您可以将其添加到另一个将使用BorderLayout
JPanel
,并将其添加到中心位置。
在此面板中,您将“ Current state”组件添加到SOUTH
位置。 这形成GridStatePanel
。
然后,您将创建另一个JPanel
并可能使用GridBagLayout
将您的字段(黄色部分)添加到其中,这将形成FieldsPanel
。
然后,您将创建另一个JPanel
并可能使用BorderLayout
,将FieldsPanel
添加到WEST
位置,并将GridStatePanel
到中心。 这将形成您的基本面板或主要面板...我将其称为MainContentPane
...,以便我们知道我们在说什么。
从那里,为它做客,创建另一个JPanel
。 在NORTH
位置,添加“ Title”组件,然后将MainContentPane
添加到中心。
这是一个非常强大且重要的概念,很难找到一个能够一次布局整个布局的布局管理器。
如果您使用GridBagLayout来完成整个操作,您将会非常困惑。 我倾向于更喜欢排版。 这是您的布局在我看来:
您最外面的面板具有带有West和Center的BorderLayout。 (请记住,使用BorderLayout时,拉伸窗口时,中间的东西会扩展;而西方(或东方,北方,南方)的东西不会扩展)
在中心内,您有一个JPanel,它也具有BorderLayout。 在其北部是JLabel(“ Title”)。 桌子中间是桌子。 南部是JLabel(“当前状态”)
在西部,您有一个具有另一个BorderLayout的JPanel。 在北部是一个带有GridLayout(3,2)的JPanel,带有JLabels(Label1,Label2,Label3)和关联的文本字段。 在中心是您的下拉列表(也许?从图中不清楚)。 在南部是按钮。 (并且,您可能想限制按钮的大小,否则它将占用面板的整个宽度)
使用诸如GridBagLayout之类的布局管理器来组织JObject,例如按钮,文本字段和组合框
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.