JPanel 动态添加组件后立即更新问题

xiao_cs 2011-07-09 03:11:08
如下是测试程序,需要实现的功能是:单击按钮之后,jPanel_main(JPanel)容器中开始自动添加两组按钮,第一组添加完成之后会停3s,但是程序运行后发现每次都是等到最后一个按钮构建完成后才会呈现所有的按钮。其实我想要的就是一个动态添加的效果,add一个,界面就出现一个...

请指点指点,谢谢啦!



public class Demo extends JFrame{

private Container contentPane;

public Demo() {
initSelf();
initComponets();
}

private void initSelf() {
setBounds(216, 70, 850, 600);
setDefaultCloseOperation(3);
setLayout(null);
contentPane = getContentPane();
contentPane.setBackground(Color.WHITE);
try {
setIconImage(ImageIO.read(this.getClass().getResource("/img/360safemini.gif")));
} catch (IOException e1) {
e1.printStackTrace();
}

}

private JScrollPane scroll_main;
private JPanel jPanel_main;

private void initComponets() {
jPanel_main = new JPanel();
jPanel_main.setBounds(0, 0, 600, 472);
jPanel_main.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY,1));
jPanel_main.setBackground(Color.white);
jPanel_main.setLayout(null);
jPanel_main.setVisible(true);

scroll_main = new JScrollPane(jPanel_main);
scroll_main.setBounds(0, 0, 600, 472);
contentPane.add(scroll_main);

JButton button = new JButton("测试");
button.setBounds(622, 12, 160, 50);
contentPane.add(button);
button.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
doSomeThing();
}

});

}

protected void doSomeThing() {
int tempHeight1 = 20;
for (int i = 0; i < 5; i++) {
JButton button = new JButton("测试按钮1-"+i);
button.setBounds(12, tempHeight1, 350, 20);
button.setVisible(true);

jPanel_main.add(button);
jPanel_main.repaint();

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

tempHeight1 = tempHeight1 + 25 ;
}

scroll_main.repaint();
scroll_main.revalidate();
scroll_main.updateUI();

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

for (int i = 0; i < 5; i++) {
JButton button = new JButton("测试按钮2-"+i);
button.setBounds(12, tempHeight1, 350, 20);
jPanel_main.add(button);

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
jPanel_main.repaint();
tempHeight1 = tempHeight1 + 25 ;
}


}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Demo().setVisible(true);;
}
});
}

}

...全文
296 1 收藏 5
写回复
5 条回复
xiao_cs 2011年07月09日
结贴了... 有别的指教的请继续留言。

这个问题最终我采取了变通的解决方法,
org.pushingpixels.substance.api.UiThreadingViolationException: Component creation must be done on Event Dispatch Thread

这个异常是因为substance皮肤引起的,我只好暂时放弃使用这个皮肤,转而使用JTattoo皮肤,这个皮肤也不错,时间紧张只好使用该策略。
回复 点赞
xiao_cs 2011年07月09日
非常感谢你答案,并且这个答案确实实现了我说的效果,分是给定了,您放心。
还想补充请教下您,我的实际程序出异常了(异常信息如下文)
其实原本我的程序里面是要动态创建一下轻量组建,主要是JLabel。之前我是放在SwingWorker中,但是也是抛出异常,提示不能在EDT外创建组建,于是我只能移除SwingWorker,想通过别的方式来实现,就是本帖最开始要测试的内容,您给的方法还是最终在使用的时候还是抛出此类异常。

请您指教一下,方便的话您加QQ(597456314)吧. 在线等你回复 谢谢!


class Task extends SwingWorker<Void, Void>{
@Override
public Void doInBackground() {
//具体创建组件的位置

return null;
}

@Override
public void done() {

}
}





org.pushingpixels.substance.api.UiThreadingViolationException: Component creation must be done on Event Dispatch Thread
at org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities.testComponentCreationThreadingViolation(SubstanceCoreUtilities.java:1921)
at org.pushingpixels.substance.internal.ui.SubstancePanelUI.createUI(SubstancePanelUI.java:57)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:36)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:243)
at javax.swing.UIDefaults.getUI(UIDefaults.java:751)
at javax.swing.UIManager.getUI(UIManager.java:1016)
at javax.swing.JPanel.updateUI(JPanel.java:109)
at javax.swing.JPanel.<init>(JPanel.java:69)
at javax.swing.JPanel.<init>(JPanel.java:92)
at javax.swing.JPanel.<init>(JPanel.java:100)
at com.org.xcs.safeTool.main.PanelOfPrivate$11.run(PanelOfPrivate.java:1003)
UIDefaults.getUI() failed: createUI() failed for javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=] java.lang.reflect.InvocationTargetException
java.lang.Error
at javax.swing.UIDefaults.getUIError(UIDefaults.java:711)
at javax.swing.MultiUIDefaults.getUIError(MultiUIDefaults.java:133)
at javax.swing.UIDefaults.getUI(UIDefaults.java:757)
at javax.swing.UIManager.getUI(UIManager.java:1016)
at javax.swing.JPanel.updateUI(JPanel.java:109)
at javax.swing.JPanel.<init>(JPanel.java:69)
at javax.swing.JPanel.<init>(JPanel.java:92)
at javax.swing.JPanel.<init>(JPanel.java:100)
at com.org.xcs.safeTool.main.PanelOfPrivate$11.run(PanelOfPrivate.java:1003)



[Quote=引用 3 楼 huntor 的回复:]
Java code
protected void doSomeThing() {
(new Thread(){
public void run(){
int tempHeight1 = 20;
for (int i = 0; i < 5; i++) {
JButton but……
[/Quote]
回复 点赞
huntor 2011年07月09日
    protected void doSomeThing() {
(new Thread(){
public void run(){
int tempHeight1 = 20;
for (int i = 0; i < 5; i++) {
JButton button = new JButton("测试按钮1-"+i);
button.setBounds(12, tempHeight1, 350, 20);
button.setVisible(true);

jPanel_main.add(button);
jPanel_main.repaint();

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

tempHeight1 = tempHeight1 + 25 ;
}

scroll_main.repaint();
scroll_main.revalidate();
scroll_main.updateUI();

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

for (int i = 0; i < 5; i++) {
JButton button = new JButton("测试按钮2-"+i);
button.setBounds(12, tempHeight1, 350, 20);
jPanel_main.add(button);

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
jPanel_main.repaint();
tempHeight1 = tempHeight1 + 25 ;
}
}
}).start();

}
回复 点赞
xiao_cs 2011年07月09日
求解,在线等.待
回复 点赞
xiao_cs 2011年07月09日
求解,在线等.待
回复 点赞
发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告