JScrollPane 滚动条不显示?
JScrollPane scroll bar won't show up?
package me.an.ugm;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
public class Application
{
private JFrame frame;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Application window = new Application();
window.frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public Application()
{
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setTitle("Application");
frame.setBounds(100, 100, 401, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//frame.setResizable(false);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 296, 710);
panel.setLayout(null);
JScrollPane scrollPane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(0, 0, 296, 399);
frame.getContentPane().add(scrollPane);
for (int i = 0, j = 10; i < 20; i++, j += 35)
{
JButton button1 = new JButton();
button1.setBounds(10, j, 25, 25);
panel.add(button1);
JComboBox<String> selectorBox = new JComboBox<>();
selectorBox.setBounds(40, j, 200, 25);
panel.add(selectorBox);
JButton button2 = new JButton();
button2.setBounds(245, j, 25, 25);
panel.add(button2);
}
}
}
不知道为什么滚动条不显示。 JPanel 比 JScrollPane 大,所以我认为它应该显示出来。此外,当我尝试对滚动窗格使用 setPreferredSize 而不是 setBounds 或 setSize 时,什么也没有显示。我对该程序的最终目标是在右侧关闭一个按钮以添加另一组按钮(循环中的按钮),这些按钮将用于 select 另一个项目。我希望程序以 10 行按钮开始,但我将其设置为 20 行以测试滚动条。是没有布局的问题还是我把滚动面板弄乱了?
问题是没有使用正确的 Swing 布局。
这是我创建的 GUI。
我在按钮和组合框中添加了文本,因此 GUI 看起来更逼真。我注释掉了关注 GUI 的外观。我更改了 class 的名称,因为我为 Stack Overflow 编写的所有代码都有一个测试包。
我在单独的方法中创建了按钮 JPanel。我使用了 GridLayout。这允许我创建 20 行 3 个 Swing 组件。这也让我可以 space 出一些组件。
我在另一个单独的方法中创建了滚动 JPanel。关键是为滚动 JPanel 使用 BorderLayout。我将滚动 JPanel 的大小设为按钮 JPanel 的一半,以便滚动。您可以根据需要调整此计算。
这是完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class JButtonScrollGUI {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new JButtonScrollGUI();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private String[] greekAlphabet;
public JButtonScrollGUI() {
this.greekAlphabet = new String[] { "alpha", "beta", "gamma", "epsilon", "zeta" };
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setTitle("Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createScrollPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createScrollPanel() {
JPanel panel = new JPanel(new BorderLayout());
JPanel innerPanel = createButtonPanel();
Dimension d = innerPanel.getPreferredSize();
d.width += 50;
d.height /= 2;
panel.setPreferredSize(d);
JScrollPane scrollPane = new JScrollPane(innerPanel);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new GridLayout(0, 3, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
for (int i = 0; i < 20; i++) {
JButton button1 = new JButton("Previous " + i);
panel.add(button1);
JComboBox<String> selectorBox = new JComboBox<>(greekAlphabet);
panel.add(selectorBox);
JButton button2 = new JButton("Next " + i);
button2.setPreferredSize(button1.getPreferredSize());
panel.add(button2);
}
return panel;
}
}
package me.an.ugm;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
public class Application
{
private JFrame frame;
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Application window = new Application();
window.frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public Application()
{
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setTitle("Application");
frame.setBounds(100, 100, 401, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//frame.setResizable(false);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 296, 710);
panel.setLayout(null);
JScrollPane scrollPane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setBounds(0, 0, 296, 399);
frame.getContentPane().add(scrollPane);
for (int i = 0, j = 10; i < 20; i++, j += 35)
{
JButton button1 = new JButton();
button1.setBounds(10, j, 25, 25);
panel.add(button1);
JComboBox<String> selectorBox = new JComboBox<>();
selectorBox.setBounds(40, j, 200, 25);
panel.add(selectorBox);
JButton button2 = new JButton();
button2.setBounds(245, j, 25, 25);
panel.add(button2);
}
}
}
不知道为什么滚动条不显示。 JPanel 比 JScrollPane 大,所以我认为它应该显示出来。此外,当我尝试对滚动窗格使用 setPreferredSize 而不是 setBounds 或 setSize 时,什么也没有显示。我对该程序的最终目标是在右侧关闭一个按钮以添加另一组按钮(循环中的按钮),这些按钮将用于 select 另一个项目。我希望程序以 10 行按钮开始,但我将其设置为 20 行以测试滚动条。是没有布局的问题还是我把滚动面板弄乱了?
问题是没有使用正确的 Swing 布局。
这是我创建的 GUI。
我在按钮和组合框中添加了文本,因此 GUI 看起来更逼真。我注释掉了关注 GUI 的外观。我更改了 class 的名称,因为我为 Stack Overflow 编写的所有代码都有一个测试包。
我在单独的方法中创建了按钮 JPanel。我使用了 GridLayout。这允许我创建 20 行 3 个 Swing 组件。这也让我可以 space 出一些组件。
我在另一个单独的方法中创建了滚动 JPanel。关键是为滚动 JPanel 使用 BorderLayout。我将滚动 JPanel 的大小设为按钮 JPanel 的一半,以便滚动。您可以根据需要调整此计算。
这是完整的可运行代码。
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class JButtonScrollGUI {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new JButtonScrollGUI();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private String[] greekAlphabet;
public JButtonScrollGUI() {
this.greekAlphabet = new String[] { "alpha", "beta", "gamma", "epsilon", "zeta" };
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setTitle("Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createScrollPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createScrollPanel() {
JPanel panel = new JPanel(new BorderLayout());
JPanel innerPanel = createButtonPanel();
Dimension d = innerPanel.getPreferredSize();
d.width += 50;
d.height /= 2;
panel.setPreferredSize(d);
JScrollPane scrollPane = new JScrollPane(innerPanel);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new GridLayout(0, 3, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
for (int i = 0; i < 20; i++) {
JButton button1 = new JButton("Previous " + i);
panel.add(button1);
JComboBox<String> selectorBox = new JComboBox<>(greekAlphabet);
panel.add(selectorBox);
JButton button2 = new JButton("Next " + i);
button2.setPreferredSize(button1.getPreferredSize());
panel.add(button2);
}
return panel;
}
}