Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increasing Font Size in a JButton

I am at a loss of what to do for the finalization of my term project. I am working on a Connect Four game and I'd like to increase the font size inside of a JButton. I'm relatively new to programming and I haven't worked anything with fonts yet. I'd just like to at least double the font inside of the button to make it more visible during gameplay. Can someone help me, or point me into the direction of finding a solution? Thanks! My code is below.

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();
}
}
like image 667
Smn21 Avatar asked Dec 09 '13 02:12

Smn21


People also ask

How do I change text in a JButton?

By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.

How do I increase the font size of a swing label?

We change the Font Size parameter to change the size of the JLabel Font. The use of the Font() function is shown below: Object. setFont(new Font("Font-Style", Font-Weight, Font Size));

How do I increase the font size of a button?

To change the font size of a button, use the font-size property.


2 Answers

You can use:

button.setFont(new Font("Arial", Font.PLAIN, 40));
  • "Arial" is obviously the name of the font being used.
  • Font.PLAIN means plain text (as opposed to bold or italic).
  • 40 is the font size (using the same numbering system for font size as Microsoft Word)

Javadoc for JComponent.setFont()

Javadoc for Java.awt.Font

like image 148
Colin Avatar answered Oct 04 '22 14:10

Colin


I'm not sure if this will work, but looking at the JButton docs, there is a setFont(Font font) method you can call. You can try passing it a Font created with the font size you'd like using the Font(String name, int style, int size) constructor.

like image 33
Brian Bowman Avatar answered Oct 04 '22 15:10

Brian Bowman