gpt4 book ai didi

java - 使用 Java Swing 进行布局

转载 作者:行者123 更新时间:2023-11-29 07:50:05 27 4
gpt4 key购买 nike

我想弄清楚哪种布局是最好的选择。我有一个 grid,它是通过将 JPanel 扩展为创建的:

public class TestPane extends JPanel{
// code
}

我需要

  • 用于显示一些文本/数字的标签。
  • 供用户选择选项的下拉列表。
  • 供用户输入一些参数的文本字段。

编辑

Approximate layout

这是该图的草稿。抱歉没有那么整洁。

关于使用哪种布局的任何建议。

请注意,标签和网格会随着算法的计算不断更新。

编辑 - 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 分解为责任区域...

在你提议的布局中,你有三个主要区域,例如......

Main

您还有一个子组,例如...

Sub

在我看来,这带来了至少两个或三个布局管理器的可能性,具体取决于您的需要。这通常称为复合布局。

从“网格”区域开始。

根据您的需要,主网格可以是 GridBagLayoutGridLayout。这将创建主要的 GridPanel

您可以将其添加到另一个使用 BorderLayoutJPanel 并将其添加到中心位置。

对于此面板,您可以将“当前状态”组件添加到 SOUTH 位置。这形成了 GridStatePanel

然后您将创建另一个 JPanel 并可能使用 GridBagLayout 将您的字段(黄色部分)添加到其中,这将形成 FieldsPanel.

然后您将创建另一个 JPanel 并可能使用 BorderLayout,将 FieldsPanel 添加到 WEST 位置和 GridStatePanel 到中心。这将构成您的基础或主要面板...我将其称为 MainContentPane...所以我们知道我们在谈论什么。

从那里,您可以创建另一个 JPanel。在 NORTH 位置,添加“Title”组件,然后将 MainContentPane 添加到中心。

这是一个非常强大和重要的概念,很难找到一个能够一次性完成整个布局的布局管理器。

关于java - 使用 Java Swing 进行布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21817430/

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