gpt4 book ai didi

javax.swing.JPanel - 在事件执行期间重新验证

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

我希望我不会用这个问题来打扰 stackoverflow 周围的所有 java 专家,这个问题看起来很愚蠢,但却给我这样的 java 新手带来了很多麻烦。

在我的辩护中,我必须提到,我在谷歌上搜索了这个问题,但在我的水平上找不到任何可以解释我是否可以实现我想要实现的目标(以及最终如何实现)的内容。

我正在解析 xml 并以用户友好的图形形式显示它。我试图显示此 xml 的加载进度,以便用户知道会发生什么。

因此,我创建了带有两个面板的简单表单 - 一个位于顶部,填充整个工作空间,另一个位于底部,充当状态栏的角色。顶部还有一个简单的菜单栏,因此用户可以单击文件 -> 加载来加载文件。我正在 file -> load 菜单按钮的单击事件中加载文件。

因此,当用户单击文件 -> 加载时,会发生以下情况:

JLabel progress_label = new JLabel("0%");
this.status_bar.add(progress_label);
this.status_bar.revalidate();
//some code that loads my xml
//inside loading loop I try to do this:
progress_label.setText(some_formula_to_calculate_percentage);
this.status_bar.revalidate();
//finish loading xml file
this.status_bar.remove(progress_label);
this.status_bar.revalidate();

上面的代码工作正常 - 除了一个小细节。我认为 revalidate() 是在事件触发后执行的,因此在事件执行期间添加标签然后删除标签时,我实际上看不到任何效果。

这是我的问题 - 有没有办法从 Activity 内部重新验证我的面板?

非常感谢您的建议。

<小时/>

编辑 - 根据建议,我准备了一些示例代码来解释我想要做什么。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;

import javax.swing.JLabel;
/**
*
* @author
*/
public class NewJFrame extends javax.swing.JFrame {

/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel1 = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jPanel1MouseClicked(evt);
}
});

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 476, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 358, Short.MAX_VALUE)
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);

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

private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {
JLabel test_label = new JLabel("test");
test_label.setBounds(1, 1, 100, 100);
this.jPanel1.add(test_label);
this.jPanel1.revalidate();
this.jPanel1.repaint();
try
{
System.in.read();
}
catch ( Exception e )
{
}
this.jPanel1.remove(test_label);
this.jPanel1.revalidate();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel jPanel1;
// End of variables declaration
}

最佳答案

I think revalidate() is being executed after event is fired so as label is being added and then removed during event was executed I can't actualy see any effect

听起来您的代码正在 EDT 上执行。所以你最终只能看到最终结果。

阅读 Concurrency 上的 Swing 教程。您可能需要使用 SwingWorker,以便长时间运行的代码在单独的线程上执行,然后发布可以在 EDT 上绘制的结果。

关于javax.swing.JPanel - 在事件执行期间重新验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16310529/

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