动态 JPanel 添加
我正在尝试创建一个动态 Swing GUI。当我单击添加/删除按钮时,我需要添加/删除 JPanels
。我无法动态添加 JPanels
。最初 JPanel
加载,但 JPanels
数组无法工作。我该怎么做?
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.print.attribute.standard.JobHoldUntil;
import javax.swing.*;
public class AccessoryFileChooser2 extends JFrame {
JFileChooser chooser = null;
JLabel statusbar;
JLabel file;
JCheckBox checkBox;
int count;
int increment;
JPanel [] panel = new JPanel[10]; //array
public AccessoryFileChooser2() {
setSize(350, 200);
count=0;
increment=1;
setDefaultCloseOperation(EXIT_ON_CLOSE);
final Container c = getContentPane();
c.setLayout(new BorderLayout());
checkBox =new JCheckBox("");
String fileName = "Choose File Name";
file = new JLabel(fileName);
final JButton accButton = new JButton("Browse");
final JButton add = new JButton("Add");
final JButton validate = new JButton("Validate");
final JButton delete = new JButton("Delete");
accButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int option = chooser.showOpenDialog(AccessoryFileChooser2.this);
if (option == JFileChooser.APPROVE_OPTION) {
statusbar.setText(
(
chooser.getSelectedFile().getPath()));
}
}
});
statusbar = new JLabel("Output of your selection will go here");
chooser = new JFileChooser();
final JPanel panels =new JPanel();
//JPanel panel2 =new JPanel();
panel[count]=new JPanel();
panel[count].add(checkBox);
panel[count].add(file);
panel[count].add(accButton );
panel[count].add(statusbar);
c.add(panel[count],BorderLayout.CENTER);
panels.add(add);
panels.add(delete);
panels.add(validate);
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
count=count+1;;
increment=increment+1;;
panel[count]=new JPanel();
System.out.println("You clicked the ADD button");
panel[count].add(checkBox);
panel[count].add(file);
panel[count].add(accButton );
panel[count].add(statusbar);
panel[count].revalidate();
panel[count].repaint();
panel[count].updateUI();
c.add(panel[count]);
}
});
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
increment--;
System.out.println("You clicked the Delete button");
System.out.println(checkBox.isSelected());
for (int i = 0; i < panel.length; i++) {
JCheckBox box=(JCheckBox) panel[i].getComponent(0);
if(box.isSelected()){
c.remove(panel[i]);
}
}
}
});
validate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the Validate button");
}
});
c.add(panels,BorderLayout.SOUTH);
}
public static void main(String args[]) {
AccessoryFileChooser2 afc = new AccessoryFileChooser2();
afc.setVisible(true);
}
}
I am trying to create a dynamic Swing GUI. I need to add/delete JPanels
when I click on add/delete button. I am not able to add JPanels
dynamically. Initially JPanel
loads but the array of JPanels
fail to work. How do I do it?
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.print.attribute.standard.JobHoldUntil;
import javax.swing.*;
public class AccessoryFileChooser2 extends JFrame {
JFileChooser chooser = null;
JLabel statusbar;
JLabel file;
JCheckBox checkBox;
int count;
int increment;
JPanel [] panel = new JPanel[10]; //array
public AccessoryFileChooser2() {
setSize(350, 200);
count=0;
increment=1;
setDefaultCloseOperation(EXIT_ON_CLOSE);
final Container c = getContentPane();
c.setLayout(new BorderLayout());
checkBox =new JCheckBox("");
String fileName = "Choose File Name";
file = new JLabel(fileName);
final JButton accButton = new JButton("Browse");
final JButton add = new JButton("Add");
final JButton validate = new JButton("Validate");
final JButton delete = new JButton("Delete");
accButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int option = chooser.showOpenDialog(AccessoryFileChooser2.this);
if (option == JFileChooser.APPROVE_OPTION) {
statusbar.setText(
(
chooser.getSelectedFile().getPath()));
}
}
});
statusbar = new JLabel("Output of your selection will go here");
chooser = new JFileChooser();
final JPanel panels =new JPanel();
//JPanel panel2 =new JPanel();
panel[count]=new JPanel();
panel[count].add(checkBox);
panel[count].add(file);
panel[count].add(accButton );
panel[count].add(statusbar);
c.add(panel[count],BorderLayout.CENTER);
panels.add(add);
panels.add(delete);
panels.add(validate);
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
count=count+1;;
increment=increment+1;;
panel[count]=new JPanel();
System.out.println("You clicked the ADD button");
panel[count].add(checkBox);
panel[count].add(file);
panel[count].add(accButton );
panel[count].add(statusbar);
panel[count].revalidate();
panel[count].repaint();
panel[count].updateUI();
c.add(panel[count]);
}
});
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
increment--;
System.out.println("You clicked the Delete button");
System.out.println(checkBox.isSelected());
for (int i = 0; i < panel.length; i++) {
JCheckBox box=(JCheckBox) panel[i].getComponent(0);
if(box.isSelected()){
c.remove(panel[i]);
}
}
}
});
validate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked the Validate button");
}
});
c.add(panels,BorderLayout.SOUTH);
}
public static void main(String args[]) {
AccessoryFileChooser2 afc = new AccessoryFileChooser2();
afc.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不是不正确,有很多新手错误,
1/因为与您为
JPanels
创建数组的方式相同,您需要为其
JComponents
创建Array
code>,因为一个 JComponent 只能添加一次编辑:部分修改/更改的代码
通过此输出显示此问题到 GUI
2/等等...
不要这样做,如果您多次添加和删除
JPanel[]
,可能会出现很多性能问题... ,GUI 将被冻结或无响应,最好按照建议寻找带有一个
TableColumn
的JTable
此处not that not correct, there are lots of newbie mistakes,
1/ because same way as you created array for
JPanels
you need to create
Array
for itsJComponents
, because one JComponent could be added only once timeEDIT: partial amended/changed code diplayed this issue
with this output to the GUI
2/ and so on ...
don't do it this way, there is possible a lots of issues with performance, if you add and remove
JPanel[]
lots of times ..., GUI would be freeze or will be un-responsiveit would be better to look for
JTable
with oneTableColumn
as is suggested here当从可见 GUI 添加或删除组件时,一般代码应该是:
重新验证基本上调用布局管理器。
重新绘制可确保在布局发生更改时重新绘制组件。
When adding or removing components from a visible GUI the general code should be:
The revalidate basically invoke the layout manager.
The repaint makes sure the components are repainted in case the layout has changed.
在
c.add(panel[count]);
之后添加c.validate()
以更新 GUI。还建议检查当前的
JPanel
数量,因为这样,您很快就会得到ArrayIndexOutOfBoundsException
...Add
c.validate()
afterc.add(panel[count]);
to update the GUI.It is also recommended to check the current number of
JPanel
s, because this way, you will getArrayIndexOutOfBoundsException
pretty fast...我已经取得了成功:
在运行时对 gui 进行更改后。当我创建、删除、移动面板时,我在组件/容器上调用这两个方法。
I have had success with:
after making changes to the gui at run time. When I create, remove, move panels I call these two methods on the component/container.