java - 在java中刷新GUI的元素

标签 java swing user-interface

我是 Java 中 GUI 的新手。

例如,我只需要更新 1 个元素(例如,JLabel)。在 Tkinter 中,我会使用类似 root.update()root.update_idletasks() 的东西。我想知道用 swing 制作的应用程序是否存在类似的简单功能。我试过 gui_element.SetVisible(false)gui_element.SetVisible(true) 和类似的东西,但不是很成功。我怀疑 javax.swing.Timer 应该可以工作,但不知道如何工作。

编辑 这是代码。如果您发现其他错误,请告诉我。谢谢

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class sof_sample{

    /**
     * @param args
     */

    private static JFrame frame1 = new JFrame("Fractal Geometry");
    public static JButton quit_button = new JButton("Quit");
    private static JButton run_button = new JButton("Run");
    private static JLabel label1 = new JLabel();
    public static JLabel label_iter = new JLabel(); 
    private static GridLayout layout1 = new GridLayout(2,20);
    private JOptionPane msg1 = new JOptionPane();
    private static int counter;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        AuxClass1 inst1 = new AuxClass1();
        quit_button.addActionListener(inst1);
        run_button.addActionListener(inst1);
        label1.setText("Iteration");
        label1.setVerticalTextPosition(JLabel.CENTER);
        label1.setHorizontalAlignment(JLabel.CENTER);
        label_iter.setText("0");
        label_iter.setBorder(BorderFactory.createLineBorder(Color.BLACK));      
        label_iter.setVerticalTextPosition(JLabel.CENTER);
        label_iter.setHorizontalAlignment(JLabel.CENTER);

        //add widgets to the frame
        frame1.add(label1);
        frame1.add(label_iter);
        frame1.add(run_button);
        frame1.add(quit_button);

        frame1.setLayout(layout1);
        frame1.setLocationRelativeTo(null);
        //frame1.pack();
        frame1.setSize(250, 75);
        frame1.setVisible(true);

    }

}

class AuxClass1 implements ActionListener{
    sof_sample inst2 = new sof_sample();

    public void actionPerformed(ActionEvent event1){

        if (event1.getSource()==inst2.quit_button){
            System.exit(0);

        }
        else{
            for (int i=0;i<20000;i++){

                inst2.label_iter.setText(Integer.toString(i));

            }
            //msg1.showMessageDialog(frame1, "Not yet!");

        }


    }



}

最佳答案

Swing 是事件驱动的。所有组件都通过 MVC 模式工作。您不需要通过隐藏/显示来显式地重新绘制它以更新它在屏幕上的表示。

您只需执行yourLabel.setText("your new text"),新文本就会出现在标签上。

请记住,大多数 GUI 更新(setText 是一个异常(exception))需要在 EDT 上执行,因此如果您需要,请使用 SwingUtilities.invokeLater 等更新由网络消息触发。

如果您对 GUI 进行结构性更改,则必须 revalidate()/repaint()

关于java - 在java中刷新GUI的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12048833/

相关文章:

Java JPanel.add(...) 不工作

java - 使用 swing 菜单和 menuitem 对象调用类

javascript - jQuery 中指示表单完成的进度条

r Shiny : access input fields in UI

java - 当值发生变化时,面板上的组件如何向面板外的其他组件发出警报?

java - 我如何按照创建它的相同顺序解析 map (foreach)(JAVA)

javax.servlet.ServletException : Servlet. 从 "viralpatel.net"运行示例时出现 init() 异常

java - 使用 showMessageDialog 显示数组并返回选定的值

Java:使用透明 JButton 覆盖图像

java - Java中的happens-before机制