⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newjframe.java

📁 java做的猜数字小游戏源码
💻 JAVA
字号:
/* * NewJFrame.java * * Created on 2008年10月23日, 下午7:14 */package quiz;import java.util.TimerTask;import java.util.Timer;/** * * @author  Ultimate */public class NewJFrame extends javax.swing.JFrame {    /** Creates new form NewJFrame */    public NewJFrame(){        initComponents();    }    /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    @SuppressWarnings("unchecked")    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents    private void initComponents() {        jLabel1 = new javax.swing.JLabel();        jTextField1 = new javax.swing.JTextField();        jLabel2 = new javax.swing.JLabel();        jLabel3 = new javax.swing.JLabel();        jLabel4 = new javax.swing.JLabel();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        setTitle("来猜数吧");        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quiz/008.png"))); // NOI18N        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {            public void mousePressed(java.awt.event.MouseEvent evt) {                jLabel1MousePressed(evt);            }            public void mouseReleased(java.awt.event.MouseEvent evt) {                jLabel1MouseReleased(evt);            }        });        jTextField1.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                jTextField1ActionPerformed(evt);            }        });        jLabel2.setText("请输入1-100的数字");        jLabel4.setText("你猜了0次");        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());        getContentPane().setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addComponent(jLabel1)                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                    .addGroup(layout.createSequentialGroup()                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)))                    .addGroup(layout.createSequentialGroup()                        .addGap(57, 57, 57)                        .addComponent(jLabel4))                    .addGroup(layout.createSequentialGroup()                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)))                .addContainerGap())        );        layout.setVerticalGroup(            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)            .addGroup(layout.createSequentialGroup()                .addContainerGap()                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)                    .addGroup(layout.createSequentialGroup()                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)                        .addGap(10, 10, 10)                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))                    .addComponent(jLabel1))                .addContainerGap())        );        pack();    }// </editor-fold>//GEN-END:initComponentsprivate void jLabel1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MousePressed// TODO add your handling code here:        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quiz/007.png")));         comparenumber();}//GEN-LAST:event_jLabel1MousePressedvoid comparenumber(){     int i,j;     if(jTextField1.getText().length()==0) return;     j=Integer.parseInt(jTextField1.getText());     i=num.compare(j);       times++;     jLabel4.setText("你猜了"+times+"次");    switch(i){        case 2: jLabel3.setText(j+"太大了!!");                repaint();                break;       case 0: jLabel3.setText(j+"太小了!!");               repaint();                                       break;          case 1: jLabel3.setText("恭喜你猜对了!!");               repaint();                 break;       default:   jLabel3.setText("错误");}}   void Flash(){       jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quiz/007.png")));         final Timer timer=new Timer();       timer.schedule(new TimerTask(){                   public void run(){                    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quiz/008.png")));               }},150);         }private void jLabel1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseReleased// TODO add your handling code here:    jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quiz/008.png"))); }//GEN-LAST:event_jLabel1MouseReleasedprivate void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed// TODO add your handling code here:    Flash();         comparenumber();}//GEN-LAST:event_jTextField1ActionPerformed    /**    * @param args the command line arguments    */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                new NewJFrame().setVisible(true);                           }        });    }    // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JLabel jLabel1;    private javax.swing.JLabel jLabel2;    private javax.swing.JLabel jLabel3;    private javax.swing.JLabel jLabel4;    private javax.swing.JTextField jTextField1;    // End of variables declaration//GEN-END:variables Number num=new Number(); int times=0;   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -