Java (Swing):JScrollPane.setBounds() 不适用?
我正在尝试创建一个带有滚动条的简单 JList,因此我需要在 JScrollPane 中包含 JList。到目前为止,一切都很好。但是,由于某种原因,我无法调整 JScrollPane 的大小/位置!?听起来逻辑是它里面的所有东西都应该拉伸到 100%,所以如果我将 JScrollPane 设置为 300px 宽,里面的元素也会如此。这是正确的吗?
当你在做的时候,如果我应该改变一些东西或优化它,请批评并给我提示。
无论如何,这是代码:
package train;
import java.awt.*;
import javax.swing.*;
public class GUI {
private DefaultListModel loggerContent = new DefaultListModel();
private JList logger = new JList(loggerContent);
GUI() {
JFrame mainFrame = new JFrame("title");
this.addToLog("testing testing");
this.addToLog("another test");
// Create all elements
logger = new JList(loggerContent);
JScrollPane logWrapper = new JScrollPane(logger);
logWrapper.setBounds(10, 10, 20, 50);
// Add all elements
mainFrame.add(logWrapper);
// Show everything
mainFrame.setSize(new Dimension(600, 500));
mainFrame.setVisible(true);
}
public void addToLog(String inputString) {
int size = logger.getModel().getSize();
loggerContent.add(size, inputString);
}
}
提前致谢, qwerty
编辑:这是它运行的屏幕截图: https://i.sstatic.net/sLGgQ.png
I'm trying to create a simple JList with a scrollbar, and therefore i need to have the JList within a JScrollPane. So far, so good. However, for some reason i can't resize/position the JScrollPane!? It sounds logic that everything inside it should stretch to 100%, so if i set the JScrollPane to be 300px wide, the elements inside will be as well. Is that correct?
While you're at it, please critisize and give me hints if i should change something or optimize it.
Anyhow, here's the code:
package train;
import java.awt.*;
import javax.swing.*;
public class GUI {
private DefaultListModel loggerContent = new DefaultListModel();
private JList logger = new JList(loggerContent);
GUI() {
JFrame mainFrame = new JFrame("title");
this.addToLog("testing testing");
this.addToLog("another test");
// Create all elements
logger = new JList(loggerContent);
JScrollPane logWrapper = new JScrollPane(logger);
logWrapper.setBounds(10, 10, 20, 50);
// Add all elements
mainFrame.add(logWrapper);
// Show everything
mainFrame.setSize(new Dimension(600, 500));
mainFrame.setVisible(true);
}
public void addToLog(String inputString) {
int size = logger.getModel().getSize();
loggerContent.add(size, inputString);
}
}
Thanks in advance,
qwerty
EDIT: Here's a screenshot of it running: https://i.sstatic.net/sLGgQ.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
的
setVisibleRowCount()
方法JList
对此特别方便,如相关的 教程。ListDemo
< /a> 就是一个很好的例子。附录:
好吧,既然你问了:不要在构造函数中调用公共方法;将它们设为私有或在构造函数完成后调用它们。当
addElement()
可用时,无需查找add()
的最后一个索引。另外,请务必在 事件调度线程 上构建 GUI 。The
setVisibleRowCount()
method ofJList
is particularly convenient for this, as suggested in the relevant tutorial.ListDemo
is a good example.Addendum:
Well, since you ask: Don't invoke public methods in the constructor; make them private or invoke them after the constructor finishes. There's no need to find the last index for
add()
, whenaddElement()
is available. Also, be sure to construct your GUI on the event dispatch thread .界限&组件的大小通常会被忽略,而不是它的首选大小和容器使用的布局的约束。
要解决这个问题,请学习如何使用布局和布局。适当地应用它们。
The bounds & size of a component are generally ignored over that of it's preferred size and the constraints of the layout being used by the container.
To solve this problem, learn how to use layouts & apply them appropriately.
尝试将 JScrollPane 放入 JPanel 中并将面板添加到框架中。
然后设置面板的边界而不是 JScrollpane
问题是 Swing 使用布局管理器来控制子边界属性。将 JScrollpane 直接添加到主框架不允许您正确选择正确的边界。
Try to put your JScrollPane inside a JPanel and add the panel to the frame.
Then set the bounds of the panel instead of the JScrollpane
The probles is that Swing uses layout managers to control child bounds property. Adding a JScrollpane directly to the main frame, doesn't allow you to choose right bounds properly.