将JScrollPane添加到JPanel

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

我要创建此界面.我的JScrollPane有问题:

I have this interface to create. I have a problem with the JScrollPane:

我声明了一个带有Gridlayout(8,1,0,2)的JPanel,我希望该面板中出现8行. 一行是一个JPanel,我设置大小以使8行面板出现在大面板中. 如果行数超过8,我将得到两列... 我添加了一个JScrollPane,但是它没有出现. 测试"按钮位于按钮位置,出现滚动窗格,但返回到面板则消失.

I declared a JPanel with a Gridlayout(8,1,0,2), I want 8 rows appear in this panel. A row is a JPanel to, I set the size to make the 8 row panels appear in the big panel. If the number of rows pass 8, I get two columns ... I added a JScrollPane but it doesn't appear. Testing button at the place of button, the scrollpane appear but returning to panel it disappear..

我该怎么办??

推荐答案

我找到了解决方案:

package d06.m03; import java.awt.BorderLayout; import java.awt.Component; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JScrollPane; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.SystemColor; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import javax.swing.BoxLayout; public class ActionExample4 extends JFrame { /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { ActionExample4 frame = new ActionExample4(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public ActionExample4() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 778, 426); getContentPane().setLayout(null); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(10, 101, 742, 276); //scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); getContentPane().add(scrollPane); JPanel borderlaoutpanel = new JPanel(); scrollPane.setViewportView(borderlaoutpanel); borderlaoutpanel.setLayout(new BorderLayout(0, 0)); JPanel columnpanel = new JPanel(); borderlaoutpanel.add(columnpanel, BorderLayout.NORTH); columnpanel.setLayout(new GridLayout(0, 1, 0, 1)); columnpanel.setBackground(Color.gray); for(int i=0;i<32;i++) { JPanel rowPanel = new JPanel(); rowPanel.setPreferredSize(new Dimension(300,30)); columnpanel.add(rowPanel); rowPanel.setLayout(null); JButton button = new JButton("New button"); button.setBounds(20, 5, 89, 23); rowPanel.add(button); if(i%2==0) rowPanel.setBackground(SystemColor.inactiveCaptionBorder); } } }

更多推荐

将JScrollPane添加到JPanel

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

发布评论

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

>www.elefans.com

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