使用用户指定的计时器更新 gui 中的 JPanel 和属性? [英] Update JPanel and attributes in a gui, with a user-specified timer?

查看:17
本文介绍了使用用户指定的计时器更新 gui 中的 JPanel 和属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这门课.我们将其称为 ProcessBox.我有另一个名为 Process 的类,它有一个 ProcessBox 作为成员变量.然后,在我的主要方法中,我有一个 Process 对象的 ArrayList(每个都有自己的 ProcessBox).我将如何去做,以便我的 ProcessBox 会每 x ms 更新一次(其中 x 是用户指定的.我已经有了用于输入 x 的监听器完成).下面是我的 ProcessBox 类.这就是我希望每 x ms 重绘一次(除了它们的整个列表).

So I have this class. We'll call it ProcessBox. I have another class called Process that has a ProcessBox as a member variable. Then, in my main method I have an ArrayList of Process objects (each having their own ProcessBox). How would I go about making it so that my ProcessBox would update every x ms (where x is user specified. I already have the listener for inputting x done). Below is my ProcessBox class. This is what I wish to redraw every x ms (except a whole list of them).

import java.awt.*;

import javax.swing.*;
import javax.swing.border.LineBorder;

import java.util.*;

public class ProcessBox extends JPanel {
    GridBagLayout gbc_panel;

    public ProcessBox(Process p) {
        super(new GridBagLayout());
        gbc_panel = new GridBagLayout();
        gbc_panel.columnWidths = new int[] { 0, 0, 0 };
        gbc_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
        gbc_panel.columnWeights = new double[] { 0, 0, Double.MIN_VALUE };
        gbc_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
                Double.MIN_VALUE };
        super.setLayout(gbc_panel);


        setPreferredSize(new Dimension(110, 110));
        setMinimumSize(new Dimension(110, 110));
        setAlignmentX(Component.LEFT_ALIGNMENT);
        setBorder(new LineBorder(new Color(0, 0, 0), 1));
        setSize(new Dimension(110, 110));

        String name = p.getName();
        int priority = p.getPriority();
        int minPriority = p.getMinPriority();
        ArrayList<Integer> times = p.getTime();
        Random r = new Random(System.currentTimeMillis());
        setBackground(new Color(r.nextInt(255 - 210) + 210,
                r.nextInt(255 - 210) + 210, r.nextInt(255 - 210) + 210));

        JLabel lblNewLabel = new JLabel("ID:");
        GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
        gbc_lblNewLabel.gridheight = 2;
        gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel.gridx = 0;
        gbc_lblNewLabel.gridy = 0;
        add(lblNewLabel, gbc_lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel(name);
        GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
        gbc_lblNewLabel_1.gridheight = 2;
        gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 0);
        gbc_lblNewLabel_1.gridx = 1;
        gbc_lblNewLabel_1.gridy = 0;
        add(lblNewLabel_1, gbc_lblNewLabel_1);

        JLabel lblNewLabel_2 = new JLabel("Priority:");
        GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
        gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_2.gridx = 0;
        gbc_lblNewLabel_2.gridy = 2;
        add(lblNewLabel_2, gbc_lblNewLabel_2);

        JLabel lblNum = new JLabel(Integer.toString(priority));
        GridBagConstraints gbc_lblNum = new GridBagConstraints();
        gbc_lblNum.insets = new Insets(0, 0, 5, 0);
        gbc_lblNum.gridx = 1;
        gbc_lblNum.gridy = 2;
        add(lblNum, gbc_lblNum);

        JLabel lblNewLabel_3 = new JLabel("Min:");
        GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
        gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
        gbc_lblNewLabel_3.gridx = 0;
        gbc_lblNewLabel_3.gridy = 3;
        add(lblNewLabel_3, gbc_lblNewLabel_3);

        JLabel lblMp = new JLabel(Integer.toString(minPriority));
        GridBagConstraints gbc_lblMp = new GridBagConstraints();
        gbc_lblMp.insets = new Insets(0, 0, 5, 0);
        gbc_lblMp.gridx = 1;
        gbc_lblMp.gridy = 3;
        add(lblMp, gbc_lblMp);

        JLabel lblTimeSlice = new JLabel("Slice:");
        GridBagConstraints gbc_lblTimeSlice = new GridBagConstraints();
        gbc_lblTimeSlice.insets = new Insets(0, 0, 0, 5);
        gbc_lblTimeSlice.gridx = 0;
        gbc_lblTimeSlice.gridy = 4;
        add(lblTimeSlice, gbc_lblTimeSlice);

        JLabel lblLeft = new JLabel(Integer.toString(times.get(0)));
        GridBagConstraints gbc_lblLeft = new GridBagConstraints();
        gbc_lblLeft.gridx = 1;
        gbc_lblLeft.gridy = 4;
        add(lblLeft, gbc_lblLeft);
        setVisible(true);

}

}

推荐答案

如何使用 Swing 计时器 并查看 例子,例如MarqueeTestAnimationTest.

这篇关于使用用户指定的计时器更新 gui 中的 JPanel 和属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆