爲了進行更改,我幾周前編寫了一個程序,現在我正嘗試將該程序轉換爲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像
原始代碼
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
化妝使用gettext的()。使用它在你的actionPerformed() –
我建議如果你不明白,在開始使用gui編輯器之前閱讀oracle的教程,但基本上gui是事件庫。當你點擊按鈕'jButton1ActionPerformed(evt);'this方法得到執行,在這裏你得到輸入的數據! – nachokk
我會用getText()替換掃描儀嗎? – Saint