这就是我目前所拥有的,我将添加更多内容,但这是我所谓的框架。我遇到的困难是决定我应该使用什么布局,如果我应该自己制作,我将如何去做?
关于我的创作的一些信息将很有用:我将在 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();
}
}
最佳答案
先看看the Visual Guide to Layout Managers .然后,您会注意到,只需一点点创意,您就可以只使用一个布局管理器或将多个布局管理器组合用于所有组件,从而实现几乎任何所需的布局。
根据您的描述,您可以在每个象限中使用带有 JPanel
的 BorderLayout
。
- 在南象限,可以使用右对齐的
FlowLayout
。 - 在 WEST 象限中,
GridBagLayout
可用于创建两列,一列用于标记(我假设它是一个JLabel
),一列用于输入空间(JTextField
)。 - 在CENTER象限中,文本区域可用于显示输出(
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();
}
}
关于java - 我应该如何插入布局以及什么布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24055699/