窗口界面将三个信息用GridBagLayout方法纵向排列java代码 点击复制后,将打开C知道体验页
时间: 2024-05-04 20:16:38 浏览: 75
Java中GridBagLayout的用法
5星 · 资源好评率100%
好的,以下是实现您提出需求的Java代码:
```
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ThreeInfoGridBagLayout extends JFrame {
private JPanel contentPane;
private JLabel lblInfo1;
private JLabel lblInfo2;
private JLabel lblInfo3;
private JTextField txtInfo1;
private JTextField txtInfo2;
private JTextField txtInfo3;
public ThreeInfoGridBagLayout() {
setTitle("Three Information with GridBagLayout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 200);
contentPane = new JPanel();
contentPane.setLayout(new GridBagLayout());
setContentPane(contentPane);
lblInfo1 = new JLabel("Information 1:");
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets.top = 10;
gbc.insets.left = 10;
contentPane.add(lblInfo1, gbc);
txtInfo1 = new JTextField();
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets.top = 5;
gbc.insets.left = 10;
gbc.insets.right = 10;
contentPane.add(txtInfo1, gbc);
lblInfo2 = new JLabel("Information 2:");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets.top = 10;
gbc.insets.left = 10;
contentPane.add(lblInfo2, gbc);
txtInfo2 = new JTextField();
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets.top = 5;
gbc.insets.left = 10;
gbc.insets.right = 10;
contentPane.add(txtInfo2, gbc);
lblInfo3 = new JLabel("Information 3:");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets.top = 10;
gbc.insets.left = 10;
contentPane.add(lblInfo3, gbc);
txtInfo3 = new JTextField();
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 5;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets.top = 5;
gbc.insets.left = 10;
gbc.insets.right = 10;
gbc.insets.bottom = 10;
contentPane.add(txtInfo3, gbc);
}
public static void main(String[] args) {
ThreeInfoGridBagLayout frame = new ThreeInfoGridBagLayout();
frame.setVisible(true);
}
}
```
您可以将此代码复制到您的Java IDE中进行实验和运行。希望这可以帮到您。
阅读全文