gpt4 book ai didi

java - 如何从框架左侧制作垂直的按钮线?

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

包含面板的框架,其中按钮位于框架左侧的列中:

/image/IUk4C.png

我尝试用真正的 Java 代码制作这张图片,但失败了。

我找不到解决此问题的正确解决方案...

    BorderLayout borderLayout = new BorderLayout();
GridBagLayout gridBagLayout = new GridBagLayout();
GridLayout gridLayout = new GridLayout();
CardLayout cardLayout = new CardLayout();
JPanel panelMain = new JPanel();
JPanel panelLeft = new JPanel();
JPanel panelInnerLeft = new JPanel();
JPanel panelRight = new JPanel();

panelMain.setLayout(borderLayout);
panelLeft.setLayout(gridBagLayout);
panelInnerLeft.setLayout(gridLayout);
panelRight.setLayout(cardLayout);

button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
button3 = new JButton("Button 3");
button4 = new JButton("Button 4");

panelInnerLeft.add(button1,gridLayout);
panelInnerLeft.add(button2,gridLayout);
panelInnerLeft.add(button3,gridLayout);
panelInnerLeft.add(button4,gridLayout);

panelLeft.add(panelInnerLeft);
panelMain.add(panelLeft);
panelMain.add(panelRight);
add(panelMain);

最佳答案

这是用于生成 that screenshot 的确切代码.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class CompoundLayout01 {

private JComponent ui = null;

CompoundLayout01() {
initUI();
}

public void initUI() {
if (ui!=null) return;

ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new TitledBorder("BorderLayout"));

CardLayout cardLayout = new CardLayout();
JPanel cards = new JPanel(cardLayout);
cards.setBorder(new TitledBorder("CardLayout"));
ui.add(cards);

JPanel lineStart = new JPanel(new GridBagLayout());
lineStart.setBorder(new TitledBorder("GridBagLayout"));
// will appear on the left, in a LTR text orientation locale
ui.add(lineStart, BorderLayout.LINE_START);

JPanel buttonsCentered = new JPanel(new GridLayout(0, 1, 10, 10));
buttonsCentered.setBorder(new TitledBorder("GridLayout"));
// as single component added w/no constraint, will be centered
lineStart.add(buttonsCentered);
for (int ii=1; ii<5; ii++) {
JButton b = new JButton("Button " + ii);
buttonsCentered.add(b);
}
}

public JComponent getUI() {
return ui;
}

public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
CompoundLayout01 o = new CompoundLayout01();

JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

关于java - 如何从框架左侧制作垂直的按钮线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52879606/

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