2013-04-04 71 views
1

我正在嘗試製作Tic tac腳趾的界面。我爲我的9個字段使用了JLabels方法。程序中沒有錯誤,但有些東西我不知道如何去做。請在下面看我的問題&請給我建議如何設置TicTacToe遊戲項目的界面?

import javax.swing.*; 
import java.awt.*; 
import java.awt.geom.*; 

public class ticTac extends JFrame 
    { 
    static JLabel label0 = new JLabel(); 
    static JLabel label1 = new JLabel(); 
    static JLabel label2 = new JLabel(); 
    static JLabel label3 = new JLabel(); 
    static JLabel label4 = new JLabel(); 
    static JLabel label5 = new JLabel(); 
    static JLabel label6 = new JLabel(); 
    static JLabel label7 = new JLabel(); 
    static JLabel label8 = new JLabel(); 
    static JLabel[] choiceLabel = new JLabel[9]; 

    static ImageIcon burger = new ImageIcon("Cross.jpg"); 

public static void main(String []args) 
    { 
     new ticTac().show(); 
    } 

public ticTac() 
    { 
     setVisible(true); 
     setSize(450,450); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

     getContentPane().setLayout(new GridBagLayout()); 
     GridBagConstraints myGrid = new GridBagConstraints(); 

     GridBagConstraints gridConstraints; 
     choiceLabel[0] = label0; 
     choiceLabel[1] = label1; 
     choiceLabel[2] = label2; 
     choiceLabel[3] = label3; 
     choiceLabel[4] = label4; 
     choiceLabel[5] = label5; 
     choiceLabel[6] = label6; 
     choiceLabel[7] = label7; 
     choiceLabel[8] = label8; 

此方法繪製從1標籤9在水平行,所以我不希望出現這種情況,我想我的計劃將繪製三個框一排,三排在第二排和三個框。請幫幫我。

 for (int i = 0; i < 9; i++) 
      { 
       gridConstraints = new GridBagConstraints(); 
       choiceLabel[i].setPreferredSize(new Dimension(burger.getIconWidth(), burger.getIconHeight())); 
       choiceLabel[i].setOpaque(true); 
       choiceLabel[i].setBackground(Color.RED); 
       gridConstraints.gridx = i; 
       gridConstraints.gridy = 0; 
       gridConstraints.insets = new Insets(10, 10, 10, 10); 
       getContentPane().add(choiceLabel[i], gridConstraints); 



     pack(); 
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 * (screenSize.height - getHeight())), getWidth(), getHeight()); 
    } 
} 
} 

回答

3

你可以使用一個GridLayout

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import static tictactoe.TicTacToe.choiceLabel; 

public class LayoutTicTacToe { 

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

    public LayoutTicTacToe() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new GridLayout(3, 3, 10, 10)); 

       choiceLabel[0] = new JLabel("_"); 
       choiceLabel[1] = new JLabel("_"); 
       choiceLabel[2] = new JLabel("_"); 
       choiceLabel[3] = new JLabel("_"); 
       choiceLabel[4] = new JLabel("_"); 
       choiceLabel[5] = new JLabel("_"); 
       choiceLabel[6] = new JLabel("_"); 
       choiceLabel[7] = new JLabel("_"); 
       choiceLabel[8] = new JLabel("_"); 

       for (int i = 0; i < 9; i++) { 
        choiceLabel[i].setText("_"); 
        choiceLabel[i].setHorizontalAlignment(JLabel.CENTER); 
        choiceLabel[i].setVerticalAlignment(JLabel.CENTER); 
        choiceLabel[i].setOpaque(true); 
        choiceLabel[i].setBackground(Color.RED); 
        frame.add(choiceLabel[i]); 
       } 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

或者,如果你真的被困在GridBagLayout正在...

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import static tictactoe.TicTacToe.choiceLabel; 

public class LayoutTicTacToe { 

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

    public LayoutTicTacToe() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new GridBagLayout()); 

       choiceLabel[0] = new JLabel("_"); 
       choiceLabel[1] = new JLabel("_"); 
       choiceLabel[2] = new JLabel("_"); 
       choiceLabel[3] = new JLabel("_"); 
       choiceLabel[4] = new JLabel("_"); 
       choiceLabel[5] = new JLabel("_"); 
       choiceLabel[6] = new JLabel("_"); 
       choiceLabel[7] = new JLabel("_"); 
       choiceLabel[8] = new JLabel("_"); 

       GridBagConstraints gbc = new GridBagConstraints(); 
       gbc.insets = new Insets(10, 10, 10, 10); 
       int col = 0; 
       int row = 0; 
       for (int i = 0; i < 9; i++) { 
        gbc.gridx = col; 
        gbc.gridy = row; 
        choiceLabel[i].setText("_"); 
        choiceLabel[i].setHorizontalAlignment(JLabel.CENTER); 
        choiceLabel[i].setVerticalAlignment(JLabel.CENTER); 
        choiceLabel[i].setOpaque(true); 
        choiceLabel[i].setBackground(Color.RED); 
        frame.add(choiceLabel[i], gbc); 
        col++; 
        if (col > 2) { 
         col = 0; 
         row++; 
        } 
       } 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

更新反饋

只是一些友好的反饋...

  • 有延長JFrame,你不添加任何其功能顯著真的沒有任何意義。最好使用JPanel,然後將其添加到JFrame的實例中。如果沒有別的,你現在可以選擇將它添加到你喜歡的任何其他容器中。
  • 在大多數情況下,有沒有必要使用JFrame#getContentPane(除非你使用的是Java 1.4或更低),爲setLayoutadd(還有一大堆別人的)被映射到直接使用getContentPane
  • 如果要將畫面居中放置在屏幕中間,請使用setLocationRelativeTo(null)。使用起來更簡單。如果可以,儘量避免使用setBounds
  • 您應該確保使用事件派發線程的上下文啓動您的UI。看看Initial Threads進一步的細節
+0

謝謝........你真棒.. – CRazyProgrammer 2013-04-04 07:41:46