2013-12-10 74 views
0

爲了進行更改,我幾周前編寫了一個程序,現在我正嘗試將該程序轉換爲gui並存在一些問題。我可以使用gui來讓gui看起來很漂亮,使用Netbeans Jframe編輯器,這使得它很簡單我的問題是試圖找出如何插入我的代碼。我將我的程序插入第一個Jbutton,但無法弄清楚如何從gui中獲取用戶輸入,而不是程序。我點擊Jbutton,它會要求控制檯做這個工作,而不是gui。請幫忙。更改程序GUI

什麼我的GUI看起來與GUI使用NetBeans的JFrame編輯

import java.util.Scanner; 

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author Christ 
*/ 
public class coin extends javax.swing.JFrame { 


public coin() { 
    initComponents(); 
} 


@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
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(); 
    jLabel5 = new javax.swing.JLabel(); 
    jButton1 = new javax.swing.JButton(); 
    jButton2 = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jLabel1.setText("Enter a Number (1-99)"); 

    jTextField1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jTextField1ActionPerformed(evt); 
     } 
    }); 

    jLabel2.setText("Quarters"); 

    jLabel3.setText("Dimes"); 

    jLabel4.setText("Nickles"); 

    jLabel5.setText("Pennies"); 

    jButton1.setText("Calculate"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jButton2.setText("Clear"); 

    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() 
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE,  179, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(18, 18, 18) 
        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGap(68, 68, 68) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addComponent(jLabel3) 
       .addComponent(jLabel4) 
       .addComponent(jLabel5)) 
      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(22, 22, 22) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jLabel1) 
       .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addGap(55, 55, 55) 
      .addComponent(jLabel2) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
      .addComponent(jLabel3) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
      .addComponent(jLabel4) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
      .addComponent(jLabel5) 
      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE) 
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) 
      .addContainerGap()) 
    ); 

    pack(); 
}// </editor-fold>       

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {            
    // TODO add your handling code here: 
}           

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 


    // Initialize varriables 
    int quarters = 25; 
    int dimes = 10; 
    int nickles = 5; 
    int pennies = 1; 

    //Loop starts here 
    while(true) { 

    System.out.println("Enter in a number between 1-99"); 

    // Blank Output for spacing 
    System.out.println(); 
    // User Input "remember this for reference" 
    Scanner Userinput = new Scanner(System.in); 


    int input = Userinput.nextInt(); 


    //while loop end 
    if(input<1) { 
    break; //break is a keyword that exits the loop when a condition is met. 
    } 

    int q = input/quarters; 
    input -= q*quarters; 
    String A = "Quarters:" +q; 

    //Blank Output for spacing 
    System.out.println(); 


    //output quarters 
    System.out.println(A); 


    int d = input/dimes; 
    input -= d*dimes; 
    String B = "Dimes:" +d; 

    //output dimes 
    System.out.println(B); 


    int n = input/nickles; 
    input -= n*nickles; 
    String C = "Nickles:" +n; 

    //output nickles 
    System.out.println(C); 


    int p = input/pennies; 
    input -= p*pennies; 
    String D = "Pennies:" +p; 

    //output pennies 
    System.out.println(D); 




    } 







}           

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(coin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(coin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(coin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(coin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the form */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new coin().setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify      
private javax.swing.JButton jButton1; 
private javax.swing.JButton jButton2; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JLabel jLabel3; 
private javax.swing.JLabel jLabel4; 
private javax.swing.JLabel jLabel5; 
private javax.swing.JTextField jTextField1; 
// End of variables declaration     
} 
01像

enter image description here

原始代碼

public static void main (String[] Args) { 

    // Initialize varriables 
    int quarters = 25; 
    int dimes = 10; 
    int nickles = 5; 
    int pennies = 1; 

    //Loop starts here 
    while(true) { 

    System.out.println("Enter in a number between 1-99"); 

    // Blank Output for spacing 
    System.out.println(); 
    // User Input "remember this for reference" 
    Scanner Userinput = new Scanner(System.in); 


    int input = Userinput.nextInt(); 


    //while loop end 
    if(input<1) { 
    break; //break is a keyword that exits the loop when a condition is met. 
    } 

    int q = input/quarters; 
    input -= q*quarters; 
    String A = "Quarters:" +q; 

    //Blank Output for spacing 
    System.out.println(); 


    //output quarters 
    System.out.println(A); 


    int d = input/dimes; 
    input -= d*dimes; 
    String B = "Dimes:" +d; 

    //output dimes 
    System.out.println(B); 


    int n = input/nickles; 
    input -= n*nickles; 
    String C = "Nickles:" +n; 

    //output nickles 
    System.out.println(C); 


    int p = input/pennies; 
    input -= p*pennies; 
    String D = "Pennies:" +p; 

    //output pennies 
    System.out.println(D); 




    } 





    } 

目前代碼

現在我認爲需要的是掃描儀,它將用戶輸入插入控制檯來完成工作,並以某種方式從GUI中的文本框中獲取用戶輸入,但我絕對不知道如何實現這一點

另外我是一個begginer所以如果你可以讓它容易理解將不勝感激,簡單是最好的。

也發現一個幫助,但一直沒有能夠使用它來嘗試和工作,我想要的東西。

鏈接圖坦卡蒙 http://www.codeproject.com/Articles/33536/An-Introduction-to-Java-GUI-Programming

+0

化妝使用gettext的()。使用它在你的actionPerformed() –

+1

我建議如果你不明白,在開始使用gui編輯器之前閱讀oracle的教程,但基本上gui是事件庫。當你點擊按鈕'jButton1ActionPerformed(evt);'this方法得到執行,在這裏你得到輸入的數據! – nachokk

+0

我會用getText()替換掃描儀嗎? – Saint

回答

2

Netbeans的賺與我的聲譽年前創建令人費解的GUI代碼。從來沒有真正爲自己測試過,但這對於如此簡單的應用程序來說似乎是很多代碼。

將基於控制檯的應用程序轉換爲基於GUI的應用程序時,您必須問自己這個問題: 用戶如何與系統交互?

在你的情況,你的控制檯應用程序與用戶的交互方式:

  1. 輸出提示輸入
  2. 接受輸入現在

,必須解決如何改變這兩個相互作用到一個GUI佈局。它應該對現有代碼進行非常小的修改,同時添加GUI代碼來處理輸入/輸出。

在你的情況下,你有一個充當提示符的標籤,一個容納輸入的textarea和一個充當接受輸入的按鈕。

所以你的程序的流程是這樣的:用戶類型分爲文本區域 - >用戶點擊提交 - >系統接受輸入並對其進行處理 - >系統輸出在宿舍角錢方面改變等

現在,我們編碼它...

第1步:設置GUI機制。您有向下

步驟2:處理用戶輸入:你的計算按鈕,而你的情況似乎jButton1ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // Initialize varriables 
    int quarters = 25; 
    int dimes = 10; 
    int nickles = 5; 
    int pennies = 1; 

    try { 
     int input = Integer.parseInt(jTextField1.getText()); 
    } 
    catch (NumberFormatException e) { 
     //prompt user to enter an integer, not erroneous input 
     //e.printStackTrace(); 
    } 

    //future code 
} 

注意輸入如何具有同樣的功能,因爲它在你的處理器做到這一點在您的控制檯應用程序中,我們只是通過不同的方式獲得它。由於GUI系統是由用戶輸入驅動的,我們不應該持續循環直到用戶輸入有效的輸入,我們只是告訴用戶他們的輸入是無效的,然後讓他們輸入一個有效的數字。我們檢查,如果數字是有效的(1到99之間輸入)和做處理,否則什麼也不做(並提示輸入有效?)

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // Initialize varriables 
    int quarters = 25; 
    int dimes = 10; 
    int nickles = 5; 
    int pennies = 1; 

    try { 
     int input = Integer.parseInt(jTextField1.getText()); 
    } 
    catch (NumberFormatException e) { 
     //prompt user to enter an integer, not erroneous input 
     //e.printStackTrace(); 
    } 

    //if input in the range of [1, 99] 
    if(1 <= input && input <= 99) { 
     //do processing 
    } 
    else { 
     //prompt user for valid input, for example by using JOptionPane 
    } 
} 

現在:輸出。而不是在A,B,C和D上執行System.out.println,只需設置表示四分之一,一角硬幣等金額的JComponents文本即可。目前你似乎缺少總數的標籤/ textareas,所以我會先添加它們,然後使用setText(A),setText(B)等。

+0

真棒我想我已經得到了大部分,你知道我怎麼能從另一個按鈕拉Varriables? http://gyazo.com/fc62c0e8be51c00b29fd1fe0dfa8ce39 – Saint

+0

你的意思是在另一個方法中聲明的變量,例如在另一個ActionPerformed中的jButton1ActionPerformed中的宿舍?在你的情況下唯一的方法是將整數聲明爲全局變量,所以它們都可以在與你的方法相同的封裝級別訪問。例如,jButton1和jButton1ActionPerformed處於同一級別。所以,如果你移動整個宿舍;對同一個區域jButton1進行了定義,您可以通過類中的任何方法訪問它。 – ryanlutgen

+0

Nvm我想通了。謝謝你,畝 – Saint

0

不是100%的解決辦法,我已經花了一個可愛的時間調整它爲你,讓你找到一個出口

試試這個代碼

package test; 


    import java.util.Scanner; 
import java.util.Scanner; 

    /* 
    * To change this template, choose Tools | Templates 
    * and open the template in the editor. 
    */ 

    /** 
    * 
    * @author Christ 
    */ 
    public class coin extends javax.swing.JFrame { 


    public coin() { 
     initComponents(); 
    } 


    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    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(); 
     jLabel5 = new javax.swing.JLabel(); 
     jButton1 = new javax.swing.JButton(); 
     jButton2 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jLabel1.setText("Enter a Number (1-99)"); 

     jTextField1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jTextField1ActionPerformed(evt); 
      } 
     }); 

     jLabel2.setText("Quarters"); 

     jLabel3.setText("Dimes"); 

     jLabel4.setText("Nickles"); 

     jLabel5.setText("Pennies"); 

     jButton1.setText("Calculate"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jButton2.setText("Clear"); 

     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() 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE,  179, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(18, 18, 18) 
         .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(68, 68, 68) 
         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)) 
        .addComponent(jLabel3) 
        .addComponent(jLabel4) 
        .addComponent(jLabel5)) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(22, 22, 22) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addGap(55, 55, 55) 
       .addComponent(jLabel2) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jLabel3) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jLabel4) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addComponent(jLabel5) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addContainerGap()) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {            
     // TODO add your handling code here: 
    }           

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 


     // Initialize varriables 
     int quarters = 25; 
     int dimes = 10; 
     int nickles = 5; 
     int pennies = 1; 

     //Loop starts here 


     System.out.println("Enter in a number between 1-99"); 

     // Blank Output for spacing 
     System.out.println(); 
     // User Input "remember this for reference" 
     Scanner Userinput = new Scanner(System.in); 


     int input = Integer.parseInt(jTextField1.getText()); 



     int q = input/quarters; 
     input -= q*quarters; 
     jLabel2.setText("Quarter: "+q); 

     //Blank Output for spacing 
     System.out.println(); 


     //output quarters 


     int d = input/dimes; 
     input -= d*dimes; 
     jLabel3.setText("Dimes: "+d); 

     //output dimes 


     int n = input/nickles; 
     input -= n*nickles; 
     jLabel4.setText("Nickles: "+n); 

     //output nickles 


     int p = input/pennies; 
     input -= p*pennies; 
     jLabel5.setText("Pennies: "+q); 

     //output pennies 





    }           

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(coin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(coin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(coin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(coin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new coin().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JLabel jLabel5; 
    private javax.swing.JTextField jTextField1; 
    // End of variables declaration  


    }