简体   繁体   English

动态地将内容添加到java swing jpanel

[英]Add Contents Dynamically to java swing jpanel

I am new to java. 我是Java新手。 I was Developing apps in c# in past. 我过去使用C#开发应用程序。 I am using NetBeans gui designer. 我正在使用NetBeans gui设计器。 I have created a new JFrame in which layout is set to group layout in which there's scrollpane. 我创建了一个新的JFrame,其中的布局设置为其中有滚动窗格的组布局。 I am adding components on runtime by using following code. 我正在使用以下代码在运行时添加组件。

MyScrollPane.add(new javax.swing.JButton("Button1");
MyScrollPane.add(new javax.swing.JCheckBox("CheckBox1");
this.revalidate();

The problem is that these components are not showing in scrollpane. 问题是这些组件没有显示在滚动窗格中。 Another problem is that there is no option for absolute layout. 另一个问题是绝对布局没有选项。 Because I want to generate a scrollable list which will get values from database and generate list at runtime. 因为我想生成一个可滚动的列表,它将从数据库获取值并在运行时生成列表。

NetBeans gui designer leverages GroupLayout to facilitate building of UIs. NetBeans gui设计器利用GroupLayout来简化UI的构建。 Adding components to this layout manager at runtime involves adding horizontal & vertical sequential groups which can be quite complex. 在运行时将组件添加到此布局管理器涉及添加水平和垂直顺序组,这可能非常复杂。 A good reason to start coding using standard layout managers when starting starting out. 开始时使用标准布局管理器开始编码的一个很好的理由。

You shouldn't be adding anything to a JScrollPane (I assume MyScrollPane is a JScrollPane). 您不应该在JScrollPane中添加任何内容(我假设MyScrollPane是JScrollPane)。 Your buttons should be added the JPanel that is the view of the scroll pane. 您的按钮应被添加为JPanel,它是滚动窗格的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM