Java 添加JPanel数组时,JScrollPane不显示滚动条

Java 添加JPanel数组时,JScrollPane不显示滚动条,java,swing,Java,Swing,我正在JScrollPane中添加一个JPanel数组。JPanel数组正在添加到JScrollPane中,但滚动条不会显示出来 public class MyFrame extends JFrame { private JPanel contentPane; private JPanel panel_1; private JScrollPane scrollPane; public static void main(String[] args) {

我正在JScrollPane中添加一个JPanel数组。JPanel数组正在添加到JScrollPane中,但滚动条不会显示出来

public class MyFrame extends JFrame {

    private JPanel contentPane;
    private JPanel panel_1;
    private JScrollPane scrollPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyFrame frame = new MyFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MyFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 288, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        panel_1 = new JPanel();
        panel_1.setBounds(10, 114, 434, 136);
        panel_1.setLayout(null);

        scrollPane = new JScrollPane(panel_1);
        scrollPane.setBounds(52, 57, 164, 126);
        scrollPane.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Histogram", TitledBorder.LEADING, TitledBorder.TOP, null, Color.BLUE));
        scrollPane.setLayout(new ScrollPaneLayout());
        contentPane.add(scrollPane);

        buildBar();
    }

    JPanel[] barPanel;
    private void buildBar(){
        int x=0,y=22,w=100,h=80,s=10,n=3;   
        barPanel = new JPanel[n];
        for(int i=0; i<n; i++){
            barPanel[i] = new JPanel();
            barPanel[i].setBounds(x, y, w, h);
            barPanel[i].setBackground(new Color(255,0,0));
            panel_1.add(barPanel[i]);
            panel_1.revalidate();
            panel_1.repaint();
            x = x + w + s;
        }
    }
}
公共类MyFrame扩展了JFrame{
私有JPanel内容窗格;
私人JPanel小组1;
私有JScrollPane滚动窗格;
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
MyFrame=新的MyFrame();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
公共MyFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
立根(100100288300);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
panel_1=新的JPanel();
面板1.立根(101114434136);
面板1.设置布局(空);
scrollPane=新的JScrollPane(面板1);
滚动窗格。后退(52、57、164、126);
scrollPane.setBorder(新标题边框(UIManager.getBorder(“标题边框.border”),“直方图”,标题边框.LEADING,标题边框.TOP,null,Color.BLUE));
setLayout(新的ScrollPaneLayout());
添加(滚动窗格);
buildBar();
}
JPanel[]barPanel;
私有void buildBar(){
int x=0,y=22,w=100,h=80,s=10,n=3;
barPanel=新JPanel[n];

对于(inti=0;i你用这两条线击中了自己的脚:

    panel_1.setBounds(10, 114, 434, 136);
    panel_1.setLayout(null);
这两种情况都会破坏JScrollPane使用和显示滚动条的能力。您需要做的是:不要使用setBounds,而是让组件使用其preferredSize,并避免使用null布局,因为这不会更改容器的preferredSize

另一个要刻意避免空布局的原因

e、 g

导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.GridBagLayout;
导入java.awt.GridLayout;
导入javax.swing.*;
公共类MyPanel2扩展了JPanel{
私有静态最终整型参数W=388;
私有静态final int PREF_H=PREF_W;
公共静态最终int-INNER\u-PREF\u W=434;
公共静态最终int-INNER_-PREF_H=126;
私有静态最终内部历史面板计数=6;
私有静态最终维度VP_SZ=新维度(164126);
private JPanel holderPanel=新JPanel(新网格布局(0,1));
公共MyPanel2(){
int w=内部优先权;
int h=内部优先权;
对于(int i=0;i<历史面板计数;i++){
holderPanel.添加(新内面板(w,h));
}
JScrollPane scrollPane=新的JScrollPane(holderPanel);
scrollPane.getViewport().setPreferredSize(VP_SZ);
setLayout(新的GridBagLayout());
添加(滚动窗格);
}
@凌驾
公共维度getPreferredSize(){
如果(isPreferredSizeSet()){
返回super.getPreferredSize();
}
返回新维度(PREF_W,PREF_H);
}
私有类InnerPanel扩展了JPanel{
私人int w;
私有inth;
公共内部面板(内部w、内部h){
这个.w=w;
这个,h=h;
setboorder(BorderFactory.createLineBorder(Color.BLUE));
}
@凌驾
受保护组件(图形g){
超级组件(g);
int x=0,y=22,w=100,h=80,s=10,n=3;
g、 setColor(Color.RED);
对于(int i=0;i{
createAndShowGui();
});
}
}

你在用这两条线打自己的脚:

    panel_1.setBounds(10, 114, 434, 136);
    panel_1.setLayout(null);
这两种情况都会破坏JScrollPane使用和显示滚动条的能力。您需要做的是:不要使用setBounds,而是让组件使用其preferredSize,并避免使用null布局,因为这不会更改容器的preferredSize

另一个要刻意避免空布局的原因

e、 g

导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.GridBagLayout;
导入java.awt.GridLayout;
导入javax.swing.*;
公共类MyPanel2扩展了JPanel{
私有静态最终整型参数W=388;
私有静态final int PREF_H=PREF_W;
公共静态最终int-INNER\u-PREF\u W=434;
公共静态最终int-INNER_-PREF_H=126;
私有静态最终内部历史面板计数=6;
私有静态最终维度VP_SZ=新维度(164126);
private JPanel holderPanel=新JPanel(新网格布局(0,1));
公共MyPanel2(){
int w=内部优先权;
int h=内部优先权;
对于(int i=0;i<历史面板计数;i++){
holderPanel.添加(新内面板(w,h));
}
JScrollPane scrollPane=新的JScrollPane(holderPanel);
scrollPane.getViewport().setPreferredSize(VP_SZ);
setLayout(新的GridBagLayout());
添加(滚动窗格);
}
@凌驾
公共维度getPreferredSi
contentPane.setLayout(null);