如何设置 JCheckBox 使用 JScrollPane?
How to set JCheckBox to use JScrollPane?
I want to make JCheckBox List to use arraylist into this left side JScrollPane. 但是这个 JScrollPane 只是粘贴并添加了最后一个索引 JCheckBox。任何时候都不要让我解决这个问题。我要添加 JList 吗?我想尽快解决这个问题。这是我的代码。
package dimi.voca;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.ScrollPaneConstants;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.Box;
import javax.swing.JList;
public class LearnSelect extends JFrame
{
public LearnSelect() {
super("DimiVoca in PC");
JPanel panel = new JPanel();
setSize(650, 500);
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
JList list = new JList();
panel.add(list);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setToolTipText("");
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(28, 10, 200, 441);
getContentPane().add(scrollPane);
ArrayList<JCheckBox> Day = new ArrayList<JCheckBox>();
String label[] = { "Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6",
"Day 7", "Day 8", "Day 9", "Day 10", "Day 11", "Day 12",
"Day 13", "Day 14", "Day 15", "Day 16", "Day 17", "Day 18",
"Day 19", "Day 20", "Day 21", "Day 22", "Day 23", "Day 24",
"Day 25", "Day 26", "Day 27", "Day 28", "Day 29", "Day 30",
"Day 31", "Day 32", "Day 33", "Day 34", "Day 35", "Day 36",
"Day 37", "Day 38", "Day 39", "Day 40", "Day 41", "Day 42",
"Day 43", "Day 44", "Day 45", "Day 46", "Day 47", "Day 48",
"Day 49", "Day 50", "Day 51", "Day 52", "Day 53", "Day 54",
"Day 55", "Day 56", "Day 57", "Day 58", "Day 59", "Day 60"};
for(int i = 0; i <= label.length - 1; i++)
{
JCheckBox checkbox = new JCheckBox(label[i]);
Day.add(checkbox);
scrollPane.setViewportView(checkbox);
}
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane_1.setToolTipText("");
scrollPane_1.setBounds(227, 10, 200, 441);
getContentPane().add(scrollPane_1);
JButton close = new JButton("\uB2EB\uAE30");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
dispose();
}
});
close.setBounds(481, 428, 97, 23);
getContentPane().add(close);
JButton start = new JButton("\uC2DC\uC791");
start.setBounds(481, 395, 97, 23);
getContentPane().add(start);
JCheckBox isRandom = new JCheckBox("\uB79C\uB364 \uD5C8\uC6A9");
isRandom.setBounds(481, 100, 145, 23);
getContentPane().add(isRandom);
JCheckBox loadCreate = new JCheckBox("Create\uC5D0\uC11C \uBC1B\uC544\uC624\uAE30");
loadCreate.setBounds(481, 150, 145, 23);
getContentPane().add(loadCreate);
setVisible(true);
}
}
在您的 for 循环中,您将滚动窗格的视口视图设置为最后一个 JCheckBox,它将是唯一可见的。如果您希望在滚动窗格中看到所有 JCheckBox,则在 for 循环之前创建一个 JPanel,为其提供一个 GridLayout,然后将您的 JCheckBox 添加到 for 循环内的这个单个 JPanel。然后将该 JPanel 添加到 JScrollPane 的视口视图中。
JPanel dayPanel = new JPanel(new GridLayout(0, 1)); // !!
ArrayList<JCheckBox> Day = new ArrayList<JCheckBox>();
String label[] = { "Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7", "Day 8",
"Day 9", "Day 10", "Day 11", "Day 12", "Day 13", "Day 14", "Day 15", "Day 16",
"Day 17", "Day 18", "Day 19", "Day 20", "Day 21", "Day 22", "Day 23", "Day 24",
"Day 25", "Day 26", "Day 27", "Day 28", "Day 29", "Day 30", "Day 31", "Day 32",
"Day 33", "Day 34", "Day 35", "Day 36", "Day 37", "Day 38", "Day 39", "Day 40",
"Day 41", "Day 42", "Day 43", "Day 44", "Day 45", "Day 46", "Day 47", "Day 48",
"Day 49", "Day 50", "Day 51", "Day 52", "Day 53", "Day 54", "Day 55", "Day 56",
"Day 57", "Day 58", "Day 59", "Day 60" };
for (int i = 0; i <= label.length - 1; i++) {
JCheckBox checkbox = new JCheckBox(label[i]);
Day.add(checkbox);
// !! scrollPane.setViewportView(checkbox);
dayPanel.add(checkbox); // !!
}
scrollPane.setViewportView(dayPanel); // !!
这里还有一个问题:
getContentPane().setLayout(null);
虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用时 运行 遇到的困难就越严重他们。当 GUI 调整大小时,它们不会调整您的组件的大小,它们很难增强或维护,当放置在滚动窗格中时它们会完全失败,当在所有平台或与原始屏幕分辨率不同的屏幕分辨率上查看时,它们看起来很糟糕。
您将想要了解布局管理器并使用它们。
I want to make JCheckBox List to use arraylist into this left side JScrollPane. 但是这个 JScrollPane 只是粘贴并添加了最后一个索引 JCheckBox。任何时候都不要让我解决这个问题。我要添加 JList 吗?我想尽快解决这个问题。这是我的代码。
package dimi.voca;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.ScrollPaneConstants;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import javax.swing.Box;
import javax.swing.JList;
public class LearnSelect extends JFrame
{
public LearnSelect() {
super("DimiVoca in PC");
JPanel panel = new JPanel();
setSize(650, 500);
getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
JList list = new JList();
panel.add(list);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setToolTipText("");
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(28, 10, 200, 441);
getContentPane().add(scrollPane);
ArrayList<JCheckBox> Day = new ArrayList<JCheckBox>();
String label[] = { "Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6",
"Day 7", "Day 8", "Day 9", "Day 10", "Day 11", "Day 12",
"Day 13", "Day 14", "Day 15", "Day 16", "Day 17", "Day 18",
"Day 19", "Day 20", "Day 21", "Day 22", "Day 23", "Day 24",
"Day 25", "Day 26", "Day 27", "Day 28", "Day 29", "Day 30",
"Day 31", "Day 32", "Day 33", "Day 34", "Day 35", "Day 36",
"Day 37", "Day 38", "Day 39", "Day 40", "Day 41", "Day 42",
"Day 43", "Day 44", "Day 45", "Day 46", "Day 47", "Day 48",
"Day 49", "Day 50", "Day 51", "Day 52", "Day 53", "Day 54",
"Day 55", "Day 56", "Day 57", "Day 58", "Day 59", "Day 60"};
for(int i = 0; i <= label.length - 1; i++)
{
JCheckBox checkbox = new JCheckBox(label[i]);
Day.add(checkbox);
scrollPane.setViewportView(checkbox);
}
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
scrollPane_1.setToolTipText("");
scrollPane_1.setBounds(227, 10, 200, 441);
getContentPane().add(scrollPane_1);
JButton close = new JButton("\uB2EB\uAE30");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
dispose();
}
});
close.setBounds(481, 428, 97, 23);
getContentPane().add(close);
JButton start = new JButton("\uC2DC\uC791");
start.setBounds(481, 395, 97, 23);
getContentPane().add(start);
JCheckBox isRandom = new JCheckBox("\uB79C\uB364 \uD5C8\uC6A9");
isRandom.setBounds(481, 100, 145, 23);
getContentPane().add(isRandom);
JCheckBox loadCreate = new JCheckBox("Create\uC5D0\uC11C \uBC1B\uC544\uC624\uAE30");
loadCreate.setBounds(481, 150, 145, 23);
getContentPane().add(loadCreate);
setVisible(true);
}
}
在您的 for 循环中,您将滚动窗格的视口视图设置为最后一个 JCheckBox,它将是唯一可见的。如果您希望在滚动窗格中看到所有 JCheckBox,则在 for 循环之前创建一个 JPanel,为其提供一个 GridLayout,然后将您的 JCheckBox 添加到 for 循环内的这个单个 JPanel。然后将该 JPanel 添加到 JScrollPane 的视口视图中。
JPanel dayPanel = new JPanel(new GridLayout(0, 1)); // !!
ArrayList<JCheckBox> Day = new ArrayList<JCheckBox>();
String label[] = { "Day 1", "Day 2", "Day 3", "Day 4", "Day 5", "Day 6", "Day 7", "Day 8",
"Day 9", "Day 10", "Day 11", "Day 12", "Day 13", "Day 14", "Day 15", "Day 16",
"Day 17", "Day 18", "Day 19", "Day 20", "Day 21", "Day 22", "Day 23", "Day 24",
"Day 25", "Day 26", "Day 27", "Day 28", "Day 29", "Day 30", "Day 31", "Day 32",
"Day 33", "Day 34", "Day 35", "Day 36", "Day 37", "Day 38", "Day 39", "Day 40",
"Day 41", "Day 42", "Day 43", "Day 44", "Day 45", "Day 46", "Day 47", "Day 48",
"Day 49", "Day 50", "Day 51", "Day 52", "Day 53", "Day 54", "Day 55", "Day 56",
"Day 57", "Day 58", "Day 59", "Day 60" };
for (int i = 0; i <= label.length - 1; i++) {
JCheckBox checkbox = new JCheckBox(label[i]);
Day.add(checkbox);
// !! scrollPane.setViewportView(checkbox);
dayPanel.add(checkbox); // !!
}
scrollPane.setViewportView(dayPanel); // !!
这里还有一个问题:
getContentPane().setLayout(null);
虽然空布局和 setBounds()
对于 Swing 新手来说似乎是创建复杂 GUI 的最简单和最好的方法,但您创建的 Swing GUI 越多,您在使用时 运行 遇到的困难就越严重他们。当 GUI 调整大小时,它们不会调整您的组件的大小,它们很难增强或维护,当放置在滚动窗格中时它们会完全失败,当在所有平台或与原始屏幕分辨率不同的屏幕分辨率上查看时,它们看起来很糟糕。
您将想要了解布局管理器并使用它们。