用户窗口界面将三个信息用GridBagLayout方法纵向排列java代码
时间: 2024-05-04 19:16:29 浏览: 80
非常感谢您的提问。下面是用GridBagLayout方法纵向排列三个信息的java代码:
```
import java.awt.*;
import javax.swing.*;
public class UserInterface extends JFrame {
private JLabel label1, label2, label3;
private JTextField textField1, textField2, textField3;
private GridBagConstraints constraints;
public UserInterface() {
label1 = new JLabel("信息1:");
label2 = new JLabel("信息2:");
label3 = new JLabel("信息3:");
textField1 = new JTextField(10);
textField2 = new JTextField(10);
textField3 = new JTextField(10);
setLayout(new GridBagLayout());
constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.WEST;
add(label1, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
add(label2, constraints);
constraints.gridx = 0;
constraints.gridy = 2;
add(label3, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.HORIZONTAL;
add(textField1, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
add(textField2, constraints);
constraints.gridx = 1;
constraints.gridy = 2;
add(textField3, constraints);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
UserInterface userInterface = new UserInterface();
userInterface.setVisible(true);
}
}
```
如果您有任何其他问题,请随时提出。感谢您对ChitGPT的使用!
阅读全文