试图了解Timer对象的工作原理 - JAVA SWING

时间:2015-12-08 20:29:20

标签: java swing timer

我在代码的开头声明了一个Timer对象,在点击JButton时对其进行初始化,但无论我做什么,我都无法阻止它。

在我的代码中,我试图在单击按钮打印时停止计时器,但它不起作用。

这是我的Window.java类:

public class Window extends JFrame{


Panel pan = new Panel();
JPanel container, north,south, west;
public JButton ip,print,cancel,start,ok;
JTextArea timeStep;
JLabel legend;
double time=0;
double temperature=0.0;
Timer chrono;


public static void main(String[] args) {
    new Window();
}

public Window()
{
    System.out.println("je suis là");
    this.setSize(1000,400);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setTitle("Assignment2 - CPU temperature");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    container = new JPanel(new BorderLayout());


    north = new JPanel();
    north.setLayout(new BorderLayout());
    ip = new JButton ("New");
    north.add(ip, BorderLayout.WEST);
    print = new JButton ("Print");
    north.add(print,BorderLayout.EAST);

    JPanel centerPanel = new JPanel();

    centerPanel.add(new JLabel("Time Step (in s): "));
    timeStep = new JTextArea("0.1",1,5);
    centerPanel.add(timeStep);
    start = new JButton("OK");
    ListenForButton lForButton = new ListenForButton();
    start.addActionListener(lForButton);
    ip.addActionListener(lForButton);
    centerPanel.add(start);

    north.add(centerPanel, BorderLayout.CENTER);


    south = new JPanel();
    legend = new JLabel("Legends are here");
    south.add(legend);

    west = new JPanel();
    JLabel temp = new JLabel("°C");
    west.add(temp);

    container.add(north, BorderLayout.NORTH);
    container.add(west,BorderLayout.WEST);
    container.add(pan, BorderLayout.CENTER);
    container.add(south, BorderLayout.SOUTH);

    this.setContentPane(container);
    this.setVisible(true);
}



private class ListenForButton implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==start)
    {

        time=Double.parseDouble(timeStep.getText());
        System.out.println(time);
        chrono = new Timer((int)(1000*time),pan);
        chrono.start();

    }
    if(e.getSource()==ip)
    {
        JPanel options = new JPanel();
        JLabel address = new JLabel("IP Address:");
        JTextField address_t = new JTextField(15);
        JLabel port = new JLabel("Port:");
        JTextField port_t = new JTextField(5);
        options.add(address);
        options.add(address_t);
        options.add(port);
        options.add(port_t);
         int result = JOptionPane.showConfirmDialog(null, options, "Please Enter an IP Address and the port wanted", JOptionPane.OK_CANCEL_OPTION);
         if(result==JOptionPane.OK_OPTION)
         {
             System.out.println(address_t.getText());
             System.out.println(port_t.getText());
         }
    }
    if(e.getSource()==print)
    {
        chrono.stop();
    }

}
}
}

这是我的Panel.java类:

public class Panel extends JPanel implements ActionListener {


int rand;
int lastrand=0;
ArrayList<Integer> randL = new ArrayList<>();
ArrayList<Integer> tL = new ArrayList<>();
int lastT = 0;
Color red = new Color(255,0,0);
Color green = new Color(0,150,0);
Color blue = new Color (0,0,150);
Color yellow = new Color (150,150,0);
int max=0;
int min=0;
int i,k,inc = 0,j;
int total,degr,moyenne;

public Panel()
{
    super();
}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setStroke(new BasicStroke(1.8f));


    g2.drawLine(20, 20, 20, this.getHeight()-50);
    g2.drawLine(20, this.getHeight()-50, this.getWidth()-50, this.getHeight()-50);
    g2.drawLine(20, 20, 15, 35);
    g2.drawLine(20, 20, 25, 35);
    g2.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-45);
    g2.drawLine(this.getWidth()-50, this.getHeight()-50, this.getWidth()-65, this.getHeight()-55);

    if(!randL.isEmpty()){
        g2.setColor(green);
        g2.drawLine(15, this.getHeight()-50-max, this.getWidth()-50,this.getHeight()-50-max);
        g2.setColor(blue);
        g2.drawLine(15, this.getHeight()-50-min, this.getWidth()-50,this.getHeight()-50-min);
        g2.setColor(yellow);
        g2.drawLine(15, this.getHeight()-50-moyenne, this.getWidth()-50,this.getHeight()-50-moyenne);
    }
    for(i = 0; i<tL.size(); i++){
        int temp = randL.get(i);
        int t = tL.get(i);
        g2.setColor(red);
        g2.drawLine(20+t, this.getHeight()-50-temp, 20+t, this.getHeight()-50);
       // Ellipse2D circle = new Ellipse2D.Double();
        //circle.setFrameFromCenter(20+t, this.getHeight()-50, 20+t+2, this.getHeight()-52);

    }
    for(j=0;j<5;j++)
    {
        inc=inc+40;
        g2.setColor(new Color(0,0,0));
        g2.drawLine(18, this.getHeight()-50-inc, 22, this.getHeight()-50-inc);
    }
    inc=0;
    }


@Override
public void actionPerformed(ActionEvent e) {
    rand = (int)(50+((Math.random() * (80))));
    lastT += 60;
    randL.add(rand);
    tL.add(lastT);
    Object obj = Collections.max(randL);
    max = (int) obj;
    Object obj2 = Collections.min(randL);
    min = (int) obj2;
   if(!randL.isEmpty()) {
    degr = randL.get(k);
    total += degr;
    moyenne=total/randL.size();
    //System.out.println(moyenne);
     }
    k++;
    if(randL.size()>=15)
    {
        randL.removeAll(randL);
        tL.removeAll(tL);
        //System.out.println(randL);
        lastT = 0;
        k=0;
        degr=0;
        total=0;
        moyenne=0;
    }

    repaint();
}

}

对不起,这项工作非常麻烦,但如果你成功找到解决方案,我会感谢你! :d

干杯!

0 个答案:

没有答案