问题描述
我正在尝试找出哪种是最好的布局.我有一个 grid
,它是通过将 JPanel
扩展为以下内容而创建的:
I am trying to figure out which is the best layout to choose. I have a grid
which is created by extending JPanel
as:
public class TestPane extends JPanel{
// code
}
我需要
- 标签以显示一些文本/数字.
- 一个供用户选择选项的下拉列表.
- 供用户输入某些参数的文本字段.
修改
这是该图的粗略草稿.抱歉没那么整洁.
This is a rough draft of the figure. Sorry for not being so neat.
有关使用哪种布局的任何建议.
Any suggestions as to which layout to use.
请注意,标签和 grid
会随着算法的计算不断更新.
Note that the labels and the grid
keep updating with computation of the algorithm.
编辑- grid
组件很大-所以目前我在想它必须在左边或右边-其余组件可以在它旁边的不同行中.
Edit- the grid
component is large - so currently I was thinking that it needs to be either to the left or right - the remaining components can be in different lines beside it.
我开始将GridBagLayout与遇到的一些示例代码一起使用,但是不清楚如何向其中添加网格:
I started using GridBagLayout with some sample code I came across but I am not clear as to how to add the grid to it:
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);
}
}
我也在考虑哪种方式可以使外观看起来更好.我应该将网格与标签和文本框分开放置还是一起放置?从设计的角度来看,哪个是推荐的?
I am also thinking as to which manner would make this look good. Should I have the grid separately from the labels and text boxes or together? Which is recommended from a design point of view?
推荐答案
花点时间,将您的UI划分为责任区域...
Take a second and break down your UI into areas of responsibility...
在您建议的布局中,您有三个主要区域,例如...
In you proposed layout, you have three main areas, for example...
您还有一个子组,例如...
You also have a sub group, for example...
在我看来,这带来了至少两个或三个版面管理器的可能性,具体取决于您的需求.这通常称为复合布局.
To my mind, this brings up the possibility of at least two or three layouts managers depending on your needs. This is commonly known as compound layouts.
从网格"区域开始.
根据您的需要,主网格可以是 GridBagLayout
或 GridLayout
.这将创建主 GridPanel
.
The main grid would either be a GridBagLayout
or a GridLayout
depending on your needs. This would create the main GridPanel
.
您将其添加到另一个使用 BorderLayout
的 JPanel
中,并将其添加到中心位置.
You would add this to another JPanel
which would be using a BorderLayout
and add it to the center position.
在此面板中,您将当前状态"组件添加到 SOUTH
位置.这形成了 GridStatePanel
.
To this panel you would add the "Current state" component to the SOUTH
position. This forms the GridStatePanel
.
然后您将创建另一个 JPanel
,并可能使用 GridBagLayout
向其中添加字段(黄色部分),这将形成 FieldsPanel
.
You would then create another JPanel
and probably using a GridBagLayout
add your fields (the yellow section) to it, this would form the FieldsPanel
.
然后您将创建另一个 JPanel
,并可能使用 BorderLayout
,将 FieldsPanel
添加到 WEST
位置和 GridStatePanel
居中.这将形成您的基本面板或主要面板...我将其称为 MainContentPane
...,以便我们知道我们在说什么.
You would then create another JPanel
and probably using a BorderLayout
, add the FieldsPanel
to the WEST
position and the GridStatePanel
to the center. This would form your base or primary panel...I'll call this MainContentPane
...so we know what we're talking about.
从那里开始为它做客,创建另一个 JPanel
.在 NORTH
位置,添加标题"组件,然后将 MainContentPane
添加到中心.
From there, you guest it, create another JPanel
. To the NORTH
position, add the "Title" component and then add the MainContentPane
to the center.
这是一个非常强大且重要的概念,要找到一个能够一次完成整个布局的布局管理器将非常困难.
This is a very powerful and important concept, it would be very difficult to find a single layout manager that would be capable to laying out the entire layout in a single go.
这篇关于使用Java Swing进行布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!