gpt4 book ai didi

java - 打开 jdialog 后更新 jlabel 文本

转载 作者:行者123 更新时间:2023-12-02 03:02:29 25 4
gpt4 key购买 nike

我必须查询数据库以获取要在 JLabel 上写入的数据并加速打开对话框

我创建了没有文本的 JLabel ,并在完成 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()方法。如果你不添加它,那么......它将不知道什么时候开始工作。它就像任何其他变量一样:

JLabel label = new JLabel("Hello world");

但是,如果您从未将 label 添加到任何内容(或者像 ActionListener 未添加到 JButton 中),则它永远不会被添加/已执行。

worker.execute();

在构造函数内部或任何您希望它开始工作的地方。

<小时/>

根据 @MadProgrammer 的评论:

Remember, SwingWorker can return data, so instead of returning Boolean, you could return String and use get in the done method to get the returns of the worker, somewhat safer and more usable then using an instance field

他指的是像这样更改你的 worker 的代码:

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();
}
};

关于java - 打开 jdialog 后更新 jlabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42256369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com