java老虎机GUI JButton未在JTextField上实现
我们的目标是创建一个有两个面板的老虎机,而这个按钮似乎不起作用,并且在面板的文本字段中没有显示任何内容。单击按钮时,面板中的文本字段应显示随机数生成器。我尝试了Action Listener代码,但它没有起到任何作用。请帮忙!谢谢!
package slotMachineMain;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class slot1 extends JFrame implements ActionListener {
JButton button1;
JTextField text1;
slot1(){
Image icon = Toolkit.getDefaultToolkit().getImage("D:\\11.png");
ImageIcon image1 = new ImageIcon("D:\\12.png");
ImageIcon image2 = new ImageIcon("D:\\13.gif");
ImageIcon image3 = new ImageIcon("D:\\14.png");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Take your Chance Here!");
JLabel label1 = new JLabel();
label1.setIcon(image1);
label1.setBounds(120, 10, 138, 87);
JLabel label2 = new JLabel();
label2.setIcon(image2);
label2.setBounds(0, 0, 400, 290);
JTextField text1 = new JTextField();
text1.setPreferredSize(new Dimension(80,80));
text1.setEditable(false);
JTextField text2 = new JTextField();
text2.setPreferredSize(new Dimension(80,80));
text2.setEditable(false);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.white);
panel1.setBounds(83, 100, 90, 90);
panel1.add(text1);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.setBounds(207, 100, 90, 90);
panel2.add(text2);
JButton button1 = new JButton();
button1.setBounds(152, 200, 75, 25);
button1.setIcon(image3);
button1.addActionListener(this);
this.setResizable(false);
this.setSize(400, 290);
this.setLayout(null);
this.setIconImage(icon);
this.add(panel1);
this.add(panel2);
this.add(button1);
this.add(label1);
this.add(label2);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
Random rn = new Random();
if(e.getSource()==button1) {
text1.setText(Integer.toString(rn.nextInt()));
}
}
}
共 (0) 个答案