【发布时间】:2018-06-20 07:13:28
【问题描述】:
我已经上传了我的代码的特定部分,我希望这足以理解我的问题。我在具有多个按钮的框架中使用p6 面板,其中一个按钮显示此面板。
此面板p6 有两个按钮。单击check_api,程序需要一些时间来执行,对用户来说,好像应用程序已经挂起。因此,我尝试添加一个在按钮忙时显示等待消息的方法。但是这样做时,我的面板会失去用户定义的大小。
有没有办法在不影响面板尺寸的情况下显示等待消息?
public static void prepare_url(JPanel p6, JPanel content, int count) {
InnerOperations.removeexcept(6, content);
if (count != 1) {
p6.removeAll();
p6.revalidate();
System.out.println("Count is greater than 1");
}
JPanel p_back = new JPanel();
JTextField field = new JTextField(" Enter url(s) with delimiter ';'");
JButton check_list = new JButton(" CHECK URL LIST STATUS");
JButton check_api = new JButton(" CHECK PREDEFINED API LIST");
p_back.add(check_list);
p_back.add(field);
p_back.add(l_check);
p_back.add(check_api);
check_api.setBounds(200, 130, 400, 30);
l_check.setBounds(50, 300, 600, 20);
field.setBounds(200, 50, 400, 30);
check_list.setBounds(200, 90, 400, 30);
p6.add(p_back);
p_back.setBounds(0, 0, 800, 600);
check_list.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String url_pref
String list[] = url_pref.split(";");
int i = 0;
while (i < list.length) {
try {
// thread1.start();
prepareurl(p6, p_back, list, list.length);
// thread1.stop();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
i++;
}
}
});
check_api.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// thread2.start();
// p_back.removeAll();
//check_api.setText("Will load in a while");
prepare_api(p6, content, p_back);
// thread2.stop();
}
});
}
public static void userwait(JPanel ppanel)
{
JLabel waiting = new JLabel();
ppanel.add(waiting);
waiting.setText("Please Wait......");
waiting.setBounds(200, 200, 400, 30);
}
【问题讨论】:
-
1) 为了尽快获得更好的帮助,请发帖 minimal reproducible example 或 Short, Self Contained, Correct Example。 2) Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
-
谢谢你们!一定会使用您的建议。
标签: java swing jpanel event-dispatch-thread