我是一名不熟悉Java编程的学生,因为我们之前的课程使用过C语言。我最近学会了如何使用Netbeans和Eclipse中的构建器设计制作GUI,但是我仍然不知道如何将函数放在每个按钮和字段上,即使我已经有了单独的函数代码。我一直在使用Base Converter,这是我的UI代码和函数的单独代码。
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中?
答案 0 :(得分:1)
实际上,我希望您首先阅读本教程,因为这个地方不能替代您的教程。
JButton教程:How to use buttons
要了解如何使用JCombobox:How to use ActionListener on a ComboBox
无论如何,这是怎么回事......
class MainPanel extends JPanel //in your case, you extends to JFrame
{
private JButton btn;
public MainPanel(){
//other initializations
btn.add(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
//invoke your conversion method here..
//e.g:
//txtResult.setText(convert(txtInput.getText()));
}
});
}
}
请注意,如果您实现GUI,则不再使用Scanner(System.in)
来接收输入。所有输入和输入与用户的交互将转到JComponents,如文本框和按钮。
答案 1 :(得分:0)
在JButton上设置操作,使用如下代码:
JButton button = new JButton("click me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("clicked");
}
});
答案 2 :(得分:0)
一些提示让你开始。在initComponents();
行之后插入如下内容:
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get values from jComboBox1.get???() and jTextField1.get???()
// Do your calculations
// Show the result with jTextArea1.set???(???)
}
});