带有JScrollPane的嵌套面板

时间:2015-07-25 20:30:13

标签: java swing jpanel jscrollpane miglayout

我遇到嵌套JPanelJScrollPane的问题。我的布局如下:

JPanel MigLayout包含8x8个单元格。在第一列中,每个单元格都有JButton,最后一列每个单元格都包含按钮。在这个列之间,我有嵌套的面板跨越其余的单元格。

这就是它的样子。

GUI layout

我想将JScrollPane添加到中央面板,以便我可以在其上添加其他按钮并滚动。但是,只要我添加滚动窗格,我的中央面板就会低于一切,或者只是消失。

enter image description here

这是我的代码:

package test;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import net.miginfocom.swing.MigLayout;
public class test extends JFrame{
public static void main(String[] args) {
    // TODO Auto-generated method stub
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            test frame = new test();
            frame.setResizable(true);   // ovo zakomentiraj ako hoces full screen
            frame.setVisible(true);
        }
    });     
}
public test() {
    JPanel panel = new JPanel(new MigLayout("debug, insets 0", "", "[]0[]"));   
    JPanel central_panel = new JPanel();
    JPanel title_panel = new JPanel();
    JScrollPane central_scroll_panel = new JScrollPane();


    JButton left_but1 = new JButton();
    JButton left_but2 = new JButton();
    JButton left_but3 = new JButton();
    JButton left_but4 = new JButton();
    JButton left_but5 = new JButton();
    JButton left_but6 = new JButton();
    JButton left_but7 = new JButton();
    JButton right_but1 = new JButton();
    JButton right_but2 = new JButton();
    JButton right_but3 = new JButton();
    JButton right_but4 = new JButton();
    JButton right_but5 = new JButton();
    JButton right_but6 = new JButton();
    JButton right_but7 = new JButton();

    panel.add(left_but1,"cell 0 2, gapy 10");
    panel.add(left_but2,"cell 0 3, gapy 10");
    panel.add(left_but3,"cell 0 4, gapy 10");
    panel.add(left_but4,"cell 0 5, gapy 10");
    panel.add(left_but5,"cell 0 6, gapy 10");
    panel.add(left_but6,"cell 0 7, gapy 10");
    panel.add(left_but7,"cell 0 8, gapy 10");
    panel.add(right_but1,"cell 2 2, align right, gapy 10");
    panel.add(right_but2,"cell 2 3, align right, gapy 10");
    panel.add(right_but3,"cell 2 4, align right, gapy 10");
    panel.add(right_but4,"cell 2 5, align right, gapy 10");
    panel.add(right_but5,"cell 2 6, align right, gapy 10");
    panel.add(right_but6,"cell 2 7, align right, gapy 10");
    panel.add(right_but7,"cell 2 8, align right, wrap 135");

    panel.add(central_panel, "cell 1 2,span 1 8, grow, w 1175, h 552");         // Ako stavis "span 1 7" tada ide do razine gumbica
    central_panel.setLayout(new MigLayout("debug",
              "[center,grow]", // Column constraints with default align
              "[center]0[]"));

    for (int i=0;i<=1000;i++){
        if (i%10==0){
            central_panel.add(new JButton(),"wrap");
        }
        else{
            central_panel.add(new JButton());
        }
    }
      central_scroll_panel=new JScrollPane(central_panel);
      central_scroll_panel.setSize(1175, 500);
      central_scroll_panel.setViewportView(central_panel);
      getContentPane().add(central_scroll_panel);
      central_scroll_panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    setContentPane(panel);
}

}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

问题是,您创建了JScrollPane并将其添加到Frame。

JScrollPane central_scroll_panel = new JScrollPane();
...
panel.add(central_panel, "cell 1 2,span 1 8, grow, w 1175, h 552");

您正在向其中添加按钮,然后将新的JScrollPane与变量central_scroll_panel相关联。因此,将创建一个空的JScrollPane并显示在框架中 您需要声明一次JScrollPane并使用这一个。如下面的代码所示:

public class App extends JFrame {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                App frame = new App();
                frame.setResizable(true); // ovo zakomentiraj ako hoces full
                                            // screen
                frame.setVisible(true);
            }
        });
    }

    public App() {
        JPanel panel = new JPanel(new MigLayout("debug, insets 0", "", "[]0[]"));
        JPanel central_panel = new JPanel();
        JPanel title_panel = new JPanel();
        JScrollPane central_scroll_panel;

        JButton left_but1 = new JButton();
        JButton left_but2 = new JButton();
        JButton left_but3 = new JButton();
        JButton left_but4 = new JButton();
        JButton left_but5 = new JButton();
        JButton left_but6 = new JButton();
        JButton left_but7 = new JButton();
        JButton right_but1 = new JButton();
        JButton right_but2 = new JButton();
        JButton right_but3 = new JButton();
        JButton right_but4 = new JButton();
        JButton right_but5 = new JButton();
        JButton right_but6 = new JButton();
        JButton right_but7 = new JButton();

        panel.add(left_but1, "cell 0 2, gapy 10");
        panel.add(left_but2, "cell 0 3, gapy 10");
        panel.add(left_but3, "cell 0 4, gapy 10");
        panel.add(left_but4, "cell 0 5, gapy 10");
        panel.add(left_but5, "cell 0 6, gapy 10");
        panel.add(left_but6, "cell 0 7, gapy 10");
        panel.add(left_but7, "cell 0 8, gapy 10");
        panel.add(right_but1, "cell 2 2, align right, gapy 10");
        panel.add(right_but2, "cell 2 3, align right, gapy 10");
        panel.add(right_but3, "cell 2 4, align right, gapy 10");
        panel.add(right_but4, "cell 2 5, align right, gapy 10");
        panel.add(right_but5, "cell 2 6, align right, gapy 10");
        panel.add(right_but6, "cell 2 7, align right, gapy 10");
        panel.add(right_but7, "cell 2 8, align right, wrap 135");

        central_panel.setLayout(new MigLayout("debug", "[center,grow]", // Column
                                                                        // constraints
                                                                        // with
                                                                        // default
                                                                        // align
                "[center]0[]"));

        for (int i = 0; i <= 1000; i++) {
            if (i % 10 == 0) {
                central_panel.add(new JButton(), "wrap");
            }
            else {
                central_panel.add(new JButton());
            }
        }

        central_scroll_panel = new JScrollPane(central_panel);

        panel.add(central_scroll_panel, "cell 1 2,span 1 8, grow, w 1175, h 552"); // Ako
        // stavis
        // "span 1 7"
        // tada
        // ide
        // do
        // razine
        // gumbica

        setContentPane(panel);
    }

}

enter image description here