私は以前のコースでCを使っていたので、私はJavaプログラミングの新しい学生です。私は最近、NetbeansとEclipseのビルダー設計を使ってGUIを作成する方法を学びましたが、まだボタンとフィールドのそれぞれに関数を配置する方法はまだ分かりません。私はベースコンバータで作業してきました。これは私のUIのコードと関数のための別のコードです。私はJava GUI上に関数を置くのに助けが必要です
public class BaseConverter extends javax.swing.JFrame {
/**
* Creates new form BaseConverter
*/
public BaseConverter() {
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">
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox<>();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jComboBox1.setFont(new java.awt.Font("Tempus Sans ITC", 1, 14)); // NOI18N
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Decimal", "Binary", "Octal", "Hexadecimal" }));
getContentPane().add(jComboBox1, new org.netbeans.lib.awtextra.AbsoluteConstraints(56, 40, 150, 40));
jTextField1.setFont(new java.awt.Font("Tempus Sans ITC", 1, 14)); // NOI18N
jTextField1.setText("Enter number");
getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 40, 180, 40));
jButton1.setFont(new java.awt.Font("Tempus Sans ITC", 1, 14)); // NOI18N
jButton1.setText("Convert");
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 40, 90, 40));
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 120, 440, 180));
jLabel1.setFont(new java.awt.Font("Tempus Sans ITC", 1, 14)); // NOI18N
jLabel1.setText("Result:");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 100, -1, -1));
pack();
}// </editor-fold>
/**
* @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(BaseConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BaseConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BaseConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BaseConverter.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 BaseConverter().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
そして、ここでの機能のための私の別々のコードです。
import java.util.Scanner;
パブリッククラスBaseConverter {
public static void main(String[] args) {
//declaring a scanner
Scanner input = new Scanner(System.in);
String ans = input.nextLine();
System.out.println("Would you like to calculate for Decimal, Binary, Octal and Hexadecimal?");
while ((ans.equals ("Yes")))
{
int c;
//asks the user what is the value type
System.out.println("What is the value type?");
System.out.println("1 - Hexadecimal");
System.out.println("2 - Decimal");
System.out.println("3 - Octal");
System.out.println("4 - Binary");
c = input.nextInt();
switch(c){
//if the user inputs 1
case 1:
System.out.println("Please enter the Hexadecimal value: "); //prompts the user to enter the hexadecimal value
String hexadecimal = input.next(); //scanning the hexadecimal
System.out.println("The Hexadecimal number is: " + hexadecimal); //shows the givenn hexadecimal value
int decimal = Integer.parseInt(hexadecimal, 16); //converts hexadecimal to decimal
System.out.println("Decimal: " + decimal); //shows the converted decimal
String binary = Integer.toBinaryString(decimal); //converting the converted decimal to binary
System.out.printf("Binary: " + binary); //shows the converted binary
String octal = Integer.toOctalString(decimal); //converts the decimal to octal
System.out.printf("Octal: " + octal); //shows the converted octal
break; //break
//if the user inputs 2
case 2:
System.out.println("Please enter the Decimal Number: "); //prompts the user to enter the decimal number
int dec = input.nextInt(); //scanning the decimal
System.out.println("The Decimal number is: " + dec); //shows the decimal
String bin = Integer.toBinaryString(dec); //converting the decimal to binary
System.out.println("Binary: " + bin); //shows the equivalent in binary
String oct = Integer.toOctalString(dec); //converting decimal to octal
System.out.println("Octal: " + oct); //shows the equivalent octal number
String hex = Integer.toHexString(dec); //converting decimal to hexadecimal
System.out.println("Hexadecimal: " + hex); //shows the hexadecimal equivalent
break; //break
//if the user inputs 3
case 3:
System.out.println("Please enter the Octal Number: "); //prompts the user to input the octal number
String octa = input.next(); //scans the octal number
int de = Integer.parseInt(octa, 8); //converts octal to decimal
System.out.println("Decimal: " + de); //shows the decimal equivalent
String bi = Integer.toBinaryString(de); //converts the decimal to binary
System.out.println("Binary: " + bi); //shows the binary equivalent
String he = Integer.toHexString(de); //converts the decimal to hexadecimal
System.out.println("Hexadecimal: " + he); //shows the hexadecimal equivalent
break; //break
//if the user inputs 4
case 4:
System.out.println("Please enter the Binary number: "); //prompts the user to enter the binary number
String q = input.next(); //scans the binary number
int r = Integer.parseInt(q, 2); //converts binary to decimal
System.out.println("Decimal: " + r); //shows the decimal equivalent
String oc = Integer.toOctalString(r); //converts decimal to octal
System.out.println("Octal: " + r); //displays the octal value
String l = Integer.toHexString(r); //converts decimal to hex
System.out.println("Hexadecimal: " + l); //displays the hex value
break;
}
}
while((ans.equals("No")))
{
System.out.println("You have successfully exit!");
break;
}
}
}
は、どのように私が作成したGUIにこれらの機能を置くために私を教えることができる人はいますか? JButtonの、このようなコードを使用上のアクションを設定する
大丈夫です。 – greenorange