使用JavaSwing进行布局
我正试图找出哪一个是最好的布局选择。我有一个使用JavaSwing进行布局,java,swing,layout,Java,Swing,Layout,我正试图找出哪一个是最好的布局选择。我有一个网格,它是通过扩展JPanel创建的: public class TestPane extends JPanel{ // code } 我需要 用于显示某些文本/数字的标签 供用户选择选项的下拉列表 用于用户输入某些参数的文本字段 编辑 这是一个粗略的数字。对不起,没有这么整洁 关于使用哪种布局的任何建议 请注意,标签和网格将随着算法的计算而不断更新 编辑-网格组件很大-所以目前我认为它需要在左边或右边-其余的组件可以在它旁边的不同行中
网格
,它是通过扩展JPanel
创建的:
public class TestPane extends JPanel{
// code
}
我需要
- 用于显示某些文本/数字的标签
- 供用户选择选项的下拉列表李>
- 用于用户输入某些参数的文本字段
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);
}
}
我也在想,用什么样的方式才能让它看起来好看。我应该将网格与标签和文本框分开还是放在一起?从设计的角度来看,建议使用哪一种方法?使用GridBagLayout来组织按钮、文本字段和组合框等作业对象使用GridBagLayout来组织按钮、文本字段和组合框等作业对象如果使用GridBagLayout来完成整个过程,您会非常困惑。我倾向于创作版面。以下是您的布局在我看来的样子:
如果您使用GridBagLayout来完成这一切,您会非常困惑。我倾向于创作版面。以下是您的布局在我看来的样子:
花一点时间,把你的用户界面分解成不同的职责区域 在您建议的布局中,您有三个主要区域,例如 您还有一个子组,例如 在我看来,这带来了至少两个或三个布局管理器的可能性,这取决于您的需要。这通常称为复合布局 从“网格”区域开始 主网格可以是
GridBagLayout
或GridBagLayout
,具体取决于您的需要。这将创建主网格面板
您可以将其添加到另一个JPanel
,该面板将使用BorderLayout
并将其添加到中心位置
在此面板中,您可以将“当前状态”组件添加到SOUTH
位置。这将形成GridStatePanel
然后,您将创建另一个JPanel
,并可能使用GridBagLayout
将字段(黄色部分)添加到其中,这将形成FieldsPanel
然后,您将创建另一个JPanel
,并可能使用BorderLayout
,将FieldsPanel
添加到WEST
位置,将gridstatempanel
添加到中间。这将构成您的基本面板或主面板…我将其称为MainContentPane
…这样我们就知道我们在谈论什么了
从那里,您可以将其来宾化,创建另一个JPanel
。在NORTH
位置,添加“Title”组件,然后将MainContentPane
添加到中间
这是一个非常强大和重要的概念,很难找到一个能够一次性布局整个布局的布局管理器。花一点时间,将您的UI分解为责任区域
在您建议的布局中,您有三个主要区域,例如
您还有一个子组,例如
在我看来,这带来了至少两个或三个布局管理器的可能性,这取决于您的需要。这通常称为复合布局
从“网格”区域开始
主网格可以是GridBagLayout
或GridBagLayout
,具体取决于您的需要。这将创建主网格面板
您可以将其添加到另一个JPanel
,该面板将使用BorderLayout
并将其添加到中心位置
在此面板中,您可以将“当前状态”组件添加到SOUTH
位置。这就形成了GridStateP