簡體   English   中英

按下按鈕時,Java 無法獲得重新繪制的繪制方法

[英]Java can't get paint method to repaint when a button is pressed

我目前正在制作搖滾剪刀游戲。 在編碼的早期我遇到了一個問題,當按下按鈕時我無法讓代碼重新繪制新的東西,盡管按鈕正在工作並由代碼注冊。 這些按鈕確實可以正常工作,因為它會打印出正在按下的內容,並且 y 的值也發生了變化,因此代碼在某種程度上可以正常工作,但它沒有正確執行。

public class RockPaperScissors implements ActionListener {
 JButton rock =new JButton("Select rock");  
 JButton paper =new JButton("Select paper");  
 JButton scissors =new JButton("Select scissors");  


public void frame() {
     Gui gui = new Gui();  
     JFrame b = new JFrame("Rock paper scissors");
     rock.setBounds(150,100,120,30);  
     paper.setBounds(350,100,120,30);  
     scissors.setBounds(550,100,120,30);
     b.setSize(905,705);
     b.setLocation(300,60);
     b.setResizable(false);
     b.setVisible(true);
     b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
     b.setResizable(false);
     b.setVisible(true);
     b.add(rock);
     b.add(paper);
     b.add(scissors);
     rock.addActionListener(this);
     paper.addActionListener(this);
     scissors.addActionListener(this);
     b.add(gui);

}

public static  void main(String[] args) {
    RockPaperScissors start = new RockPaperScissors();
    start.frame();



}

@Override
  public void actionPerformed(ActionEvent e) {
     Gui selection = new Gui();  
     
    if (e.getSource() == rock){
        selection.selector("rock");
        
    } else if (e.getSource() == paper) {
        selection.selector("paper");
        
    } else if (e.getSource() == scissors) {
        selection.selector("scissors");
    }       
}

這是 Gui 類,當然現在處理 gui 的任務只是在按下正確的按鈕時繪制不同顏色的方塊,但沒有任何反應,我懷疑這可能是我遺漏的一個明顯的解決方案。

public class Gui extends JPanel {

   private int y;
   public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(50, 300, 300, 300);
        g.setColor(Color.white);
        g.fillRect(550, 300, 300, 300);
        


        
        if (y == 1) {
            g.setColor(Color.blue);
            g.fillRect(50, 300, 300, 300);
        }
        if (y == 2) {
            g.setColor(Color.black);
            g.fillRect(50, 300, 300, 300);
        }
        if (y == 3) {
            g.setColor(Color.yellow);
            g.fillRect(50, 300, 300, 300);

        }
        
       }
   

   public void selector(String x){
        if (x == "paper"){
            y = 1;
              repaint();

            System.out.println("paper" + y);

        } else if (x == "rock") {
            y = 2;
              repaint();

            System.out.println("rock" + y);

        } else if (x == "scissors") {
            y = 3;
              repaint();

            System.out.println("scissors" + y);

        }   
       
   }
}

一個問題在這里:

public void actionPerformed(ActionEvent e) {
    Gui selection = new Gui();

    //....

    selection.selector("rock");

是的,您正在更改 Gui 實例的狀態,但它是錯誤的實例 你已經有已顯示的GUI實例,就是其狀態應該改變的實例。 您應該將該實例放在它自己的字段中,而不是將它埋在框架方法中。

所以改變

public class RockPaperScissors implements ActionListener {
    JButton rock =new JButton("Select rock");  
    JButton paper =new JButton("Select paper");  
    JButton scissors =new JButton("Select scissors");  


    public void frame() {
         Gui gui = new Gui(); 

public class RockPaperScissors implements ActionListener {
    private JButton rock =new JButton("Select rock");  
    private JButton paper =new JButton("Select paper");  
    private JButton scissors =new JButton("Select scissors");  
    private Gui gui = new Gui();      

    public void frame() {
         // Gui gui = new Gui(); 

並更改 gui字段的狀態

另一個問題:不要使用==!=比較字符串。 請改用equals(...)equalsIgnoreCase(...)方法。 理解==檢查兩個對象引用是否相同,這不是您感興趣的。另一方面,方法檢查兩個字符串是否具有相同順序的相同字符,這就是這里的重要內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM