【问题标题】:Java Swing JScrollPane not scrolling/scallingJava Swing JScrollPane 不滚动/缩放
【发布时间】:2016-11-30 13:55:47
【问题描述】:

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

我遇到的问题是,滚动窗格不可滚动,文本只能缩放到极小。

这是下面的代码,我已经尝试过使用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);
}

【问题讨论】:

  • 如果 Container 引用 java.awt.Container,则将每次出现的 new Container 替换为 new JPanel。没有人应该直接实例化 Container。您还应该使用 JPanel 而不是 JLabel 来保存多个组件; JLabel 并不意味着要生孩子,即使它在技术上是允许的。
  • 我将所有new Container 替换为new JPanel,但仍然无法正常工作。
  • 您可能会发现将所有数据放在 JTable 中,然后将 JTable 放在 JScrollPane 中会更容易。
  • 谢谢,它现在可以与 JTable 一起使用。

标签: java swing user-interface scroll scale


【解决方案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 给了我用 JTable 替换它的提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2010-09-22
    相关资源
    最近更新 更多