【发布时间】:2011-06-14 10:06:14
【问题描述】:
我有一个 Applet 类(扩展名为 JApplet)。在类中,我正在实例化一个 JPanel 并初始化一个 JButton whit setEnabled(true)。用户在面板中单击此按钮并进行一些处理后,我调用 JPanel 内部的一个方法来更新面板。然后我执行setEnabled(false) JPanel 上单击的按钮。
但是,在我在主面板上调用 add(ScrollPane) 后,JPanel 没有正确“刷新”。在处理并将 JButton 设置为未启用(并且我确认存在正确的数据等)之后,JPanel 仍处于其初始化形式。
换句话说,我需要做什么才能在小程序内的 JPanel 上调用 add(JScrollPane) 真正刷新面板?
基本上我想知道:如果您更新嵌套在 JApplet 内的 swing 组件内的面板,更新是否应该可见?如果没有,需要做什么来刷新?
这是代码:
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (STAMPA_COMMAND.equals(command)) {
stampa.setEnabled(false);
JPanel areaPrint = new JPanel();
JLabel lab = new JLabel("Wait Printing...");
areaPrint.setBackground(Color.magenta);
areaPrint.add(lab);
scrollArea.getViewport().add(areaPrint); // THIS IS THE PROBLEM...THE CHANGE ARE NOT REFRESHED
try {
PrintPdf printPDFFile;
ArrayList assegniDaStampare = new ArrayList();
for (int i = 0; i < assegni.size(); i++) {
DatiAssegno datiAss = (DatiAssegno) assegni.get(i);
if (datiAss != null && datiAss.getStatoAssegno().equals(STATUS_OK)) {
printPDFFile = new PrintPdf("Stampa Assegni", datiAss);
printPDFFile.print();
String servletLocation = "http://localhost/Servlet";
// connect to the servlet
URL studentDBservlet = new URL(servletLocation);
URLConnection servletConnection = studentDBservlet.openConnection();
servletConnection.setUseCaches(false);
servletConnection.setDefaultUseCaches(false);
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
OutputStream outstream = servletConnection.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(datiAss.idAssegno);
oos.flush();
oos.close();
ObjectInputStream inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
try {
String statusSave = (String) inputFromServlet.readObject();
} catch (ClassNotFoundException e4) {
e4.printStackTrace();
}
}
}
JPanel areaPrint2 = new JPanel();
JLabel lab2 = new JLabel("Print Complete");
areaPrint2.setBackground(Color.green);
areaPrint2.add(lab2);
scrollArea.getViewport().add(areaPrint2);
} catch (FileNotFoundException e1) {
//do something
} catch (IOException e2) {
//do something
} catch (PrinterException e3) {
//do something
}
}
if (EXIT_COMMAND.equals(command)) {
JSObject win = JSObject.getWindow(appletParent);
appletParent.stop();
appletParent.destroy();
win.eval("self.close()");
}
}
【问题讨论】:
-
你能分享你的代码吗?
-
“我需要做什么才能在 applet 内的 JPanel 上调用 add(JScrollPane) 来真正刷新面板?”与您在应用程序中所做的完全相同。小程序不适合新手。顺便说一句 1)
JPanel/Panel要么使用正确的类名,要么不要将 Swing 与 AWT 混合使用。 2) 为了尽快获得更好的帮助,请发帖SSCCE。 -
我需要做什么才能在小应用程序内的 JPanel 上调用 add(JScrollPane) 来真正刷新 JPanel?