2016-04-10 25 views
-2

Tic Tac Toe GUI yaptım ve fare tıklatıldığında paint() yönteminin iki kez çağrıldığını buldum (mouseClicked() olay kodunu denetle). Hiçbir fikrim yok - bu nasıl oluyor ve bu sorunu önlemek için ne yapmalıyım?Neden boya yöntemi iki kere çağrıldı?

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
public class tic_tac_toe_game extends JFrame implements MouseListener{ 
JPanel contentPane; 
JLabel box_1_1,box_1_2,box_1_3,box_2_1,box_2_2,box_2_3,box_3_1,box_3_2,box_3_3; //x, y coordinate 

static int turn = 1; // 1 is p1 turn 2 is p2 turn 
static boolean win_state = false; 
static boolean break_up = false; 
static boolean cant_turn[][] = new boolean[3][3]; 
public tic_tac_toe_game() { 

    contentPane = new JPanel(); 
    contentPane.setLayout(null); 
    box_1_1 = new JLabel("T"); 
    box_1_2= new JLabel("T"); 
    box_1_3 = new JLabel("T"); 
    box_2_1= new JLabel("T"); 
    box_2_2 = new JLabel("T"); 
    box_2_3= new JLabel("T"); 
    box_3_1 = new JLabel("T"); 
    box_3_2= new JLabel("T"); 
    box_3_3 = new JLabel("T"); 

    //modify it in paint() 


    //contentPane *NO* layout mnager 
    setContentPane(contentPane); 
    setSize(600,600); 
    setVisible(true); 
    setTitle("Tic tac toe game"); 
    setLayout(null); 


} 
public void paint(Graphics g) 
{ 


    g.drawLine(0, getHeight()/3*1, getWidth(), getHeight()/3); // 1, 2 lines haha (hor) 
    g.drawLine(0, getHeight()/3*2, getWidth(), getHeight()/3*2); //  (vertical) 
    g.drawLine(getWidth()/3*1, 0, getWidth()/3*1, getHeight()); // 1, 2 lines haha (hor) 
    g.drawLine(getWidth()/3*2, 0, getWidth()/3*2, getHeight()); //(vertical); 

    setup_UI(); 
    //box_1_1.set 
} 
public void setup_UI() 
{ 
    box_1_1.setFont(new Font("Serif",Font.BOLD,50)); 
    box_1_1.setBounds(getWidth()/3/3*1, getHeight()/3/3*1, 40, 40); 
    box_1_1.addMouseListener(this); 
    contentPane.add(box_1_1); 

    box_1_2.setFont(new Font("Serif",Font.BOLD,50)); 
    box_1_2.setBounds(getWidth()/3/3*1, getHeight()/3/3*4, 40, 40); 
    box_1_2.addMouseListener(this); 
    contentPane.add(box_1_2); 

    box_1_3.setFont(new Font("Serif",Font.BOLD,50)); 
    box_1_3.setBounds(getWidth()/3/3*1, getHeight()/3/3*7, 40, 40); 
    box_1_3.addMouseListener(this); 
    contentPane.add(box_1_3); 

    box_2_1.setFont(new Font("Serif",Font.BOLD,50)); 
    box_2_1.setBounds(getWidth()/3/3*4, getHeight()/3/3*1, 40, 40); 
    box_2_1.addMouseListener(this); 
    contentPane.add(box_2_1); 

    box_2_2.setFont(new Font("Serif",Font.BOLD,50)); 
    box_2_2.setBounds(getWidth()/3/3*4, getHeight()/3/3*4, 40, 40); 
    box_2_2.addMouseListener(this); 
    contentPane.add(box_2_2); 

    box_2_3.setFont(new Font("Serif",Font.BOLD,50)); 
    box_2_3.setBounds(getWidth()/3/3*4, getHeight()/3/3*7, 40, 40); 
    box_2_3.addMouseListener(this); 
    contentPane.add(box_2_3); 

    box_3_1.setFont(new Font("Serif",Font.BOLD,50)); 
    box_3_1.setBounds(getWidth()/3/3*7, getHeight()/3/3*1, 40, 40); 
    box_3_1.addMouseListener(this); 
    contentPane.add(box_3_1); 

    box_3_2.setFont(new Font("Serif",Font.BOLD,50)); 
    box_3_2.setBounds(getWidth()/3/3*7, getHeight()/3/3*4, 40, 40); 
    box_3_2.addMouseListener(this); 
    contentPane.add(box_3_2); 

    box_3_3.setFont(new Font("Serif",Font.BOLD,50)); 
    box_3_3.setBounds(getWidth()/3/3*7, getHeight()/3/3*7, 40, 40); 
    box_3_3.addMouseListener(this); 
    contentPane.add(box_3_3); 
} 
public static void main(String[] args) 
{ 
    tic_tac_toe_game tic = new tic_tac_toe_game(); 
    /*while(tic.win_state== false) 
    { 
    tic.whos_turn(); 
    //tic.rule(); // de termine if player win or not 
    }*/ 
} 
public void rule() 
{ 
    // if(box_1_1.equals('O') == true) 
    //  System.out.append("Yes"); 
} 
public void whos_turn() 
{ 
    if(turn == 1 && break_up == false) 
    { 
    JOptionPane.showMessageDialog(null, "P1's turn ! "); 

    break_up = true; 

    } 
    else if(turn == 2 && break_up == false) 
    { 
    JOptionPane.showMessageDialog(null, "P2's turn ! ");  

    break_up = true; 
    } 
} 

@Override 


public void mouseClicked(MouseEvent e) {   
    // TODO Auto-generated method stub 
     System.out.print("I'm clicked"); //this one called twice 

} 


@Override 
public void mouseEntered(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 
@Override 
public void mouseExited(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 
@Override 
public void mousePressed(MouseEvent e) { 
    // TODO Auto-generated method stub 

} 
@Override 
public void mouseReleased(MouseEvent e) { 
    // TODO Auto-generated method stub 
} 
} 
+1

* "Bunun nasıl gerçekleşeceğine dair bir fikrim yok." * API, bir bileşenin ne zaman boyanacağına karar verir. Restorasyon sonrası, cepheye gelip yeniden boyutlandırıldıktan sonra resme ihtiyaç duyulabilir .. * ".. .. ve bu sorunu önlemek için nasıl yapmalıyım?" * Bu bir sorun değil, düzeltebileceğiniz bir şey değil. Uygulama bunu söylendiğinde kendisini yeniden boyamak zorundadır. Resmin karmaşık bir parçası varsa, bir 'BufferedImage' dosyasına boyayın (ardından görüntüyü boya yönteminde çizin). –

+4

Bu soruyu off-topic olarak kapatmak için oy kullanıyorum çünkü OP, bozuk olmayan bir şeyi düzeltmeye çalışıyor. –

+0

'null' düzeni kullanmaktan kaçının, pixel mükemmel düzenler, modern kullanıcı arabirimi tasarımı içinde bir yanılsamadır. Bileşenlerin bireysel büyüklüğünü etkileyen çok fazla faktör vardır, bunların hiçbiri kontrol edemezsiniz. Swing, düzen yöneticileri ile birlikte çalışacak şekilde tasarlandı, bunları atmak, daha fazla zaman harcayacağınız sorunları ve problemleri ortadan kaldıracak ve – MadProgrammer

cevap

1

Sizin paint yöntemi yalnızca boya ve çağrı setup_UI gibi ekstra bir şey yapmamalıyız. Bunun yerine kurucuda setup_UI'u arayın.

Programınızın iş parçacığı olmadığını unutmayın, salıncak bileşenlerini oluşturmak ve değiştirmek için ilk iş parçacığını kullanmamalısınız. https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html

+0

ve super.paint() ve repaint() – mKorbel

İlgili konular