gpt4 book ai didi

java - 具有固定大小的动态 GridLayout

转载 作者:搜寻专家 更新时间:2023-11-01 01:33:59 26 4
gpt4 key购买 nike

需要指导..

想要实现如下图所示的效果,即子面板保持相同大小但最多包含 4 个组件。我意识到我可以通过更改下面的网格布局中的列数来实现这一点,但为了使子面板保持相同的大小,我将不得不更改边框大小,这是我不介意做的事情,但看起来有点麻烦并且想知道是否有聪明的方法来解决这个问题。我提供的代码基于提供给我的示例代码 here

enter image description here

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

public class DynamicGridLayout {

private JPanel ui = null;

DynamicGridLayout() {
initUI();
}

public final void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridBagLayout());
ui.setBorder(new TitledBorder("Parent Panel"));

JPanel controls = new JPanel(new GridLayout(2,0,10,10));
ui.add(controls);
controls.setBackground(Color.RED);
controls.setBorder(new TitledBorder("Child Panel"));

for (int ii=1; ii<5; ii++) {
addLabel(controls, "String " + ii);
}
}

public JComponent getUI() {
return ui;
}

private void addLabel(JPanel panel, String text) {
JPanel controls1 = new JPanel(new GridLayout(3,0,3,3));
controls1.setBackground(Color.green);
controls1.setBorder(new EmptyBorder(75,75,75,75));
panel.add(controls1);
}

public static void main(String[] args) {
Runnable r = new Runnable() {

@Override
public void run() {
JFrame f = new JFrame("Three Button/Text Field Combo");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
DynamicGridLayout dgl = new DynamicGridLayout();
f.setContentPane(dgl.getUI());
f.setSize(1050, 720);
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

最佳答案

  • 只是为了我的享受,乐趣,

  • 请注意,在 LayoutManager 从 GridLayout 切换回 BorderLayout 后,必须通知父级 JPanel(最小/最大/首选尺寸所需的最深子级覆盖)(图 5 中不需要的输出。)

    <

.

enter image description here

.

enter image description here

.

enter image description here

.

enter image description here

.

enter image description here

.

来自

.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class DynamicGridLayout {

private JFrame f = new JFrame("Three Button/Text Field Combo");
private JPanel ui = new JPanel(new GridBagLayout()) {
@Override
public Dimension getMinimumSize() {
return new Dimension(400, 300);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}

@Override
public Dimension getMaximumSize() {
return new Dimension(800, 600);
}
};
private JPanel controls = new JPanel() {
@Override
public Dimension getMinimumSize() {
return new Dimension(400, 300);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}

@Override
public Dimension getMaximumSize() {
return new Dimension(1050, 720);
}
};
private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack;

DynamicGridLayout() {
initUI();
}

public final void initUI() {
ui.setBorder(new TitledBorder("Parent Panel"));
ui.add(controls);
controls.setBackground(Color.RED);
controls.setBorder(new TitledBorder("Child Panel"));
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.add(ui);
f.add(getCheckBoxPanel(), "South");
f.setMinimumSize(ui.getPreferredSize());
f.setVisible(true);
}

public JComponent getUI() {
return ui;
}

private void addLabel() {
JPanel controls1 = new JPanel(new GridLayout(3, 0, 3, 3));
controls1.setBackground(Color.green);
controls1.setBorder(new EmptyBorder(75, 75, 75, 75));
controls.add(controls1);
}

private JPanel getCheckBoxPanel() {
checkValidate = new JCheckBox("validate");
checkValidate.setSelected(false);
checkReValidate = new JCheckBox("revalidate");
checkReValidate.setSelected(true);
checkRepaint = new JCheckBox("repaint");
checkRepaint.setSelected(true);
checkPack = new JCheckBox("pack");
checkPack.setSelected(false);
JButton addComp = new JButton("Add New One");
addComp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (controls.getComponentCount() < 1) {
controls.setLayout(new BorderLayout());
addLabel();
makeChange();
} else if (controls.getComponentCount() == 1) {
controls.setLayout(new GridLayout(0, 2, 10, 10));
addLabel();
makeChange();
} else {
controls.setLayout(new GridLayout(2, 0, 10, 10));
addLabel();
makeChange();
}
System.out.println(" Components Count after Adds :" + controls.getComponentCount());
}
});
JButton removeComp = new JButton("Remove One");
removeComp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int count = controls.getComponentCount();
if (count > 0) {
if (controls.getComponentCount() == 2) {
controls.setLayout(new BorderLayout());
controls.remove(0);
} else if (controls.getComponentCount() == 3) {
controls.setLayout(new GridLayout(0, 2, 10, 10));
controls.remove(0);
} else {
controls.remove(0);
}

}
makeChange();
System.out.println(" Components Count after Removes :" + controls.getComponentCount());
}
});
JPanel panel2 = new JPanel();
panel2.add(checkValidate);
panel2.add(checkReValidate);
panel2.add(checkRepaint);
panel2.add(checkPack);
panel2.add(addComp);
panel2.add(removeComp);
return panel2;
}

private void makeChange() {
if (checkValidate.isSelected()) {
ui.validate();
}
if (checkReValidate.isSelected()) {
ui.revalidate();
}
if (checkRepaint.isSelected()) {
ui.repaint();
}
if (checkPack.isSelected()) {
f.pack();
}
}

public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
DynamicGridLayout dgl = new DynamicGridLayout();
}
};
SwingUtilities.invokeLater(r);
}
}

关于java - 具有固定大小的动态 GridLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26797304/

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