gpt4 book ai didi

SplitPane 中的 Java Swing 复选框

转载 作者:行者123 更新时间:2023-12-02 10:30:27 29 4
gpt4 key购买 nike

我所追求的当前布局只是一个水平分割 Pane ,其中左侧是复选框,右侧将输出。在用户检查所有想要的项目后,我最终将在左侧添加一个提交按钮,并将结果显示在右侧。当前的问题是我什至无法显示背景颜色,并且复选框正在添加不稳定的内容。在某些时候,我只能在左侧面板中看到一个复选框,我不确定为什么,而且我还将每个容器设置为可见,但仍然看不到它。我将它们添加到 addBoxes 函数中。

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class CheckBox2 extends JFrame {
private ArrayList<JCheckBox> boxes = new ArrayList<JCheckBox>();
JLabel leftLabel;
JLabel rightLabel;
JSplitPane splitPane;

public CheckBox2() {
leftLabel = new JLabel();
rightLabel = new JLabel();

splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(leftLabel), new JScrollPane(rightLabel) );
leftLabel.setBackground(Color.BLUE);
rightLabel.setBackground(Color.RED);
leftLabel.setVisible(true);
rightLabel.setVisible(true);
splitPane.setVisible(true);
add(splitPane);



}

void addBoxes() {
int i = 0;

for ( i = 0; i < 1; i++ ) {
add(new JCheckBox("word" + i ) );
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub

CheckBox2 cb = new CheckBox2();
cb.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
cb.setSize(340, 340);
cb.setVisible(true);
cb.addBoxes();
}



}

最佳答案

首先阅读 Laying Out Components Within a ContainerHow to Use Split Panes两者都包含大量示例。

Swing 布局很懒惰。这意味着,除非您有意触发布局过程,否则任何更改都不会反射(reflect)在 UI 上(直到触发布局过程,例如调整窗口大小或使其首次可见)。

虽然您可以在要更改的容器上调用revalidaterepaint,但在您的情况下,只需最后调用setVisible就会有相同的结果想要的效果

Thanks, so with that I'm just getting the last checkbox, checkbox 9 to show up but it's not giving split screen or showing color :(

这是因为 JFrame 默认情况下使用 BorderLayout,它只允许在五个可用位置中的任何一个位置管理单个组件。相反,您需要将复选框添加到分割 Pane 中的容器之一。

SplitPane and checkboxes

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class CheckBox2 extends JFrame {

private ArrayList<JCheckBox> boxes = new ArrayList<JCheckBox>();
JSplitPane splitPane;

private JPanel leftPanel;
private JPanel rightPanel;

public CheckBox2() {

leftPanel = new JPanel(new GridBagLayout());
rightPanel = new JPanel(new GridBagLayout()) {
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
};

splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, new JScrollPane(rightPanel));
leftPanel.setBackground(Color.BLUE);
rightPanel.setBackground(Color.RED);
add(splitPane);

addBoxes();
}

void addBoxes() {
int i = 0;

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
for (i = 0; i < 10; i++) {
leftPanel.add(new JCheckBox("word" + i), gbc);
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub

CheckBox2 cb = new CheckBox2();
cb.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
cb.pack();
cb.setLocationRelativeTo(null);
cb.setVisible(true);
}

}

关于SplitPane 中的 Java Swing 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53644381/

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