Java 老虎机图形用户界面
又是我。我们班的任务是制作一台有两个面板的老虎机。除了if-else条件外,一切都很好。JOptionPane似乎不工作或在满足条件时不显示,但在控制台中没有显示任何错误。我看到右边的警告,它说JTextField与String无关,但我将随机数整数转换为String,以在面板上显示它,因为JTextField中似乎只接受String。请帮忙!谢谢大家!Java 老虎机图形用户界面,java,swing,Java,Swing,又是我。我们班的任务是制作一台有两个面板的老虎机。除了if-else条件外,一切都很好。JOptionPane似乎不工作或在满足条件时不显示,但在控制台中没有显示任何错误。我看到右边的警告,它说JTextField与String无关,但我将随机数整数转换为String,以在面板上显示它,因为JTextField中似乎只接受String。请帮忙!谢谢大家! package slotMachineMain; import java.awt.BorderLayout; import java.aw
package slotMachineMain;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
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.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class slot1 extends JFrame implements ActionListener {
JButton button1;
JTextField text1;
JTextField text2;
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);
text1 = new JTextField();
text1.setPreferredSize(new Dimension(80,80));
text1.setFont(new Font("Elephant", Font.BOLD, 35));
text1.setForeground(Color.white);
text1.setBackground(Color.black);
text1.setHorizontalAlignment(JLabel.CENTER);
text1.setEditable(false);
text2 = new JTextField();
text2.setPreferredSize(new Dimension(80,80));
text2.setFont(new Font("Elephant", Font.BOLD, 35));
text2.setForeground(Color.white);
text2.setBackground(Color.black);
text2.setHorizontalAlignment(JLabel.CENTER);
text2.setEditable(false);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.white);
panel1.setBorder(BorderFactory.createLineBorder(Color.black));
panel1.setBounds(83, 100, 90, 90);
panel1.add(text1, BorderLayout.CENTER);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.white);
panel2.setBorder(BorderFactory.createLineBorder(Color.black));
panel2.setBounds(207, 100, 90, 90);
panel2.add(text2, BorderLayout.CENTER);
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((10-8))+8));
text2.setText(Integer.toString(rn.nextInt((10))));
if (text1.equals("8") & text2.equals("9")) {
JOptionPane.showMessageDialog(null, "YOU WIN A TOKEN!", null, JOptionPane.PLAIN_MESSAGE);
}
if (text1.equals("9") & text2.equals("9")) {
JOptionPane.showMessageDialog(null, "JACKPOT YEHEY!", null, JOptionPane.PLAIN_MESSAGE);
}
}
}
}
您通过键入将JTextField与字符串进行比较
text1.equals("8")
等等
你应该这样做
text1.getText().equals("8")
(从JTextField中提取字符串)并获得所需的功能。1)“…它表示JTextField与字符串无关“我打赌它不会!始终复制/粘贴错误和异常输出!2) “又是我了。”。。再一次?您的个人资料只显示一个问题,没有答案。您是否删除了以前的问题,或者为您删除了这些问题?3) Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与。。。。布局的填充和边框。