JScrollPane 在 JPanel 中不起作用
JScrollPane doesn't work while inside a JPanel
JScrollPane
当我给它一个 JPanel
然后将 JScrollPane
直接添加到带有 frame.getContentPane.add()
的 JFrame
时,它工作得很好。 但是,当 我将 JScrollPane
添加到 JPanel
,然后将 JPanel
添加到 JFrame
时,它不起作用。我需要使用第二种方法,因为我要在 JPanel
和 JFrame
中添加多个内容,并且我需要保持组织有序。这是我的代码。
import java.awt.*;
import javax.swing.*;
public class Main {
/**
* @param inpanel asks if the JScrollPane should
* be inside of a JPanel (so other things can also be added)
*/
public static void testScroll(boolean inpanel) {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setResizable(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.red));
//panel.setLayout(new BoxLayout(panel, 1));
panel.setLayout(new GridLayout(0,1));
for (int i = 0; i < 100; i++) {
JLabel l = new JLabel("hey"+i,SwingConstants.CENTER);
l.setBorder(BorderFactory.createLineBorder(Color.green));
l.setPreferredSize(new Dimension(200,200));
panel.add(l);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setBorder(BorderFactory.createLineBorder(Color.blue));
//**********THIS DOES NOT WORK HOW I WANT IT TO************
if(inpanel){
JPanel holder = new JPanel();
holder.add(scrollPane);
f.getContentPane().add(holder);
}
//************THIS DOES WORK HOW I WANT IT TO****************
else{
f.getContentPane().add(scrollPane);
}
f.pack();
f.setSize(500, 500);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue(bar.getMaximum());
bar.setUnitIncrement(50);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
testScroll(false); //OR TRUE
}
};
SwingUtilities.invokeLater(r);
}
}
在 main 方法中,如果我传递 false,它会像我之前提到的那样工作,但是当我传递 true 时,它会显示没有滚动条。
传false时的图片
传真时的图片
我需要一种方法来将 JScrollPane
添加到 JPanel
并且仍然有效。
提前致谢!
您的问题是 holder JPanel 的布局。默认情况下,它是 FlowLayout,它不会在需要时重新调整其子组件的大小。改为将其设为 BorderLayout,您的滚动窗格将在需要时调整大小。如果您需要更复杂的东西,请查看布局管理器教程。
JScrollPane
当我给它一个 JPanel
然后将 JScrollPane
直接添加到带有 frame.getContentPane.add()
的 JFrame
时,它工作得很好。 但是,当 我将 JScrollPane
添加到 JPanel
,然后将 JPanel
添加到 JFrame
时,它不起作用。我需要使用第二种方法,因为我要在 JPanel
和 JFrame
中添加多个内容,并且我需要保持组织有序。这是我的代码。
import java.awt.*;
import javax.swing.*;
public class Main {
/**
* @param inpanel asks if the JScrollPane should
* be inside of a JPanel (so other things can also be added)
*/
public static void testScroll(boolean inpanel) {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setResizable(true);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.red));
//panel.setLayout(new BoxLayout(panel, 1));
panel.setLayout(new GridLayout(0,1));
for (int i = 0; i < 100; i++) {
JLabel l = new JLabel("hey"+i,SwingConstants.CENTER);
l.setBorder(BorderFactory.createLineBorder(Color.green));
l.setPreferredSize(new Dimension(200,200));
panel.add(l);
}
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setBorder(BorderFactory.createLineBorder(Color.blue));
//**********THIS DOES NOT WORK HOW I WANT IT TO************
if(inpanel){
JPanel holder = new JPanel();
holder.add(scrollPane);
f.getContentPane().add(holder);
}
//************THIS DOES WORK HOW I WANT IT TO****************
else{
f.getContentPane().add(scrollPane);
}
f.pack();
f.setSize(500, 500);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setVisible(true);
JScrollBar bar = scrollPane.getVerticalScrollBar();
bar.setValue(bar.getMaximum());
bar.setUnitIncrement(50);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
testScroll(false); //OR TRUE
}
};
SwingUtilities.invokeLater(r);
}
}
在 main 方法中,如果我传递 false,它会像我之前提到的那样工作,但是当我传递 true 时,它会显示没有滚动条。
传false时的图片
传真时的图片
我需要一种方法来将 JScrollPane
添加到 JPanel
并且仍然有效。
提前致谢!
您的问题是 holder JPanel 的布局。默认情况下,它是 FlowLayout,它不会在需要时重新调整其子组件的大小。改为将其设为 BorderLayout,您的滚动窗格将在需要时调整大小。如果您需要更复杂的东西,请查看布局管理器教程。