英文:
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:
- 你不直接将组件添加到滚动面板(scroll panel)。你需要将组件添加到滚动窗格(viewport)中。
- 只能将一个组件添加到滚动窗格中。
因此,你的代码应该类似于:
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:
- you don't add components directly to the scroll panel. You add a component to the
viewport
of the scroll pane - 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 );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论