用户窗口界面将三个按钮用GridBagLayout方法纵向排列java代码
时间: 2024-04-29 22:26:16 浏览: 78
好的,下面是Java代码实现:
```
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class VerticalButtonLayout {
public static void main(String[] args) {
JFrame frame = new JFrame("Vertical Button Layout");
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = GridBagConstraints.RELATIVE;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.weighty = 1.0;
JButton button1 = new JButton("Button 1");
panel.add(button1, gbc);
JButton button2 = new JButton("Button 2");
panel.add(button2, gbc);
JButton button3 = new JButton("Button 3");
panel.add(button3, gbc);
frame.getContentPane().add(panel);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
```
这段代码将三个按钮使用GridBagLayout方法纵向排列,其中GridBagConstraints对象指定了按钮在容器中的位置和大小。
阅读全文