13

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();
}
}
asked Dec 9, 2013 at 2:36

3 Answers 3

44

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

Dzarafata
5583 silver badges11 bronze badges
answered Dec 9, 2013 at 3:02
Sign up to request clarification or add additional context in comments.

2 Comments

Should I call this in actionPerformed?
It's working fine with setFont(Font). But there is only one problem, it also affect the size of your button if font size is large enough ...
5

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.

answered Dec 9, 2013 at 3:01

Comments

0

To change the font in java swing:

name_Button.setFont(new FontUIResource(String name, int style, int size));
  • name the font name

  • style the style constant for the font

  • size the point size of the font

Elikill58
5,04527 gold badges33 silver badges65 bronze badges
answered Jan 24, 2022 at 18:37

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.