gpt4 book ai didi

java - 使用update时Jlabel文本相乘和重叠(图形)

转载 作者:行者123 更新时间:2023-11-29 06:53:04 25 4
gpt4 key购买 nike

我编写了这段代码来更新我的进度条进度消息。基本上,当我有在 for() 循环上运行的 length 进程时,我将其放入循环中,并使用 prog = i 和 max = 数组 max 对其进行更新。

for(int i = 1; i <= array.length-1; i++){
//Process
setPrg(this.lbl, ths.prg, i, array.length-1, "Doing so process");
}

-

public void setPrg(JLabel lbl, JProgressBar prg, int prog, int max, String msg) {
prg.setMaximum(max);
if (prog <= max) {
prg.setValue(prog);
}
prg.update(prg.getGraphics());
if (prog >= max) {
setMessage(lbl, "");
} else {
setMessage(lbl, msg);
}
}

public void setMessage(JLabel lbl, String msg) {
lbl.setText(msg);
lbl.update(lbl.getGraphics());
}

我使用多个连续的 for() 循环来执行此操作,每个循环都有描述该过程的唯一消息。

问题是,出于某种原因,标签文本与自身重叠。例如,如果我有一个运行此代码的循环:

setPrg(this.lbl, ths.prg, i, array.length-1, "This is process 1");

还有一个运行这段代码的:

setPrg(this.lbl, ths.prg, i, array.length-1, "The second process this is");

然后,一旦第二个进程开始,两条消息的文本就会重叠,而不是“这是进程 1”消失。

另一方面,如果我不包括这一行:

lbl.update(lbl.getGraphics());

在循环期间根本不会显示任何文本,只有为标签设置的最后一个文本会在循环后显示。

非常感谢帮助!

完整程序如下:

package test;

import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class Main extends javax.swing.JFrame {

public Main() {
initComponents();
}
public void setPrg(JLabel lbl, JProgressBar prg, int prog, int max, String msg) {
prg.setMaximum(max);
if (prog <= max) {
prg.setValue(prog);
}
prg.update(prg.getGraphics());
if (prog >= max) {
setMessage(lbl, "");
} else {
setMessage(lbl, msg);
}
}

public void setMessage(JLabel lbl, String msg) {
lbl.setText(msg);
lbl.update(lbl.getGraphics());
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jProgressBar1 = new javax.swing.JProgressBar();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("Button");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel1.setText("jLabel1");

jTextField1.setText("jTextField1");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(246, 246, 246)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addGap(226, 226, 226)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1)))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 540, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(108, Short.MAX_VALUE)
.addComponent(jButton1)
.addGap(22, 22, 22)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
for (int i = 1; i <= 10000; i++) {
this.setPrg(jLabel1, jProgressBar1, i, 10000, "Message 1");
}
for (int i = 1; i <= 10000; i++) {
this.setPrg(jLabel1, jProgressBar1, i, 10000, "Second Message");
}
}

public static void main(String args[]) {
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}

// Variables declaration - do not modify
public javax.swing.JButton jButton1;
public javax.swing.JLabel jLabel1;
public javax.swing.JProgressBar jProgressBar1;
public javax.swing.JTextField jTextField1;
// End of variables declaration
}

最佳答案

当您使用 Swing 时,您自动处于多线程环境中。一个是主线程,Swing 使用 EDT 进行 UI 重绘。

使用 SwingUtilities.invokeLater() 在 EventDispatchThread 上执行 JProgressBar.setValue()JLabel.setValue()。这可确保它们得到相应更新。

如何使用 SwingUtilities 的示例片段。

    final JProgressBar prg = new JProgressBar();
final JLabel lbl = new JLabel();

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
prg.setValue(70);
lbl.setText("Text");
}
});

您会看到,不再需要调用lbl.update(lbl.getGraphics());

关于java - 使用update时Jlabel文本相乘和重叠(图形),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41144758/

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