我有一個Applet類(擴展JApplet
)。在類內部,我實例化一個JPanel並初始化一個JButton對象setEnabled(true)
。在用戶點擊Panel中的這個按鈕並做了一些處理之後,我調用了JPanel中的一個方法來更新面板。然後我做了setEnabled(false)
點擊JPanel上的按鈕。Java Swing刷新JPanel
但是,在主面板上調用add(ScrollPane)
之後,JPanel不能正確「刷新」。在處理並設置JButton未啓用(並且我確認了正確的數據存在等)之後,JPanel仍處於初始化形式。
換句話說,我需要做什麼才能在applet中的JPanel上調用add(JScrollPane)
實際刷新Panel?
基本上我想知道:如果更新嵌套在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()");
}
}
你能分享你的代碼嗎? – 2011-06-14 10:12:56
「我需要做什麼以便在applet中的JPanel上調用add(JScrollPane)實際刷新Panel?」在應用程序中完全一樣的東西。小程序不適用於新手。 BTW 1)'JPanel' /'Panel'要麼使用正確的類名,要麼不要將Swing與AWT混合使用。 2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)。 – 2011-06-14 10:24:27
我需要做什麼以便在applet中的JPanel上調用add(JScrollPane)實際刷新JPanel? – Satisfaction 2011-06-14 10:31:26