Java Swing JScrollPane没有滚动/ scalling

时间:2016-11-30 13:55:47

标签: java swing user-interface scroll scale

我正在写一个小原型,它应该显示一些带有文本的JLabel,并且整个站点应该是可滚动的。

我遇到的问题是,滚动窗格不可滚动,文字只是极小的。

Scrolling not working

这是以下代码,我已尝试使用preferredSize和setSize,但没有任何效果。

public History(Controller c, Model m) {        
            new HistoryHandler(this.history).save();

    this.setLayout(new BorderLayout());

    this.history = new ArrayList<>();

    this.history = new ArrayList<>();
    this.history = new HistoryHandler(this.history).load();

    this.label = new JLabel();
    Container c1 = new Container();
    c1.setLayout(new GridLayout(this.history.size(),1));
    Container c2 = new Container();
    c2.setLayout(new GridLayout(this.history.size(),1));
    Container c3 = new Container();
    c3.setLayout(new GridLayout(this.history.size(),1));
    Container c4 = new Container();
    c4.setLayout(new GridLayout(this.history.size(),1));
    Container c5 = new Container();
    c5.setLayout(new GridLayout(this.history.size(),1));
    Container c6 = new Container();
    c6.setLayout(new GridLayout(this.history.size(),1));

    JLabel c0 = new JLabel();
    c0.setLayout(new GridLayout(1,6));
    for (int i = 0; i < this.history.size(); i++) {
        c1.add(new JLabel(this.history.get(i).getUser()));
        c2.add(new JLabel(this.history.get(i).getDate()));
        c3.add(new JLabel(this.history.get(i).getFilesize()));
        c4.add(new JLabel(this.history.get(i).getFilename()));
        c5.add(new JLabel(this.history.get(i).getMessage()));
        c6.add(new JLabel(""+this.history.get(i).isAccepted()));

    }
    this.label.setLayout(new BorderLayout());
    c0.add(c2);
    c0.add(c1);
    c0.add(c4);
    c0.add(c3);
    c0.add(c6);
    c0.add(c5);
    this.label.add(c0, BorderLayout.CENTER);

    this.scroll = new JScrollPane(this.label, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    this.add(this.scroll);

    this.setVisible(true);
}

1 个答案:

答案 0 :(得分:1)

使用JTable替换Container和JLabel,现在可以正常工作。

String[] columnNames = {
            this.translation.getTranslation("date"), 
            this.translation.getTranslation("user"),
            this.translation.getTranslation("filename"),
            this.translation.getTranslation("filesize"),
            this.translation.getTranslation("isAccepted"),
            this.translation.getTranslation("message")
    };

    Object[][] data = new Object[this.history.size()][6];

    for (int i = 0; i < this.history.size(); i++) {
        for (int j = 0; j < 6; j++) {
            if (j == 0) data[i][j] = this.history.get(i).getDate();
            if (j == 1) data[i][j] = this.history.get(i).getUser();
            if (j == 2) data[i][j] = this.history.get(i).getFilename();
            if (j == 3) data[i][j] = this.history.get(i).getFilesize();
            if (j == 4) data[i][j] = this.history.get(i).isAccepted();
            if (j == 5) data[i][j] = this.history.get(i).getMessage();
        }
    }

    JTable table = new JTable(data, columnNames);

    this.scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    this.add(this.scroll);

感谢@VGR给我tipp用JTable替换它。