JScrollPane组件不显示

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

JScrollPane components do not appear

问题

  1. 我试图将一些组件放入 `JScrollPane` 但每次运行程序时它们都不会显示出来我今天刚开始学习关于GUI所以我想可能是我漏掉了一些细节但无论我在网上搜索哪里都找不到答案唯一显示的是 `JScrollPane` 本身
  2. class MainFrame extends JFrame{
  3. public MainFrame(String title){
  4. // 主窗口设置
  5. super(title);
  6. setSize(655, 480);
  7. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  8. // 布局
  9. FlowLayout flow = new FlowLayout();
  10. setLayout(flow);
  11. // 组件
  12. JButton spam_button = new JButton("Print");
  13. JLabel label = new JLabel("What do you want to print?",
  14. JLabel.LEFT);
  15. JTextField user_input = new JTextField("Type Here", 20);
  16. // 滚动窗格
  17. JScrollPane scroll_pane = new JScrollPane(
  18. ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
  19. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  20. scroll_pane.setPreferredSize(new Dimension(640, 390));
  21. // 添加到滚动窗格
  22. scroll_pane.setViewportView(label);
  23. scroll_pane.setViewportView(user_input);
  24. scroll_pane.setViewportView(spam_button);
  25. // 添加到主窗口
  26. add(scroll_pane);
  27. // 可见性
  28. setVisible(true);
  29. }
  30. }

这个程序的目的是将您输入的内容打印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.

  1. class MainFrame extends JFrame{
  2. public MainFrame(String title){
  3. //Main Frame Stuff
  4. super(title);
  5. setSize(655, 480);
  6. setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  7. //Layout
  8. FlowLayout flow = new FlowLayout();
  9. setLayout(flow);
  10. //Components
  11. JButton spam_button = new JButton("Print");
  12. JLabel label = new JLabel("What do you want to print?",
  13. JLabel.LEFT);
  14. JTextField user_input = new JTextField("Type Here", 20);
  15. //Scroll Pane
  16. JScrollPane scroll_pane = new JScrollPane(
  17. ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
  18. ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  19. scroll_pane.setPreferredSize(new Dimension(640, 390));
  20. //Adding to Scroll
  21. scroll_pane.add(label);
  22. scroll_pane.add(user_input);
  23. scroll_pane.add(spam_button);
  24. //Adding to the Main Frame
  25. add(scroll_pane);
  26. //Visibility
  27. setVisible(true);
  28. }
  29. }

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教程。这里有许多演示代码可供下载、测试和修改。

  1. scrollPane.add(label);
  2. scrollPane.add(user_input);
  3. scrollPane.add(spam_button);

JScrollPane 不同于 JPanel:

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

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

  1. JPanel panel = new JPanel();
  2. panel.add(label);
  3. panel.add(user_input);
  4. panel.add(spam_button);
  5. 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.

  1. scroll_pane.add(label);
  2. scroll_pane.add(user_input);
  3. 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:

  1. JPanel panel = new JPanel();
  2. panel.add(label);
  3. panel.add(user_input);
  4. panel.add(spam_button);
  5. 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:

确定