java猜数字代码有界面 java猜数字游戏代码界面( 二 )


private final JLabel label;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuessNumFrame frame = new GuessNumFrame();
frame.setVisible(true);//窗口可见
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GuessNumFrame() {
setTitle("猜数字");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(360, 160);//窗口大小
setLocationRelativeTo(null);//居中
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new GridLayout(3, 1, 0, 0));
JPanel panel = new JPanel();
panel_1.add(panel);
JButton jbNum = new JButton("得到一个随机数");
panel.add(jbNum);
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
label = new JLabel("");
label.setOpaque(true);
label.setBackground(Color.CYAN);
panel_2.add(label);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
jtfInput = new JTextField();
panel_3.add(jtfInput);
jtfInput.setColumns(10);
JButton jbOk = new JButton("确定");
panel_3.add(jbOk);
jbNum.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num = (int)(Math.random()*10);//生成随即数
label.setText("随机数已经生成范围0~9");
System.out.println(num);
}
});
jbOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x;
try {
x = Integer.parseInt(jtfInput.getText());//取得输入框的数字 进行比较
}catch (Exception e1) {
JOptionPane.showMessageDialog(null, "sorry,输入错误!");
return;
}
if(x==num){
JOptionPane.showMessageDialog(null, "厉害,猜对了~!");
}else if(xnum){
JOptionPane.showMessageDialog(null, "你猜小了", "提示",JOptionPane.WARNING_MESSAGE);
}else if(xnum) {
JOptionPane.showMessageDialog(null, "你猜大了", "提示",JOptionPane.WARNING_MESSAGE);
}
}
});
}
}
java窗体猜数字代码import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class caishuzi extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private JPanel textpanel, buttonpanel, fopanel, downpanel;
private JButton btn;
private JTextField jtinput, jttishi;
private JLabel lbnum0, lbnum, lbtishi, lbinput;
private JTextArea tafo;
private Boolean start = false, jisuan = false, jinru = false;
MenuBar mb = new MenuBar();
Menu filemenu = new Menu("文件"), systemmenu = new Menu("系统"),
helpmenu = new Menu("帮助");
MenuItem newfile = new MenuItem("文件"), openfile = new MenuItem("打开"),
closefile = new MenuItem("关闭"), quit = new MenuItem("退出"),
inputsecret = new MenuItem("输入密码"), help = new MenuItem("帮助"),
inputcishu = new MenuItem("输入限制次数");
char[] c = new char[4];
String strsecret = "123";
int xianzhi=8;
caishuzi()
{
super("猜数字");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setLocationRelativeTo(null);
// 设置菜单
filemenu.add(newfile);
filemenu.add(openfile);
filemenu.addSeparator();
filemenu.add(closefile);
newfile.setEnabled(false);
openfile.setEnabled(false);
closefile.setEnabled(false);
inputsecret.addActionListener(this);
inputcishu.addActionListener(this);

推荐阅读