gpt4 book ai didi

java - 在一个 JButton 中处理多个事件

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:54 27 4
gpt4 key购买 nike

我有一个关于摄氏度到华氏度转换器的作业,它会询问以下问题:

  1. 用户应输入摄氏度值,单击“转换”按钮并获取华氏度值;

  2. 用户应插入华氏度值,单击“转换”按钮并获取摄氏度值;

  3. 显示温度转换结果后,如果再次单击“转换”按钮,则应清除 GUI 中的所有输入输出文本字段。

但是,我很难通过一个按钮实现多个操作。我的错误在哪里(jButton1ActionPerformed中的事件代码)?

Java中有一段代码,我使用Netbeans 8.1:如下

import java.util.Scanner;
import javax.swing.JOptionPane;

public class TConverter extends javax.swing.JFrame {

/**
* Creates new form TConverter
*/
public TConverter() {
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() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("Celcius");

jLabel2.setText("Fahrenheit");

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

jButton2.setText("Reset");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

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

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

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(jLabel1)))
.addGap(39, 39, 39))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel2)
.addGap(47, 47, 47)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1)
.addComponent(jTextField2))
.addContainerGap(177, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.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(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(48, 48, 48)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(90, Short.MAX_VALUE))
);

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

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
jTextField2.setText("");
}

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {


}

private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

float CelciusInput = Float.parseFloat(jTextField1.getText());
float FahrenheitInput = Float.parseFloat(jTextField1.getText());


if (!(jTextField1.getText().isEmpty()))
{
try
{

jTextField2.setText(String.valueOf((CelciusInput * 1.8) + 32));

}
catch (Exception ex)
{

}
}

else if (!(jTextField2.getText().isEmpty()))
{
try
{
jTextField1.setText(String.valueOf((FahrenheitInput - 32) * 5 / 9));
}
catch (Exception ex)
{

}
}

else if (String.valueOf(jTextField2.getText()).equals(String.valueOf(jTextField1.getText())))
{
jTextField1.setText("");
jTextField2.setText("");
}

else
{
JOptionPane.showMessageDialog(null, "Wrong type of input",
"Error dialog message", JOptionPane.ERROR_MESSAGE);
}






}

/**
* @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(TConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TConverter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TConverter.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 TConverter().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.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration }

最佳答案

尝试下面的代码,看看它是否适合您:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
float CelciusInput;
float FahrenheitInput;
if (!(jTextField1.getText().isEmpty())) {
if (jTextField2.getText().isEmpty()) {
try {
CelciusInput = Float.parseFloat(jTextField1.getText());
jTextField2.setText(String.valueOf((CelciusInput * 1.8) + 32));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Wrong type of input",
"Error dialog message", JOptionPane.ERROR_MESSAGE);
jTextField1.setText("");
jTextField2.setText("");
return;
}
} else {
jTextField1.setText("");
jTextField2.setText("");
}
} else if (!(jTextField2.getText().isEmpty())) {
try {
FahrenheitInput = Float.parseFloat(jTextField2.getText());
jTextField1.setText(String.valueOf((FahrenheitInput - 32) * 5 / 9));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Wrong type of input",
"Error dialog message", JOptionPane.ERROR_MESSAGE);
jTextField1.setText("");
jTextField2.setText("");
}
}
}

关于java - 在一个 JButton 中处理多个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58852367/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com