简体   繁体   English

如何刷新 JFrame

[英]How to Refresh a JFrame

so I want to make some sort of Tic tac toe, and I'm not sure if the rest of my code is correct (I'm obviously not done yet) but I think that the Problem is that the JFrame doesn't Refresh and I don't know how to do that so how to Refresh a frame?所以我想做某种 Tic tac toe,我不确定我的其余代码是否正确(我显然还没有完成)但我认为问题在于JFrame没有刷新和我不知道该怎么做,所以如何刷新帧?

package pack1;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Gui {


public  Gui() {
     Var.jf = new JFrame();
      Var.jf.setSize(Var.screenwidth, Var.screenheight);

      Var.button1 = new JButton(Var.arr[Var.a]);
      Var.jf.add(Var.button1);
      Var.button2 = new JButton(Var.arr[Var.b]);
      Var.jf.add(Var.button2);
      Var.button3 = new JButton(Var.arr[Var.c]);
      Var.jf.add(Var.button3);
      Var.button4 = new JButton(Var.arr[Var.d]);
      Var.jf.add(Var.button4);
      Var.button5 = new JButton(Var.arr[Var.e]);
      Var.jf.add(Var.button5);
      Var.button6 = new JButton(Var.arr[Var.f]);
      Var.jf.add(Var.button6);
      Var.button7 = new JButton(Var.arr[Var.g]);
      Var.jf.add(Var.button7);
      Var.button8 = new JButton(Var.arr[Var.h]);
      Var.jf.add(Var.button8);
      Var.button9 = new JButton(Var.arr[Var.i]);
      Var.jf.add(Var.button9);
      Var.jf.setLayout(new GridLayout(3,3,10,10));
      Var.button1.addActionListener(new KeyListiner());

      Var.jf.setTitle("Tic tac toe");
      Var.jf.setVisible(true);
      Var.jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Var.jf.setLayout(null);
      Var.jf.setLocationRelativeTo(null);
      Var.jf.setResizable(false);
   }
}

 package pack1;

 import javax.swing.*;

 public class Var {
 static JFrame jf;
 static JButton button1;
 static JButton button2;
 static JButton button3;
 static JButton button4;
 static JButton button5;
 static JButton button6;
 static JButton button7;
 static JButton button8;
 static JButton button9;
 static  int a = 2;
 static  int b = 2;
 static int c = 2;
 static int d = 2;
 static int e = 2;
 static int f = 2;
 static int g = 2;
 static int h = 2;
 static int i = 2;

 static int screenwidth = 900, screenheight = 900;
 static String[]arr = new String[3];{

 arr[0] = "X";
 arr[1] = "O";
 arr[2] = " ";
 }}

package pack1;

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

public class KeyListiner implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==Var.button1);
    
     
     System.out.println(Var.a);
     Var.a= 0 ;
     
}
}

If I didn't explained something correctly or something like this just tell me.如果我没有正确解释某些事情或类似的事情,请告诉我。

You may have several buttons but ony one of them has a keylistener.您可能有多个按钮,但其中任何一个都有一个键侦听器。 That means you would have to move the focus to the one button and press a key.这意味着您必须将焦点移到一个按钮上并按下一个键。 Tricky as the UI does not indicate such a limitation.棘手,因为 UI 并不表示这种限制。

Try to take this with an event-driven approach.尝试用事件驱动的方法来解决这个问题。 Attach an ActionListener to every button.将 ActionListener 附加到每个按钮。 Inside this actionlistener you change the board as required, then call the repaint() method (that does not really repaint but mark this component to be repainted), and EXIT your method.在这个 actionlistener 中,您可以根据需要更改板,然后调用 repaint() 方法(它不会真正重绘,而是标记要重绘的组件),然后退出您的方法。 Unless you exit the method which is running on the Event Dispatcher Thread no UI updates will happen.除非您退出在 Event Dispatcher Thread 上运行的方法,否则不会发生 UI 更新。

If you want to continue doing your stuff while the UI updates you need to create your own Thread.如果你想在 UI 更新时继续做你的事情,你需要创建自己的线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM