gpt4 book ai didi

Java Swing JScrollPane 不滚动/缩放

转载 作者:行者123 更新时间:2023-11-30 07:01:13 25 4
gpt4 key购买 nike

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

我遇到的问题是,滚动 Pane 不可滚动,并且文本只能缩放得极小。

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);
}

最佳答案

用 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 替换它。

关于Java Swing JScrollPane 不滚动/缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40890228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com