crescent
crescent

Reputation: 61

Can I write a character of string on JButton?

I wanna create a word game. I'll give the word that is not in order. The gamer will find the correct word. I made three button that have the same background. And I have a string for example BOX. Can I write 'B' to buton1 then 'O' to buton2. I'm confused because they must have a background. Please help thanks

import javax.swing.*;

import java.awt.*;

public class Example extends JFrame {
     private static Example main;

        public static void main (String[] args){


        main = new Example();

    JLabel backGround = new JLabel(new ImageIcon("C:\\he\\main2.png"));
    main.setTitle("FiHa");
    main.setSize(750, 550);
    main.getContentPane().add(backGround);
    main.setLocationRelativeTo(null); // Center the frame
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setVisible(true);
    main.setResizable(false);


    }

    public Example() {
      setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\he\\iconfh.png"));


       ImageIcon ikon=new ImageIcon("C:\\he\\bx.png");

       String word1 ="BOX";

        JButton btn1 = new JButton("1");
        btn1.setBounds( 150, 300, 100,100);
        btn1.setIcon(ikon);
        this.add(btn1);

        JButton btn2 = new JButton("2");
        btn2.setBounds( 290, 300, 100,100);
        btn2.setBackground(Color.pink);
        btn2.setIcon(ikon);
        this.add(btn2);

        JButton btn3 = new JButton("3");
        btn3.setBounds( 430, 300, 100,100);
        btn3.setBackground(Color.pink);
        btn3.setIcon(ikon);
        this.add(btn3);
    }



}

Upvotes: 0

Views: 495

Answers (1)

SHAMBANNA
SHAMBANNA

Reputation: 312

you can use button.setText("button name");

public class Example extends JFrame implements ActionListener
{
    private static Example main;
    JButton btn1, btn2, btn3;

    public static void main(String[] args)
    {


    main = new Example();

    JLabel backGround = new JLabel(new ImageIcon("C:\\he\\main2.png"));
    main.setTitle("FiHa");
    main.setSize(750, 550);
    main.getContentPane().add(backGround);
    main.setLocationRelativeTo(null); // Center the frame
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setVisible(true);
    main.setResizable(false);


    }

    public Example()
    {
    setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\he\\iconfh.png"));


    ImageIcon ikon = new ImageIcon("C:\\he\\bx.png");

    String word1 = "BOX";

    btn1 = new JButton("1");
    btn1.setBounds(150, 300, 100, 100);
    btn1.setBackground(Color.pink);
    btn1.setIcon(ikon);
    this.add(btn1);
    btn1.addActionListener(this);
    btn1.setActionCommand("1");

    btn2 = new JButton("2");
    btn2.setBounds(290, 300, 100, 100);
    btn2.setBackground(Color.pink);
    btn2.setIcon(ikon);
    this.add(btn2);
    btn2.addActionListener(this);
    btn2.setActionCommand("2");

    btn3 = new JButton("3");
    btn3.setBounds(430, 300, 100, 100);
    btn3.setBackground(Color.pink);
    btn3.setIcon(ikon);
    this.add(btn3);
    btn3.addActionListener(this);
    btn3.setActionCommand("3");
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
    JButton btn = (JButton) e.getSource();
    if (btn.getActionCommand().equals("1"))
    {
        btn1.setText("B");
    }
    else if (btn.getActionCommand().equals("2"))
    {
        btn2.setText("o");
    }
    else if (btn.getActionCommand().equals("3"))
    {
        btn3.setText("x");
    }


    }

Upvotes: 1

Related Questions