我必须查询数据库以获取要写入的数据JLabel并加速打开对话框
JLabel
我没有创建JLabel任何文本,并在完成SwingWorker处理后设置了文本
SwingWorker
但是JLabel文字没有更新
有什么办法可以做到这一点
这是我对话框的src代码:
package com.caisse.caisseFrame.dialogs; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Image; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.SwingWorker; import com.caisse.ConnectDB; import com.caisse.CustomComponent.JButtonCustom; import com.caisse.CustomComponent.JPanelCustom; import com.caisse.caisseFrame.CaisseFrame; import com.soqop.caisse.dao.TicketDAO; public class LectureXDialog extends JDialog { /** * */ private static final long serialVersionUID = 1L; JButton ok_btn; TicketDAO ticketDAO; public LectureXDialog(final CaisseFrame parent) { setAlwaysOnTop(true); setSize(new Dimension(300, 280)); setLocationRelativeTo(parent); setTitle("Lecture X"); setVisible(true); setModalityType(ModalityType.DOCUMENT_MODAL); parent.setEnabled(false); ticketDAO = new TicketDAO(ConnectDB.getInstance()); final JPanelCustom componentsPanel = new JPanelCustom(); GridBagLayout componentsPanelLayout = new GridBagLayout(); componentsPanel.setLayout(componentsPanelLayout); final JLabel lectureXlabel = new JLabel(); lectureXlabel.setFont(new Font("Cambria", Font.BOLD, 24)); lectureXlabel.setPreferredSize(new Dimension(250, 55)); lectureXlabel.setMinimumSize(new Dimension(250, 55)); GridBagConstraints gbc_lectureXlabel = new GridBagConstraints(); gbc_lectureXlabel.insets = new Insets(2, 2, 2, 2); gbc_lectureXlabel.gridx = 0; gbc_lectureXlabel.gridy = 0; gbc_lectureXlabel.anchor = GridBagConstraints.CENTER; componentsPanel.add(lectureXlabel, gbc_lectureXlabel); ok_btn = new JButtonCustom("ok"); ok_btn.setFont(new Font("Cambria", Font.BOLD, 24)); try { Image img = ImageIO.read(getClass().getResource("/resources/ok.png")); ok_btn.setIcon(new ImageIcon(img)); } catch (IOException e1) { e1.printStackTrace(); } ok_btn.setPreferredSize(new Dimension(150, 55)); ok_btn.setMinimumSize(new Dimension(150, 55)); ok_btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { parent.setEnabled(true); parent.setFocus(); LectureXDialog.this.dispose(); } }); GridBagConstraints gbc_ok_btn = new GridBagConstraints(); gbc_ok_btn.insets = new Insets(2, 2, 2, 2); gbc_ok_btn.gridx = 0; gbc_ok_btn.gridy = 1; componentsPanel.add(ok_btn, gbc_ok_btn); add(componentsPanel); addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent windowEvent) { parent.setEnabled(true); parent.setFocus(); LectureXDialog.this.dispose(); } }); SwingWorker worker = new SwingWorker<Boolean, Void>() { String text = ""; @Override public Boolean doInBackground() { text = "<html><center>Lecture X : " + ticketDAO.getLectureX() + "</center></html>"; return true; } @Override public void done() { lectureXlabel.setText(text); lectureXlabel.revalidate(); lectureXlabel.repaint(); componentsPanel.revalidate(); componentsPanel.repaint(); getContentPane().revalidate(); getContentPane().repaint(); } }; } }
为了使您Swing Worker能够进行后台工作,您需要调用SwingWorker#execute()方法。如果您不添加它,那么…它将不知道何时开始工作。就像其他任何变量一样:
Swing Worker
SwingWorker#execute()
JLabel label = new JLabel("Hello world");
但是,如果您从未将其添加label到任何内容(或未ActionListener添加到JButton),则将永远不会添加/执行该内容。
label
ActionListener
JButton
worker.execute();
在构造函数内部或您希望其开始工作的任何地方。
根据@MadProgrammer的评论:
请记住,SwingWorker可以返回数据,因此Boolean可以String使用方法实例字段返回,而不是返回,而是可以get在done方法中返回并使用它来获取工作程序的返回值,该方法更安全,更实用。
Boolean
String
get
done
他指的是这样更改您的工人的代码:
SwingWorker worker = new SwingWorker<Boolean, Void>() { @Override public String doInBackground() { return "<html><center>Lecture X : " + ticketDAO.getLectureX() + "</center></html>"; } @Override public void done() { lectureXlabel.setText(get()); lectureXlabel.revalidate(); lectureXlabel.repaint(); componentsPanel.revalidate(); componentsPanel.repaint(); getContentPane().revalidate(); getContentPane().repaint(); } };