0

我在制作JSplitPane我喜欢的作品时遇到了一些麻烦。

首先,我希望它完全填充它的容器(一个 JFrame),而不是让它的大小由拆分窗格内的组件决定。其次,当我调整容器大小时,它不会在JScrollPanes 或JLists 上显示滚动条。

任何想法如何实现这一点。我确定我只需要正确连接一些听众,但我不太确定从哪里开始。

我的面板看起来像这样......

public class MainPanel extends JPanel {
    public MainPanel(){
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();

        p1.setLayout(new BoxLayout(p1, BoxLayout.PAGE_AXIS));
        p2.setLayout(new BoxLayout(p2, BoxLayout.PAGE_AXIS));
        p3.setLayout(new BoxLayout(p3, BoxLayout.PAGE_AXIS));

        p1.add(new SomeButtons());
        p2.add(new SomeButtons());
        p3.add(new SomeButtons());

        p1.add(newNeedyList());
        p2.add(newNeedyList());
        p3.add(newNeedyList());

        Dimension ms = new Dimension(0,0);

        p1.setMinimumSize(ms);
        p2.setMinimumSize(ms);
        p3.setMinimumSize(ms);

        p1.setBackground(Color.red);
        p2.setBackground(Color.green);
        p3.setBackground(Color.blue);



        JScrollPane scp1 = new JScrollPane(p1);
        JScrollPane scp2 = new JScrollPane(p2);
        JScrollPane scp3 = new JScrollPane(p3);

        scp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scp2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scp3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

        scp1.setHorizontalScrollBarPolicy(
           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scp2.setHorizontalScrollBarPolicy(
           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scp3.setHorizontalScrollBarPolicy(
           JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);


        JSplitPane sp1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scp2,scp3);
        JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,scp1,sp1);


        sp1.setDividerLocation(0.5);
        sp2.setDividerLocation(0.333333333);

        sp2.setResizeWeight(0.333333333);
        sp1.setResizeWeight(1.0);

        this.add(sp2, BorderLayout.CENTER);

    }

    /** Some buttons... this part is just to fill the
     panel roughly how I want it look */

    private class SomeButtons extends JPanel{
        public SomeButtons(){
            this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
            this.add(new JLabel("Stuff:"));
            for(int i=0; i<5; i++){
                final JButton btn = new JButton(" "+i+" ");
                btn.addActionListener(new ActionListener(){

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        JOptionPane.showMessageDialog(btn,
                                "Feck off!", "You have pressed a button",
                                JOptionPane.WARNING_MESSAGE);
                    }
                });
                this.add(btn);

            }
        }
    }

    private JList newNeedyList(){
        String[] s = new String[7];
        s[0] = "Choose Me!";
        for (int i=1; i<7; i++){
            s[i] = "No! Choose ME!";
        }
        return new JList(s);
    }
}

目前我有它在一个 JFrame

public class ThreePanelsTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame();

        MainPanel panel = new MainPanel();

        frame.add(panel);

        frame.setSize(1000,600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);

    }
}
4

2 回答 2

2

我认为你不需要添加MainPanel类,而是将你添加到Mainpanel对象中的组件直接添加到JFrame的contentPane中......

final Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout(2, 2));
于 2013-08-28T14:13:29.993 回答
1
  1. 一切都取决于我在此处发布的评论太难说了,因为您的描述很矛盾,我的观点 ---> 将执行的输出绘制到图像,


  2. 使用 frame.pack() 而不是 frame.setSize(1000,600);

  3. setDividerLocation() 应该在 invokeLater 中包装(延迟)

  4. BoxLayout 接受最小、最大和首选尺寸,覆盖它,

于 2013-08-28T14:48:30.677 回答