我可以在JButton上写一个字符串吗?

时间:2016-03-09 10:17:36

标签: java string swing user-interface awt

我想创造一个文字游戏。我会说出不合适的词。玩家会找到正确的单词。我制作了三个具有相同背景的按钮。我有一个字符串,例如BOX。我可以将'B'写入buton1然后将'O'写入buton2。我很困惑,因为他们必须有背景。请帮助谢谢

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);
    }



}

1 个答案:

答案 0 :(得分:0)

你可以使用button.setText(“按钮名称”);

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");
    }


    }