Java (Swing):JScrollPane.setBounds() 不适用?

发布于 2024-10-26 10:05:49 字数 1278 浏览 4 评论 0原文

我正在尝试创建一个带有滚动条的简单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遮云壑 2024-11-02 10:05:49

setVisibleRowCount() 方法JList 对此特别方便,如相关的 教程ListDemo< /a> 就是一个很好的例子。

附录:

请批评并给我提示......

好吧,既然你问了:不要在构造函数中调用公共方法;将它们设为私有或在构造函数完成后调用它们。当 addElement() 可用时,无需查找 add() 的最后一个索引。另外,请务必在 事件调度线程 上构建 GUI 。

import java.awt.*;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5422160 */
public class ListPanel extends JPanel {

    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);

    ListPanel() {
        list.setVisibleRowCount(5);
    }

    public void append(String inputString) {
        model.addElement(inputString);
    }

    private void init() {
        for (int i = 0; i < 10; i++) {
            this.append("String " + String.valueOf(i));
        }
        JFrame mainFrame = new JFrame("GUI");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane jsp = new JScrollPane(list);
        mainFrame.add(jsp);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ListPanel().init();
            }
        });
    }
}

The setVisibleRowCount() method of JList is particularly convenient for this, as suggested in the relevant tutorial. ListDemo is a good example.

Addendum:

please critisize and give me hints…

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(), when addElement() is available. Also, be sure to construct your GUI on the event dispatch thread .

import java.awt.*;
import javax.swing.*;

/** @see http://stackoverflow.com/questions/5422160 */
public class ListPanel extends JPanel {

    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);

    ListPanel() {
        list.setVisibleRowCount(5);
    }

    public void append(String inputString) {
        model.addElement(inputString);
    }

    private void init() {
        for (int i = 0; i < 10; i++) {
            this.append("String " + String.valueOf(i));
        }
        JFrame mainFrame = new JFrame("GUI");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane jsp = new JScrollPane(list);
        mainFrame.add(jsp);
        mainFrame.pack();
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ListPanel().init();
            }
        });
    }
}
耶耶耶 2024-11-02 10:05:49

界限&组件的大小通常会被忽略,而不是它的首选大小和容器使用的布局的约束。

要解决这个问题,请学习如何使用布局和布局。适当地应用它们。

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.

慕巷 2024-11-02 10:05:49

尝试将 JScrollPane 放入 JPanel 中并将面板添加到框架中。

JPanel panel = new JPanel();
panel.add (logWrapper);
mainFrame.add(panel);

然后设置面板的边界而不是 JScrollpane

panel.setBounds(10, 10, 20, 50);

问题是 Swing 使用布局管理器来控制子边界属性。将 JScrollpane 直接添加到主框架不允许您正确选择正确的边界。

Try to put your JScrollPane inside a JPanel and add the panel to the frame.

JPanel panel = new JPanel();
panel.add (logWrapper);
mainFrame.add(panel);

Then set the bounds of the panel instead of the JScrollpane

panel.setBounds(10, 10, 20, 50);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文