gpt4 book ai didi

java - 我不知道如何排列这些按钮,如链接中所示

转载 作者:行者123 更新时间:2023-12-01 12:38:42 26 4
gpt4 key购买 nike

package GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridBagLayoutEx2
{
public GridBagLayoutEx2()
{
JFrame frame = new JFrame("GridBagLayoutEx2");
frame.setVisible(true);
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");

gbc.insets = new Insets(2, 2, 2, 2);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 5;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.VERTICAL;
panel.add(b1, gbc);

gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridheight = 1;
gbc.gridwidth = 1;
panel.add(b2, gbc);

gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridheight = 1;
panel.add(b3, gbc);

gbc.gridx = 3;
gbc.gridy = 3;
gbc.gridheight = 1;
panel.add(b4, gbc);

gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(b5, gbc);

frame.add(panel);
//frame.pack();
}

public static void main(String[] args) {
new GridBagLayoutEx2();
}

}

enter image description here

大家好。我已经尝试解决这个 GUI 一段时间了。尽管我看了很多GUI教程,但经过无数次的尝试和错误后我放弃了。在上图中,您可以看到我正在尝试制作的布局。提前致谢...

最佳答案

考虑使用MigLayout管理器。对于单位类型,我选择了厘米。MigLayout 提供多种单元类型可供选择。

以下示例非常适合您的形象:

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class ButtonsEx extends JFrame {

public ButtonsEx() {

initUI();

setTitle("MigLayout example");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private void initUI() {

JPanel pnl = new JPanel(new MigLayout());
pnl.add(new JButton("Button"), "spany 2, grow, w 4cm, h 4cm");
pnl.add(new JButton("Button"), "spanx 2, w 4cm, h 2cm, grow");
pnl.add(new JButton("Button"), "w 2cm, h 2cm, wrap");
pnl.add(new JButton("Button"), "w 2cm, h 2cm");
pnl.add(new JButton("Button"), "w 2cm, h 2cm");
pnl.add(new JButton("Button"), "w 2cm, h 2cm, wrap");

add(pnl);

pack();
}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ButtonsEx ex = new ButtonsEx();
ex.setVisible(true);
}
});
}
}

MigLayout example

关于java - 我不知道如何排列这些按钮,如链接中所示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25333132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com