gpt4 book ai didi

java - 将框架一分为三

转载 作者:行者123 更新时间:2023-11-30 06:06:37 25 4
gpt4 key购买 nike

我编写了一个如下所示的 GUI。它的编码如下:

  • 有一个由 BorderLayout 管理的主框架。
  • 它的西部是一个带有 3 x 2 按钮网格布局的面板。
  • 中心部分是一个面板。

我想添加第三个面板,如图所示。我怎样才能做到这一点?

enter image description here

最佳答案

In the center part is a Panel

该面板可能还有一个 BorderLayout,将两个组合框放在其 PAGE_START 的一个面板中,并将第三个面板放在 CENTER.

enter image description here

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

public class ThreePanelLayout {

private JComponent ui = null;
private String[] buttonNames = {"Time", "Price", "Route", "Sort", "Admin", "End"};
private String[][] comboFirstNames = {{"Departing Stop"}, {"Final Stop"}};

ThreePanelLayout() {
initUI();
}

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

// I always use this 'ui' panel as a content pane that contains
// everything else..
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));

// now to create the 3 panels of the '3 panel layout'.
JPanel panel1 = new JPanel(new BorderLayout());
panel1.setBackground(Color.RED);
panel1.setBorder(new TitledBorder("Choose Option"));

JPanel panel2 = new JPanel(new BorderLayout());
panel2.setBackground(Color.GREEN);
panel2.setBorder(new TitledBorder("Choose Two Stops"));

JPanel panel3 = new JPanel(new BorderLayout());
panel3.setBackground(Color.ORANGE);
panel3.setBorder(new TitledBorder("Third Panel Here"));

// add the buttons to 1st panel
panel1.add(addButtonsToPanel(buttonNames), BorderLayout.LINE_START);
// add the combos to the top of 2nd panel
panel2.add(addCombosToPanel(comboFirstNames), BorderLayout.PAGE_START);
// give the 3rd panel some size
panel3.add(new JLabel(new ImageIcon(new BufferedImage(400,200,BufferedImage.TYPE_INT_ARGB))));

// now assemble them all together
panel2.add(panel3, BorderLayout.CENTER);
panel1.add(panel2, BorderLayout.CENTER);
ui.add(panel1, BorderLayout.CENTER);
}

private JPanel addButtonsToPanel(String[] ids) {
JPanel p = new JPanel(new GridLayout(0, 2));
for (String id : ids) {
p.add(new JButton(id));
}
return p;
}

private JPanel addCombosToPanel(String[][] ids) {
JPanel p = new JPanel(new FlowLayout());
for (String[] id : ids) {
p.add(new JComboBox<String>(id));
}
return p;
}

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) {
}
ThreePanelLayout o = new ThreePanelLayout();

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/43700072/

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