JScrollPane组件不显示

huangapple go评论136阅读模式
英文:

JScrollPane components do not appear

问题

我试图将一些组件放入 `JScrollPane`但每次运行程序时它们都不会显示出来我今天刚开始学习关于GUI所以我想可能是我漏掉了一些细节但无论我在网上搜索哪里都找不到答案唯一显示的是 `JScrollPane` 本身

class MainFrame extends JFrame{
    public MainFrame(String title){
        // 主窗口设置
        super(title);
        setSize(655, 480);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        // 布局
        FlowLayout flow = new FlowLayout();
        setLayout(flow);

        // 组件
        JButton spam_button = new JButton("Print");
        JLabel label = new JLabel("What do you want to print?",
                                  JLabel.LEFT);
        JTextField user_input = new JTextField("Type Here", 20);

        // 滚动窗格
        JScrollPane scroll_pane = new JScrollPane(
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroll_pane.setPreferredSize(new Dimension(640, 390));

        // 添加到滚动窗格
        scroll_pane.setViewportView(label);
        scroll_pane.setViewportView(user_input);
        scroll_pane.setViewportView(spam_button);

        // 添加到主窗口
        add(scroll_pane);

        // 可见性
        setVisible(true);
    }
}

这个程序的目的是将您输入的内容打印100次,但我还没有完成,因为我一直卡在这个滚动问题上。当我最终让事物显示在滚动窗格中后,我将把这三个组件移到滚动窗格上方的 JPanel 中,然后我将把100个单词添加到滚动窗格中,以便您可以滚动查看。

英文:

I'm trying to put some components inside of a JScrollPane but every time I launch my program they don't appear. I just started learning about GUIs today so I figure I missed something small but no matter where I look online I can't find an answer. The only thing that appears is the JScrollPane itself.

class MainFrame extends JFrame{
    public MainFrame(String title){
        //Main Frame Stuff
        super(title);
        setSize(655, 480);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

        //Layout
        FlowLayout flow = new FlowLayout();
        setLayout(flow);

        //Components
        JButton spam_button = new JButton("Print");
        JLabel label = new JLabel("What do you want to print?",
                                  JLabel.LEFT);
        JTextField user_input = new JTextField("Type Here", 20);

        //Scroll Pane
        JScrollPane scroll_pane = new JScrollPane(
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroll_pane.setPreferredSize(new Dimension(640, 390));

        //Adding to Scroll
        scroll_pane.add(label);
        scroll_pane.add(user_input);
        scroll_pane.add(spam_button);

        //Adding to the Main Frame
        add(scroll_pane);

        //Visibility
        setVisible(true);
    }
}

The point of the program is to print whatever you type 100 times but I haven't gotten that far yet because I've been hung up on this scroll problem. When I finally get things to show up in the scroll pane I'm going to move those three components to a JPanel above the scroll pane and then I'm going to add the 100 words to the scroll pane so that you can scroll through it.

答案1

得分: 1

我今天刚开始学习关于图形用户界面(GUI)。

因此,首要学习的地方是Swing教程。这里有许多演示代码可供下载、测试和修改。

scrollPane.add(label);
scrollPane.add(user_input);
scrollPane.add(spam_button);

JScrollPane 不同于 JPanel:

  1. 你不直接将组件添加到滚动面板(scroll panel)。你需要将组件添加到滚动窗格(viewport)中。
  2. 只能将一个组件添加到滚动窗格中。

因此,你的代码应该类似于:

JPanel panel = new JPanel();
panel.add(label);
panel.add(user_input);
panel.add(spam_button);
scrollPane.setViewportView(panel);
英文:

> I just started learning about GUIs today

So the first place to start is with the Swing tutorial. There is plenty of demo code to download and test and modify.

scroll_pane.add(label);
scroll_pane.add(user_input);
scroll_pane.add(spam_button);

A JScrollPane is not like a JPanel:

  1. you don't add components directly to the scroll panel. You add a component to the viewport of the scroll pane
  2. only a single component can be added to the viewport.

So your code should be something like:

JPanel panel = new JPanel();
panel.add(label);
panel.add(user_input);
panel.add(spam_button);
scrollPane.setViewportView( panel );

huangapple
  • 本文由 发表于 2020年5月30日 10:57:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/62097260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定