[英]How should I go about inserting a layout and what layout?
因此,这就是到目前为止的内容,我将添加更多内容,但这就是我所谓的骨骼。 我遇到的麻烦是决定应该使用哪种布局,如果我应该自己制作,我将如何进行呢?
有关我的创作的一些信息将非常有用:我将在GUI的最左侧区域中具有多个标签和输入空间,在它们的右侧,在底部的右下方将有一个按钮,在顶部的右上方将是一个文本框,将不断流式传输机器人正在执行的信息。
请帮助我找到我可以使用的布局,并告诉我如何轻松实现它(仅供参考,我正在使用Ubuntu)。
import java.awt.*;
import javax.swing.*;
public class dogedice {
public static void createWindow() {
JFrame frame = new JFrame("Framework Michael+Dalin");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel textLabel = new JLabel("Welcome to the framework of our future bots!");
JLabel textLabel2 = new JLabel("Username");
JTextField textField = new JTextField("Username");
JPanel panel = new JPanel();// Any new parts must be added here!
panel.add(textLabel);
panel.add(textField);
panel.add(textLabel2);
textLabel.setPreferredSize(new Dimension(800, 750));
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createWindow();
}
}
首先看一下《布局管理器视觉指南》 。 然后,您会发现,只要稍加创造力,您就可以只使用一个布局管理器,或者为所有组件组合多个布局管理器,就可以实现几乎任何所需的布局。
根据您的描述,可以在每个象限中使用带有JPanel
的BorderLayout
。
FlowLayout
与右对齐一起使用。 GridBagLayout
创建两列,一列用于标签(我假设是JLabel
),一列用于输入空间( JTextField
)。 JTextArea
封装在JScrollPanel
) 这是一个例子:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class LayoutExample1 extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LayoutExample1 frame = new LayoutExample1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LayoutExample1() {
setTitle("Title of GUI");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.WEST);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{0.0, 1.0};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
JLabel lblTag = new JLabel("Tag 1");
GridBagConstraints gbc_lblTag = new GridBagConstraints();
gbc_lblTag.insets = new Insets(0, 0, 0, 5);
gbc_lblTag.anchor = GridBagConstraints.EAST;
gbc_lblTag.gridx = 0;
gbc_lblTag.gridy = 0;
panel.add(lblTag, gbc_lblTag);
textField = new JTextField();
GridBagConstraints gbc_textField = new GridBagConstraints();
gbc_textField.fill = GridBagConstraints.HORIZONTAL;
gbc_textField.gridx = 1;
gbc_textField.gridy = 0;
panel.add(textField, gbc_textField);
textField.setColumns(10);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
JButton btnConfirm = new JButton("Confirm");
panel_1.add(btnConfirm);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new JTextArea("I am a bot and I will tell you what I am doing to your systems!");
textArea.setColumns(20);
scrollPane.setViewportView(textArea);
pack();
}
}
如果UI如此简单,则将所有标签和输入放在带有GridLayout的JPanel中,然后将其放在带有BorderLayout的mainPanel的中央。
如果您对更复杂的布局感兴趣并想学习如何使用它,我建议http://miglayout.com/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.