gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:15:57 24 4
gpt4 key购买 nike

我想创建一个文字游戏。我将给出不按顺序的词。玩家将找到正确的单词。我制作了三个具有相同背景的按钮。我有一个字符串,例如 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);
}



}

最佳答案

您可以使用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");
}


}

关于java - 我可以在JButton上写一个字符串字符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35888720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com