Java Swing 远离空布局
Java Swing Moving Away From Null Layout
我使用不满意的空布局构建了一个很棒的 GUI(我定义了很多常量并使用了 window 调整大小侦听器以使其变得简单)。在我开始使用新电脑之前,一切都运行良好。现在,组件的位置不正确(从图片中您可以看到组件向下和向右偏移)。在研究了这个问题之后,我了解到布局管理器确保组件在不同机器上的位置正确。因此,我想开始在实际的布局管理器中重建 GUI。问题是,当我尝试使用实际的布局管理器时,我常常觉得我定位组件的方式受到限制。
对于任何好奇的人,我最初使用的是 windows 10 的戴尔 inspiron 笔记本电脑,并已转移到华硕笔记本电脑(我不知道实际型号,但触摸屏可以分离从键盘),还有 windows 10.
我的问题:
哪个布局管理器构建上图所示的 GUI 最快且最容易(Swing 布局 和 其他)。我希望此布局仅针对少数而非所有组件尊重组件的实际尺寸。使用此布局,我将如何定位库存按钮(左下角的锤子),使库存按钮的左下角距离容器左下角右上角 5 像素,即使在调整大小后也是如此容器?
提前致谢。感谢所有帮助。
编辑:"go find a key" 和 "Attempt to force the door open" 选项应考虑其大小。
我想到的最简单的解决方案是 BorderLayout
for the main panel. Add the textarea
to NORTH
/ PAGE_START
. Make another BorderLayout
containing the inventory button (WEST
/ LINE_START
) and the location label (EAST
/ LINE_END
). Add that to SOUTH
/ PAGE_END
of the main BorderLayout
. Then just add a BoxLayout
with vertical alignment to the main BorderLayout
's CENTER
containing the two buttons. Here's a tutorial 标准布局管理器。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Example {
public Example() {
JTextArea textArea = new JTextArea("There is a locked door");
textArea.setRows(5);
textArea.setBorder(BorderFactory.createLineBorder(Color.GRAY));
textArea.setEditable(false);
WhiteButton button1 = new WhiteButton("Go find a key") {
@Override
public Dimension getMinimumSize() {
return new Dimension(200, 25);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 25);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(200, 25);
}
};
WhiteButton button2 = new WhiteButton("Attempt to force the door open");
button2.setMargin(new Insets(0, 60, 0, 60));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
buttonPanel.add(button1);
buttonPanel.add(Box.createVerticalStrut(5));
buttonPanel.add(button2);
WhiteButton inventoryButton = new WhiteButton(
new ImageIcon(new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB)));
JLabel locationLabel = new JLabel("Location: 0");
locationLabel.setVerticalAlignment(JLabel.BOTTOM);
JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(inventoryButton, BorderLayout.WEST);
southPanel.add(locationLabel, BorderLayout.EAST);
JPanel mainPanel = new JPanel(new BorderLayout(0, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.add(textArea, BorderLayout.NORTH);
mainPanel.add(buttonPanel);
mainPanel.add(southPanel, BorderLayout.SOUTH);
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
private class WhiteButton extends JButton {
public WhiteButton() {
setBackground(Color.WHITE);
}
public WhiteButton(String text) {
this();
setText(text);
}
public WhiteButton(ImageIcon icon) {
this();
setIcon(icon);
setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
}
}
我使用不满意的空布局构建了一个很棒的 GUI(我定义了很多常量并使用了 window 调整大小侦听器以使其变得简单)。在我开始使用新电脑之前,一切都运行良好。现在,组件的位置不正确(从图片中您可以看到组件向下和向右偏移)。在研究了这个问题之后,我了解到布局管理器确保组件在不同机器上的位置正确。因此,我想开始在实际的布局管理器中重建 GUI。问题是,当我尝试使用实际的布局管理器时,我常常觉得我定位组件的方式受到限制。
对于任何好奇的人,我最初使用的是 windows 10 的戴尔 inspiron 笔记本电脑,并已转移到华硕笔记本电脑(我不知道实际型号,但触摸屏可以分离从键盘),还有 windows 10.
我的问题:
哪个布局管理器构建上图所示的 GUI 最快且最容易(Swing 布局 和 其他)。我希望此布局仅针对少数而非所有组件尊重组件的实际尺寸。使用此布局,我将如何定位库存按钮(左下角的锤子),使库存按钮的左下角距离容器左下角右上角 5 像素,即使在调整大小后也是如此容器?
提前致谢。感谢所有帮助。
编辑:"go find a key" 和 "Attempt to force the door open" 选项应考虑其大小。
我想到的最简单的解决方案是 BorderLayout
for the main panel. Add the textarea
to NORTH
/ PAGE_START
. Make another BorderLayout
containing the inventory button (WEST
/ LINE_START
) and the location label (EAST
/ LINE_END
). Add that to SOUTH
/ PAGE_END
of the main BorderLayout
. Then just add a BoxLayout
with vertical alignment to the main BorderLayout
's CENTER
containing the two buttons. Here's a tutorial 标准布局管理器。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Example {
public Example() {
JTextArea textArea = new JTextArea("There is a locked door");
textArea.setRows(5);
textArea.setBorder(BorderFactory.createLineBorder(Color.GRAY));
textArea.setEditable(false);
WhiteButton button1 = new WhiteButton("Go find a key") {
@Override
public Dimension getMinimumSize() {
return new Dimension(200, 25);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 25);
}
@Override
public Dimension getMaximumSize() {
return new Dimension(200, 25);
}
};
WhiteButton button2 = new WhiteButton("Attempt to force the door open");
button2.setMargin(new Insets(0, 60, 0, 60));
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
buttonPanel.add(button1);
buttonPanel.add(Box.createVerticalStrut(5));
buttonPanel.add(button2);
WhiteButton inventoryButton = new WhiteButton(
new ImageIcon(new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB)));
JLabel locationLabel = new JLabel("Location: 0");
locationLabel.setVerticalAlignment(JLabel.BOTTOM);
JPanel southPanel = new JPanel(new BorderLayout());
southPanel.add(inventoryButton, BorderLayout.WEST);
southPanel.add(locationLabel, BorderLayout.EAST);
JPanel mainPanel = new JPanel(new BorderLayout(0, 5));
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.add(textArea, BorderLayout.NORTH);
mainPanel.add(buttonPanel);
mainPanel.add(southPanel, BorderLayout.SOUTH);
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
private class WhiteButton extends JButton {
public WhiteButton() {
setBackground(Color.WHITE);
}
public WhiteButton(String text) {
this();
setText(text);
}
public WhiteButton(ImageIcon icon) {
this();
setIcon(icon);
setBorder(BorderFactory.createLineBorder(Color.GRAY));
}
}
}