2013-12-09 149 views
6

我很遺憾要完成我的術語項目。我正在開發Connect Four遊戲,我想增加JButton中的字體大小。我對編程相對比較陌生,而且我還沒有使用任何字體。我只想至少將按鈕內的字體加倍,以使其在遊戲過程中更顯眼。有人能幫助我,還是將我指向尋找解決方案的方向?謝謝!我的代碼如下。在JButton中增加字體大小

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

public class Connect implements ActionListener { 





private JFrame window = new JFrame("Connect Four by Steven and Anthony");  
private JPanel myPanel = new JPanel(); 
private JPanel myPanelB = new JPanel(); 
private JButton[][] myButtons = new JButton[6][7]; 
private JButton[] buttons = new JButton[7]; 
private boolean win = false; 

private int count = 5; 
private int count2 = 5; 
private int count3 = 5; 
private int count4 = 5; 
private int count5 = 5; 
private int count6 = 5; 
private int count7 = 5; 
private int countA = 0; 
private String letter = ""; 

public boolean checkHorizontalWin(String letter) { 
for (int y = 0; y < myButtons.length; y++) {   
    for (int x = 0; x < myButtons[y].length - 3; x++) { 
     if (myButtons[y][x].getText().equals(letter) 
      && myButtons[y][x + 1].getText().equals(letter) 
      && myButtons[y][x + 2].getText().equals(letter) 
      && myButtons[y][x + 3].getText().equals(letter) 
     ) { 
      return true; 
     } 
    } 
} 
return false; 
} 


public boolean checkVerticalWin(String letter) { 
    for (int y = 0; y < myButtons.length - 3; y++) { 
     for (int x = 0; x < myButtons[y].length; x++) { 
      if (myButtons[y][x].getText().equals(letter) 
       && myButtons[y + 1][x].getText().equals(letter) 
       && myButtons[y + 2][x].getText().equals(letter) 
       && myButtons[y + 3][x].getText().equals(letter) 
      ) { 
       return true; 
      } 
     } 
    } 
    return false; 
} 


public boolean checkDiagonalToTheLeftWin(String letter) { 
    for (int y = 0; y < myButtons.length - 3; y++) { 
     for (int x = 0; x < myButtons[y].length - 3; x++) { 
      if (myButtons[y][x].getText().equals(letter) 
       && myButtons[y + 1][x + 1].getText().equals(letter) 
       && myButtons[y + 2][x + 2].getText().equals(letter) 
       && myButtons[y + 3][x + 3].getText().equals(letter) 
      ) { 
       return true; 
      } 
     } 
    } 
    return false; 
} 


public boolean checkDiagonalToTheRightWin(String letter) { 
    for (int y = 0; y < myButtons.length - 3; y++) { 
     for (int x = 3; x < myButtons[y].length; x++) { 
      if (myButtons[y][x].getText().equals(letter) 
       && myButtons[y + 1][x - 1].getText().equals(letter) 
       && myButtons[y + 2][x - 2].getText().equals(letter) 
       && myButtons[y + 3][x - 3].getText().equals(letter) 
      ) { 
       return true; 
      } 
     } 
    } 
    return false; 
} 



public Connect(){ 
    window.setSize(800,700); 
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    myPanel.setLayout(new GridLayout(1,7)); 
    myPanelB.setLayout(new GridLayout(6,7)); 
    for (int i = 0; i < buttons.length; i ++){ 
     buttons[i] = new JButton(); 
     myPanel.add(buttons[i]); 
     buttons[i].addActionListener(this); 
    } 
    for (int i = 0; i < 6; i ++){ 
     for (int j = 0; j < 7; j ++){ 
      myButtons[i][j] = new JButton(); 
      myPanelB.add(myButtons[i][j]); 
     } 
    } 
    window.add(myPanel, BorderLayout.NORTH); 
    window.add(myPanelB, BorderLayout.CENTER); 
    window.setVisible(true); 
} 

public void actionPerformed(ActionEvent e){ 
    countA++; 
    if (countA % 2 == 0) 
     letter = "X"; 
    else 
     letter = "O"; 



    if (e.getSource() == buttons[0]){ 
     myButtons[count][0].setText(letter); 
     count --; 
    } 
    if (e.getSource() == buttons[1]){ 
     myButtons[count2][1].setText(letter); 
     count2 --; 
    } 
    if (e.getSource() == buttons[2]){ 
     myButtons[count3][2].setText(letter); 
     count3--; 
    } 
    if (e.getSource() == buttons[3]){ 
     myButtons[count4][3].setText(letter); 
     count4--; 
    } 
    if (e.getSource() == buttons[4]){ 
     myButtons[count5][4].setText(letter); 
     count5--; 
    } 
    if (e.getSource() == buttons[5]){ 
     myButtons[count6][5].setText(letter); 
     count6--; 
    } 
    if (e.getSource() == buttons[6]){ 
     myButtons[count7][6].setText(letter); 
     count7--; 
    } 
    if (myButtons[0][0].getText().equals("O") || myButtons[0][0].getText().equals("X")){ 
     buttons[0].setEnabled(false); 
    } 
    if (myButtons[0][1].getText().equals("O") || myButtons[0][1].getText().equals("X")){ 
     buttons[1].setEnabled(false); 
    } 
    if (myButtons[0][2].getText().equals("O") || myButtons[0][2].getText().equals("X")){ 
     buttons[2].setEnabled(false); 
    } 
    if (myButtons[0][3].getText().equals("O") || myButtons[0][3].getText().equals("X")){ 
     buttons[3].setEnabled(false); 
    } 
    if (myButtons[0][4].getText().equals("O") || myButtons[0][4].getText().equals("X")){ 
     buttons[4].setEnabled(false); 
    } 
    if (myButtons[0][5].getText().equals("O") || myButtons[0][5].getText().equals("X")){ 
     buttons[5].setEnabled(false); 
    }      

    if (myButtons[0][6].getText().equals("O") || myButtons[0][6].getText().equals("X")){ 
     buttons[6].setEnabled(false); 
} 

if (checkHorizontalWin(letter) 
    || checkVerticalWin(letter) 
    || checkDiagonalToTheLeftWin(letter) 
    || checkDiagonalToTheRightWin(letter) 
    ) { 
     win = true; 

     if (win == true) { 
      JOptionPane.showMessageDialog(null, letter + " has won!"); 
      System.exit(0); 
     }  
    } 
} 




/** 
* 
* @param args 
*/ 
public static void main(String[] args){ 
    new Connect(); 
} 
} 

回答

2

我不知道這是否會工作,但看JButton文檔,有一個setFont(Font font)方法,你可以調用。你可以嘗試傳遞一個Font,使用你想要使用Font(String name, int style, int size)構造函數的字體大小創建它。

18

您可以使用:

button.setFont(new Font("Arial", Font.PLAIN, 40)); 
  • "Arial"顯然是正在使用的字體的名稱。
  • Font.PLAIN表示純文本(而不是粗體或斜體)。
  • 40是字體大小(使用字體大小爲Microsoft Word中相同的編號系統)

Javadoc for JComponent.setFont()

Javadoc for Java.awt.Font

+0

我應該在actionPerformed中調用它嗎? – Smn21

0

它的正常工作與setFont(Font)。但只有一個問題,它也影響你的按鈕的大小,如果字體大小足夠大...