我想在ScrollPane中使用BoxLayout,它具有Panels的ArrayList

时间:2014-08-08 00:58:18

标签: java swing jpanel boxlayout

当我将outpanel设置为BoxLayout时,面板消失。但是滚动条显示表明我在ArrayList中的面板位于正确的位置。 我是Java的新手,所以我会感激任何评论。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.*;
import javax.swing.*;

public class gui extends JFrame{

    int ctr=0, top=5;
    public List<JPanel> o_panels = new ArrayList<JPanel>(); //Your List    

    public gui(){
        super("MCC");
        setLayout(null);
        //Output panel for the results
        JPanel outpanel = new JPanel();
        outpanel.setBackground(Color.blue); 
        outpanel.setVisible(true);
        outpanel.setLayout(new BoxLayout(outpanel, BoxLayout.PAGE_AXIS));           
        //Scroll pane
        JScrollPane scrollPane = new JScrollPane(outpanel,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);            
        scrollPane.setBounds(0,0,780,400);
        add(scrollPane);            
        //result panel
        //creating and adding panels in to the array list
        while(ctr<=4){
            JPanel label1 = new JPanel();
            label1.setPreferredSize(new Dimension(600,100));
            o_panels.add(label1);
            outpanel.add(o_panels.get(ctr));        
        ctr++;
        }
    }

    public void runGui(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800,600);
        this.setVisible(true);
        this.setResizable(false);
        //i call this on the other class
    }
}

1 个答案:

答案 0 :(得分:2)

您的代码存在错误,问题是,您没有建立任何方法来查看您添加的内容

看看这个......

while (ctr <= 4) {
    JPanel label1 = new JPanel();
    label1.setPreferredSize(new Dimension(600, 100));
    o_panels.add(label1);
    outpanel.add(o_panels.get(ctr));
    ctr++;
}

所有面板都是相同的颜色,你没有添加任何颜色,所以你怎么可能知道它们是否被添加或布局正确...

我简单地添加了label1.setBorder(new LineBorder(Color.RED));并得到了这个结果......

Panels

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }
                Test test = new Test();
                test.runGui();
            }
        });
    }

    int ctr = 0, top = 5;

    public List<JPanel> o_panels = new ArrayList<JPanel>(); //Your List

    public Test() {
        super("MCC");
        //Output panel for the results
        JPanel outpanel = new JPanel();
        outpanel.setBackground(Color.blue);
        outpanel.setVisible(true);
        outpanel.setLayout(new BoxLayout(outpanel, BoxLayout.PAGE_AXIS));

        //Scroll pane
        JScrollPane scrollPane = new JScrollPane(outpanel,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        scrollPane.setBounds(0, 0, 780, 400);
        add(scrollPane);

    //result panel
        //creating and adding panels in to the array list
        while (ctr <= 4) {
            JPanel label1 = new JPanel();
            label1.setPreferredSize(new Dimension(600, 100));
            label1.setBorder(new LineBorder(Color.RED));
            o_panels.add(label1);
            outpanel.add(o_panels.get(ctr));
            ctr++;
        }
    }

    public void runGui() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        this.setVisible(true);
//        this.setResizable(false);
        setLocationRelativeTo(null);
    }
}

另请查看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

你真的应该避免使用null布局,像素完美布局是现代ui设计中的错觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正