java jpanel grouplayout格式化问题
我正在尝试布局我的jpanel,以使:
- 信息标签位于顶部,
- 区域直接下面
- 文本 从左到右(均等)是配置,保存,然后是清除按钮,
- 下面是成功/故障标签(中央,保存按钮下方)和主页按钮(右,在“清除”按钮下)。
我尝试了许多组合,并使用了其他堆栈答案,但无法正确解决,这是关于团体布局的,我无法抓住我的头!
import javax.swing.*;
public class JFrameTest {
private static JFrame mainApp;
private static JPanel mainPanel;
public JFrameTest() {
mainApp = new JFrame("Application");
mainApp.setSize(640, 480);
mainApp.setLocationRelativeTo(null);
mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainApp.add(mainPanel());
mainApp.setVisible(true);
}
private JPanel mainPanel() {
JFrame.setDefaultLookAndFeelDecorated(true);
mainPanel= new JPanel();
mainPanel.setSize(600,450);
Container container = mainApp.getContentPane();
JLabel labelInfo = new JLabel("add necessary information here");
JLabel labelSOrF = new JLabel("Success/Failure");
// labelSOrF.setVisible(false);
JTextArea textArea = new JTextArea();
JButton configButton= new JButton("config");
JButton saveButton= new JButton("save");
JButton clearButton= new JButton("clear");
JButton homeButton= new JButton("Home");
GroupLayout layout = new GroupLayout(container);
container.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(configButton)
.addGroup(layout.createParallelGroup()
.addComponent(saveButton)
.addComponent(labelSOrF, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup()
.addComponent(clearButton)
.addComponent(homeButton)
.addComponent(labelInfo)
.addComponent(textArea, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
)))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(labelInfo)
.addComponent(textArea, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(configButton)
.addComponent(saveButton)
.addComponent(buttonClear))
.addGroup(layout.createParallelGroup()
.addComponent(labelSOrF, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(homeButton)))
);
编辑 - 添加代码
I am trying to layout my JPanel such that:
- The information label is at the top,
- The text area is directly below, this and the info label go across (horizontally) the entire panel,
- Below the text from left to right (equally spaced) are the config, save and then clear buttons,
- Below those is the success/failure label (central, under the save button) and the home button (right, under the clear button).
I have tried many combinations and used other stack answers but cannot get it right, something about group layout I can't get my head around!
import javax.swing.*;
public class JFrameTest {
private static JFrame mainApp;
private static JPanel mainPanel;
public JFrameTest() {
mainApp = new JFrame("Application");
mainApp.setSize(640, 480);
mainApp.setLocationRelativeTo(null);
mainApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainApp.add(mainPanel());
mainApp.setVisible(true);
}
private JPanel mainPanel() {
JFrame.setDefaultLookAndFeelDecorated(true);
mainPanel= new JPanel();
mainPanel.setSize(600,450);
Container container = mainApp.getContentPane();
JLabel labelInfo = new JLabel("add necessary information here");
JLabel labelSOrF = new JLabel("Success/Failure");
// labelSOrF.setVisible(false);
JTextArea textArea = new JTextArea();
JButton configButton= new JButton("config");
JButton saveButton= new JButton("save");
JButton clearButton= new JButton("clear");
JButton homeButton= new JButton("Home");
GroupLayout layout = new GroupLayout(container);
container.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(configButton)
.addGroup(layout.createParallelGroup()
.addComponent(saveButton)
.addComponent(labelSOrF, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup()
.addComponent(clearButton)
.addComponent(homeButton)
.addComponent(labelInfo)
.addComponent(textArea, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
)))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(labelInfo)
.addComponent(textArea, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(configButton)
.addComponent(saveButton)
.addComponent(buttonClear))
.addGroup(layout.createParallelGroup()
.addComponent(labelSOrF, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(homeButton)))
);
edit - added code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Oracle有一个有用的教程,创建带有swing 的GUI。使用Netbeans IDE部分跳过学习秋千。
Grouplayout是为GUI建造者设计的。没有人会故意使用杂种。它太难理解,并且您发现,维护。
这是满足您要求的GUI。
秋千设计为从内而外构建。您将摆动组件布局,然后让
jpanels
和jframe
大小本身。您不会从Jframe开始,并适合所有秋千组件。所有秋千应用程序必须启动,呼叫
swingutilities
InvokeLater
方法。 This method ensures that the Swing components are created and executed on the Event Dispatch Thread 。我用三个下属
JPanels
创建了一个主JPANEL
。要构建复杂的GUI,您可以嵌套简单JPANELS
。我在列,行顺序中将秋千组件添加到每个
jpanel
中。这有助于我组织代码,并使代码的读者更容易理解代码。这是完整的可运行代码。
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.
GroupLayout was designed for GUI builders. No human being will deliberately use a GroupLayout. It's too hard to understand and as you've discovered, maintain.
Here's a GUI that meets your requirements.
Swing was designed to be constructed from the inside out. You layout the Swing components and let the
JPanels
and theJFrame
size themselves. You don't start with the JFrame and fit all the Swing components.All Swing applications must start with a call to the
SwingUtilities
invokeLater
method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.I created a main
JPanel
with three subordinateJPanels
. To construct a complex GUI, you nest simpleJPanels
.I added the Swing components to each
JPanel
in column, row order. That helps me to organize the code and makes it easier for readers of your code to understand the code.Here's the complete runnable code.