将JPanel添加到JScrollPane

编程入门 行业动态 更新时间:2024-10-28 15:25:54
本文介绍了将JPanel添加到JScrollPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个gui,它的Panel包含一系列标签和TextField,并使用弹性布局(这是mainPanel),而另一个Panel仅包含button(buttonPanel).我正在尝试使我的mainPanel也具有垂直滚动条.我想实现我的GUI,以便在JFrame中有2个面板. mainPanel出现在框架的顶部,而buttonPanel出现在mainPanel的下方.

I have a gui which has a Panel that contains a sequence of labels and TextFields and uses a spring layout(this is the mainPanel) and another Panel that just contains a button(buttonPanel). I am trying to make my mainPanel to have a vertical scrollbar as well. I would like to implement my GUI such that within the JFrame I have 2 panels. The mainPanel appears on the top of the frame and the buttonPanel appears below the mainPanel.

我的问题是我无法使面板出现,使得buttonPanel在mainPanel下方,并且我也不确定如何向mainPanel添加滚动条.任何帮助,将不胜感激.

My problem is I am not able to make the Panels appear such that the buttonPanel is below the mainPanel and I am also not sure how to add a scrollbar to the mainPanel. Any help would be appreciated.

我能够解决有关JPanels的问题,现在唯一的问题是我无法使mainPanel滚动.我在下面添加了我的最新代码:

EDIT : I was able to solve my issue regarding the JPanels, now my only problem is that I cant get my mainPanel to scroll. I've added my most recent code below :

这是我到目前为止的代码:

Here is the code I have so far:

public static void main(String args[]) { JFrame frame = new JFrame("SpringLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JScrollPane scroll = new JScrollPane(); Container contentPane = frame.getContentPane(); JButton next = new JButton("Next"); JPanel buttonPanel = new JPanel(); buttonPanel.add(next); SpringLayout layout = new SpringLayout(); JPanel mainPanel = new JPanel(); mainPanel.setLayout(layout); contentPane.setLayout(new BorderLayout()); int j = 25; for(int i =0;i<150;i++){ JLabel label = new JLabel("Enter Name " + i ); JTextField text = new JTextField(15); mainPanel.add(label); mainPanel.add(text); layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, contentPane); layout.putConstraint(SpringLayout.NORTH, label, j, SpringLayout.NORTH, contentPane); layout.putConstraint(SpringLayout.NORTH, text, j, SpringLayout.NORTH, contentPane); layout.putConstraint(SpringLayout.WEST, text, 20, SpringLayout.EAST, label); j+=30; } //mainPanel.setSize(500,800); scroll.setPreferredSize(new Dimension(500,500)); scroll.setViewportView(mainPanel); contentPane.add(scroll); contentPane.add(buttonPanel,BorderLayout.SOUTH); //mainWindow.add(contentPane); frame.setSize(500, 600); frame.setVisible(true); }

推荐答案

要使其可滚动,我只需要增大mainPanel的首选大小,使其大于滚动条即可.

To make it scrollable I just needed to increase the preferred size of my mainPanel such that it would be bigger than the scrollbar.

public static void main(String args[]) { JFrame frame = new JFrame("SpringLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JScrollPane scroll = new JScrollPane(); Container contentPane = frame.getContentPane(); JButton next = new JButton("Next"); JPanel buttonPanel = new JPanel(); buttonPanel.add(next); SpringLayout layout = new SpringLayout(); JPanel mainPanel = new JPanel(); mainPanel.setLayout(layout); contentPane.setLayout(new BorderLayout()); int j = 25; for(int i =0;i<18;i++){ JLabel label = new JLabel("Enter Name " + i ); JTextField text = new JTextField(15); mainPanel.add(label); mainPanel.add(text); layout.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, contentPane); layout.putConstraint(SpringLayout.NORTH, label, j, SpringLayout.NORTH, contentPane); layout.putConstraint(SpringLayout.NORTH, text, j, SpringLayout.NORTH, contentPane); layout.putConstraint(SpringLayout.WEST, text, 20, SpringLayout.EAST, label); j+=30; } mainPanel.setPreferredSize(new Dimension(mainPanel.getWidth(), 1500)); scroll.setPreferredSize(new Dimension(500,500)); scroll.setViewportView(mainPanel); contentPane.add(scroll); contentPane.add(buttonPanel,BorderLayout.SOUTH); //mainWindow.add(contentPane); frame.setSize(500, 600); frame.setVisible(true); }

更多推荐

将JPanel添加到JScrollPane

本文发布于:2023-08-06 03:30:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309765.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JPanel   JScrollPane

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!