我正在制作一个单位转换器,遇到了一个问题与JoptionPane.ShowMessageDialog代码是完整的,应该运行良好。
如果将两个转换单元都放到同一个单元,转换器将显示一个消息对话框。当消息窗口弹出,它说消息,然后一个确定按钮点击。当我单击Ok按钮时,它不会退出消息对话框。我从这个转换器的另一个更长的版本中为它复制了代码行,它可以很好地使用它。另外,如果你对如何做更好的数学有任何建议,请告诉我。我还是一个编程和Java的初学者。任何帮助都很感激。
package V3Converter;
import java.awt.Toolkit;
import java.math.BigInteger;
import java.util.HashMap;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
/**
*
* @author Anthony Tennenbaum
*/
public class v3Converter extends javax.swing.JFrame {
//constant type double arrays filled in order of: centimeter,feet,inche,kilometer,mile,millimeter,meter,yard
final double[] TO_CENTM = {1,30.48,2.54,100000,160935,0.1,100,91.44};
final double[] TO_FEET = {0.032808399,1,0.083333333334,3280.839895,5280.019685,0.0032808399,3.280839895,3};
final double[] TO_INCHE ={0.3937007874,12,1,39370.07874,63360.23622, 0.0393700787,39.37007874,36} ;
final double[] TO_KILOM = { 0.00001,0.0003048, } ;
final double[] TO_MILE = {};
final double[] TO_MILIM = {};
final double[] TO_METER = {0.01,0.3048,0.0254,0.001,1609.35,0.001,1,0.9144};
final double[] TO_YARD = {0.010936133,0.3333333333,0.0277777778,1093.6132983,1760.0065617,1760.0065617,1};
//making a String array
final String[] UNIT_NAME = {"Centimeters", "Feet", "Inches", "Kilometers", "Miles", "Millimeters", "Meters", "Yards"};a
/**
* Creates new form Converter
*/
public v3Converter() {
initComponents();
setIcon(); //sets the icon image for the jFrame
setSize(350,225); //set the starting size of frame
setLocationRelativeTo(null); //on startup centers the frame
getContentPane().setBackground(java.awt.Color.BLACK);
comboBoxFrom.setSelectedIndex(0); //comboBox starts at index 0(first item in list) -1 puts box empty
comboBoxTo.setSelectedIndex(0);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnConvert = new javax.swing.JButton();
comboBoxFrom = new javax.swing.JComboBox<>();
comboBoxTo = new javax.swing.JComboBox<>();
txtField1 = new javax.swing.JTextField();
btnReset = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
lblConvertTo = new javax.swing.JLabel();
lblConvertFrom = new javax.swing.JLabel();
pnlAnswer = new javax.swing.JPanel();
lblAnswer = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Unit Converter");
setFont(new java.awt.Font("Copperplate Gothic Light", 1, 14)); // NOI18N
setResizable(false);
btnConvert.setBackground(new java.awt.Color(204, 0, 0));
btnConvert.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btnConvert.setForeground(new java.awt.Color(0, 204, 0));
btnConvert.setText("Convert");
btnConvert.setActionCommand("Convert To");
btnConvert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnConvertActionPerformed(evt);
}
});
comboBoxFrom.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Choose One..", "Length Units", "Time Units" }));
comboBoxFrom.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboBoxFromActionPerformed(evt);
}
});
comboBoxTo.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Choose One..", " " }));
comboBoxTo.setToolTipText("");
comboBoxTo.setEnabled(false);
comboBoxTo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboBoxToActionPerformed(evt);
}
});
txtField1.setFont(new java.awt.Font("Tahoma", 1, 13)); // NOI18N
txtField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtField1ActionPerformed(evt);
}
});
btnReset.setBackground(new java.awt.Color(0, 204, 0));
btnReset.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btnReset.setForeground(new java.awt.Color(204, 0, 0));
btnReset.setText("Reset");
btnReset.setActionCommand("Convert To");
btnReset.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnResetActionPerformed(evt);
}
});
btnExit.setBackground(new java.awt.Color(0, 0, 204));
btnExit.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
btnExit.setForeground(new java.awt.Color(204, 0, 0));
btnExit.setText("Exit");
btnExit.setActionCommand("Convert To");
btnExit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExitActionPerformed(evt);
}
});
lblConvertTo.setFont(new java.awt.Font("Copperplate Gothic Light", 1, 11)); // NOI18N
lblConvertTo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblConvertTo.setText("Convert to");
lblConvertFrom.setFont(new java.awt.Font("Copperplate Gothic Light", 1, 11)); // NOI18N
lblConvertFrom.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblConvertFrom.setText("Convert From");
pnlAnswer.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
lblAnswer.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
lblAnswer.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
javax.swing.GroupLayout pnlAnswerLayout = new javax.swing.GroupLayout(pnlAnswer);
pnlAnswer.setLayout(pnlAnswerLayout);
pnlAnswerLayout.setHorizontalGroup(
pnlAnswerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pnlAnswerLayout.createSequentialGroup()
.addComponent(lblAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
pnlAnswerLayout.setVerticalGroup(
pnlAnswerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlAnswerLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(lblAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
);
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(266, 266, 266)
.addComponent(btnExit))
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(btnConvert)
.addGap(45, 45, 45)
.addComponent(btnReset)
.addGap(196, 196, 196))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(pnlAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(comboBoxTo, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(lblConvertTo, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(lblConvertFrom, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(txtField1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(comboBoxFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addContainerGap(421, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(lblConvertFrom)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(comboBoxFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtField1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnConvert)
.addComponent(btnReset)
.addComponent(btnExit))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(pnlAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblConvertTo))
.addComponent(comboBoxTo, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(324, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void comboBoxFromActionPerformed(java.awt.event.ActionEvent evt) {
//String lengthUnits[] = {"Centimeters", "Feet", "Inches", "Kilometers", "Light Years", "Miles", "Millimeters", "Meters", "Yards" }; //makes an array called lengthUnits then populates it
String timeUnits[] = {"Hours", "Milliseconds", "Minutes", "Seconds"};
if(comboBoxFrom.getSelectedItem().equals("Length Units")){ //if true then
comboBoxTo.setEnabled(true); //set the comboBoxTo to be activated
//DefaultComboBoxModel time = new DefaultComboBoxModel(timeUnits);
comboBoxFrom.setModel( new DefaultComboBoxModel( UNIT_NAME ) ); //sets the model of comboBoxFrom to array lengthUnits
comboBoxTo.setModel( new DefaultComboBoxModel( UNIT_NAME) ); //sets the model of comboBoxTo to array lengthUnits
}
else if(comboBoxFrom.getSelectedItem().equals("Time Units")){
comboBoxTo.setEnabled(true); //set the comboBoxTo to be activated
//DefaultComboBoxModel time = new DefaultComboBoxModel(timeUnits); //makes a new model and fills it from the array timeUnits and will link the 2 comboBox's
comboBoxFrom.setModel( new DefaultComboBoxModel( timeUnits ) ); //also makes a new model and fills it from the array timeUnits
comboBoxTo.setModel( new DefaultComboBoxModel( timeUnits ) ); //but doesn't link the oomboBox's
}
}
private void comboBoxToActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {
String comboStart[] = {"Choose one..", "Length Units", "Time Units"};
lblAnswer.setText(null); //making the lable clear
txtField1.setText(null); //making the textField clear
DefaultComboBoxModel reset = new DefaultComboBoxModel(comboStart);
comboBoxFrom.setModel(reset); //set the model of the comboBoxFrom to the default setting
comboBoxTo.setModel(reset);
comboBoxFrom.setSelectedIndex(0); // starts the comboBox at index 0. type -1 to start it empty
comboBoxTo.setSelectedIndex(0);
comboBoxTo.setEnabled(false); //deactivates the combBoxTo
lblAnswer.setText(null); //sets the lable or where answer displays, back to empty
}
private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0); //closes the program
}
private void btnConvertActionPerformed(java.awt.event.ActionEvent evt) {
String userInput = txtField1.getText(); //userInput equals to whatever the user typed in txtField1
try{
Double.parseDouble(userInput); //checks to see if the userInput from the txtField1 is an integer if not the catch takes control
for(String s: UNIT_NAME){
if(comboBoxFrom.getSelectedItem().equals(comboBoxTo.getSelectedItem())){
JOptionPane.showMessageDialog(null, "Cannot convert to the same unit type.", "Um, You Forgot Something!!", JOptionPane.INFORMATION_MESSAGE);
}
else if((comboBoxFrom.getSelectedItem().equals(s)) && comboBoxTo.getSelectedItem().equals("Centimeters")){
double answer = (Double.parseDouble(userInput) * TO_CENTM[comboBoxFrom.getSelectedIndex()]) ; //converts userInput into a type double then makes the unit conversion
lblAnswer.setText(String.valueOf(answer));
}
else if((comboBoxFrom.getSelectedItem().equals(s)) && comboBoxTo.getSelectedItem().equals("Feet")){
double answer = (Double.parseDouble(userInput) * TO_FEET[comboBoxFrom.getSelectedIndex()]) ; //converts userInput into a type double then makes the unit conversion
lblAnswer.setText(String.valueOf(answer));
}
else if((comboBoxFrom.getSelectedItem().equals(s)) && comboBoxTo.getSelectedItem().equals("Inches")){
double answer = (Double.parseDouble(userInput) * TO_INCHE[comboBoxFrom.getSelectedIndex()]) ; //converts userInput into a type double then makes the unit conversion
lblAnswer.setText(String.valueOf(answer));
}
else if((comboBoxFrom.getSelectedItem().equals(s)) && comboBoxTo.getSelectedItem().equals("Kilometers")){
double answer = (Double.parseDouble(userInput) * TO_KILOM[comboBoxFrom.getSelectedIndex()]) ; //converts userInput into a type double then makes the unit conversion
lblAnswer.setText(String.valueOf(answer));
}
else if((comboBoxFrom.getSelectedItem().equals(s)) && comboBoxTo.getSelectedItem().equals("Miles")){
double answer = (Double.parseDouble(userInput) * TO_MILE[comboBoxFrom.getSelectedIndex()]) ; //converts userInput into a type double then makes the unit conversion
lblAnswer.setText(String.valueOf(answer));
}
else if((comboBoxFrom.getSelectedItem().equals(s)) && comboBoxTo.getSelectedItem().equals("Millimeters")){
double answer = (Double.parseDouble(userInput) * TO_MILIM[comboBoxFrom.getSelectedIndex()]) ; //converts userInput into a type double then makes the unit conversion
lblAnswer.setText(String.valueOf(answer));
}
else if((comboBoxFrom.getSelectedItem().equals(s)) && comboBoxTo.getSelectedItem().equals("Meters")){
double answer = (Double.parseDouble(userInput) * TO_METER[comboBoxFrom.getSelectedIndex()]) ; //converts userInput into a type double then makes the unit conversion
lblAnswer.setText(String.valueOf(answer));
}
else if((comboBoxFrom.getSelectedItem().equals(s)) && comboBoxTo.getSelectedItem().equals("Yards")){
double answer = (Double.parseDouble(userInput) * TO_YARD[comboBoxFrom.getSelectedIndex()]) ; //converts userInput into a type double then makes the unit conversion
lblAnswer.setText(String.valueOf(answer));
}
}
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(this, "You must type a Number", "HEY IDIOT!!", JOptionPane.INFORMATION_MESSAGE);
}
}
private void txtField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @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(v3Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(v3Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(v3Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(v3Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new v3Converter().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnConvert;
private javax.swing.JButton btnExit;
private javax.swing.JButton btnReset;
private javax.swing.JComboBox<String> comboBoxFrom;
private javax.swing.JComboBox<String> comboBoxTo;
private javax.swing.JLabel lblAnswer;
private javax.swing.JLabel lblConvertFrom;
private javax.swing.JLabel lblConvertTo;
private javax.swing.JPanel pnlAnswer;
static javax.swing.JTextField txtField1;
// End of variables declaration
}
您的代码似乎在for循环中多次调用JOptionPane,您需要移动:
if(comboBoxFrom.getSelectedItem().equals(comboBoxTo.getSelectedItem())){
在for循环之前。你不想多次打这个电话。并在其后面调用return;
以退出侦听器。实际上,将其移到try块之前:
private void btnConvertActionPerformed(java.awt.event.ActionEvent evt) {
String userInput = txtField1.getText(); //userInput equals to whatever the user typed in txtField1
// check this out *** here ***
if(comboBoxFrom.getSelectedItem().equals(comboBoxTo.getSelectedItem())){
JOptionPane.showMessageDialog(null, "Cannot convert to the same unit type.",
"Um, You Forgot Something!!", JOptionPane.INFORMATION_MESSAGE);
return;
}
try{
Double.parseDouble(userInput); //checks to see if the userInput from the txtField1 is an integer if not the catch takes control
for(String s: UNIT_NAME){
if((comboBoxFrom.getSelectedItem().equals(s)) && comboBoxTo.getSelectedItem().equals("Centimeters")){
我制作了一个GUI程序,它统计在主文本字段中输入的任何内容的某些元素。如果文本字段为空,则应弹出一条消息,说明用户应在文本字段中输入文本。我做了一个if语句,如果tfMain==null,JOptionPane消息应该会弹出,但由于某些原因它不会弹出。有没有关于为什么它不会弹出的提示? 这是我的代码:
请求注册请求时出错。 路由器的req.body也打印正确 我认为创建时发生了错误 back/routes/user.js 控制台打印错误消息 错误:XMLHttpRequest的createError(createError.js:16)处出现网络错误。handleError(xhr.js:81) 岗位http://localhost:3065/api/usernet::ERR\u EMPTY\u
问题内容: 在类下运行以下代码时 我收到以下错误: 我也试过搬家 走出do-while循环但徒劳无功。 问题答案: 问题 问题是,按照javadoc的说明,您执行了该操作,将关闭流 并释放与之关联的所有系统资源 。 为了快速验证,请注释掉: 您可以随时随地回答,没有任何例外。 一个办法 一种解决方案是在所有读取终止后关闭缓冲区读取器:
问题内容: 我安装了XAMPP 1.7.4(使用PHP 5.3.5),问题是PHP不显示任何错误消息。例如,如果我使用不带参数的MYSQL连接,PHP将不会抱怨必填字段。 为什么是这样? 如何配置PHP以显示错误? 问题答案: 要在脚本级别启用错误,请在脚本顶部添加: 或者,如果它 不是 生产站点,而仅仅是开发/测试站点,则可以在php.ini中打开错误报告。搜索以下设置:
我有一个简单的GUI,它要求输入一个字符串,然后将其写入文本文件,但如果没有输入,JLabel会显示一条错误消息,我希望该错误消息保持5秒钟
给出测试URL并运行文件后,它会显示错误消息,如“uncaughtexceptionjava.lang.NoClassDefFoundError:无法初始化类org.apache.jmeter.save.SaveService。有关详细信息,请参阅日志文件。” 已创建线程组已添加http采样器已添加listner 错误消息是“未捕获异常java.lang.NoClassDefFoundError: