[英]Java Swing Layouts and Menus
Ok so I am having trouble using the grid layout to arrange the window in this format, I want to arrange each label and its text field to an individual line and then have a centered button at the bottom. 好的,所以我在使用网格布局以这种格式排列窗口时遇到麻烦,我想将每个标签及其文本字段排列到单独的一行,然后在底部有一个居中的按钮。
public class Main extends JFrame {
//below is how i want to format the frame
/*label textfield
label textfield
label textfield
button*/
JPanel panel = new JPanel();
JLabel nameLabel = new JLabel("Name");
JTextField nameText = new JTextField(15);
JLabel addressLabel = new JLabel("Address");
JTextField addressText = new JTextField(15);
public Main(){
setTitle("JSWING");
setLayout(new GridLayout(3,2));
setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
setVisible(true);
setSize(450,250);
//setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel.add(nameLabel);
panel.add(nameText);
panel.add(addressLabel);
panel.add(addressText);
add(panel);
}
public static void main(String[] args){
Main mainFrame = new Main();
//mainFrame.setVisible(true);
}
}
//setVisible(true); // don't do this until all components are added to the frame.
...
add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton button = new JButton("Button");
buttonPanel.add( button );
add(buttonPanel, BorderLayout.PAGE_END);
setVisible(true);
The default layout manager for a frame is a BorderLayout. 框架的默认布局管理器是BorderLayout。 So you can add multiple panels to the frame. 因此,您可以在框架中添加多个面板。 One panel containing your labels/text fields and the other containing your button. 一个面板包含您的标签/文本字段,另一个面板包含您的按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.