好吧所以我完成了我以前的程序,它工作正常,但它说最后一行代码中有一个错误,它标识了我的标题jTextField
package trigonomityhelper;
/*This program was mae by andrew cameron
*on september 25th 2016
*to help people with trigonometric functions
*/
public class TrigonomityHelperUI extends javax.swing.JFrame {
/**
* Creates new form TrigonomityHelperUI
*/
public TrigonomityHelperUI() {
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() {
title = new javax.swing.JLabel();
sin = new javax.swing.JButton();
cos = new javax.swing.JButton();
tan = new javax.swing.JButton();
csc = new javax.swing.JButton();
sec = new javax.swing.JButton();
cot = new javax.swing.JButton();
asin = new javax.swing.JButton();
acos = new javax.swing.JButton();
atan = new javax.swing.JButton();
acsc = new javax.swing.JButton();
asec = new javax.swing.JButton();
acot = new javax.swing.JButton();
calculate = new javax.swing.JButton();
in = new javax.swing.JTextField();
out = new javax.swing.JLabel();
background = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMaximumSize(new java.awt.Dimension(400, 330));
setMinimumSize(new java.awt.Dimension(400, 330));
setPreferredSize(new java.awt.Dimension(400, 330));
setResizable(false);
getContentPane().setLayout(null);
title.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
title.setForeground(new java.awt.Color(0, 0, 255));
title.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
title.setText("<html>Trigonometry <br> Helper</html>");
getContentPane().add(title);
title.setBounds(120, 10, 100, 50);
sin.setText("sin");
sin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sinActionPerformed(evt);
}
});
getContentPane().add(sin);
sin.setBounds(240, 23, 70, 20);
cos.setText("cos");
cos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cosActionPerformed(evt);
}
});
getContentPane().add(cos);
cos.setBounds(240, 43, 70, 20);
tan.setText("tan");
tan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tanActionPerformed(evt);
}
});
getContentPane().add(tan);
tan.setBounds(240, 63, 70, 20);
csc.setText("csc");
csc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cscActionPerformed(evt);
}
});
getContentPane().add(csc);
csc.setBounds(240, 83, 70, 20);
sec.setText("sec");
sec.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
secActionPerformed(evt);
}
});
getContentPane().add(sec);
sec.setBounds(240, 103, 70, 20);
cot.setText("cot");
cot.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cotActionPerformed(evt);
}
});
getContentPane().add(cot);
cot.setBounds(240, 123, 70, 20);
asin.setText("asin");
asin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
asinActionPerformed(evt);
}
});
getContentPane().add(asin);
asin.setBounds(310, 23, 70, 20);
acos.setText("acos");
acos.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
acosActionPerformed(evt);
}
});
getContentPane().add(acos);
acos.setBounds(310, 43, 70, 20);
atan.setText("atan");
atan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
atanActionPerformed(evt);
}
});
getContentPane().add(atan);
atan.setBounds(310, 63, 70, 20);
acsc.setText("acsc");
acsc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
acscActionPerformed(evt);
}
});
getContentPane().add(acsc);
acsc.setBounds(310, 83, 70, 20);
asec.setText("asec");
asec.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
asecActionPerformed(evt);
}
});
getContentPane().add(asec);
asec.setBounds(310, 103, 70, 20);
acot.setText("acot");
acot.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
acotActionPerformed(evt);
}
});
getContentPane().add(acot);
acot.setBounds(310, 123, 70, 20);
calculate.setText("calculate");
calculate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calculateActionPerformed(evt);
}
});
getContentPane().add(calculate);
calculate.setBounds(300, 150, 80, 23);
in.setHorizontalAlignment(javax.swing.JTextField.CENTER);
getContentPane().add(in);
in.setBounds(40, 240, 300, 30);
out.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
getContentPane().add(out);
out.setBounds(10, 270, 360, 20);
background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/trigonomityhelper/TrigAngles.gif"))); // NOI18N
background.setToolTipText("Type the number which you need to calculate, select the calculation type, and then click calculate to get your answer.");
getContentPane().add(background);
background.setBounds(0, -40, 400, 340);
pack();
}// </editor-fold>
// define global variables
String sInput;
int type = 0;
double input, output;
// make custom selection/recoloration button method
private void select (int a, int b, int c) {
//recolor selected button
switch (a) {
case 1: sin.setForeground(new java.awt.Color(0, 0, 255));
break;
case 2: cos.setForeground(new java.awt.Color(0, 0, 255));
break;
case 3: tan.setForeground(new java.awt.Color(0, 0, 255));
break;
case 4: csc.setForeground(new java.awt.Color(0, 0, 255));
break;
case 5: sec.setForeground(new java.awt.Color(0, 0, 255));
break;
case 6: cot.setForeground(new java.awt.Color(0, 0, 255));
break;
case 7: asin.setForeground(new java.awt.Color(0, 0, 255));
break;
case 8: acos.setForeground(new java.awt.Color(0, 0, 255));
break;
case 9: atan.setForeground(new java.awt.Color(0, 0, 255));
break;
case 10:acsc.setForeground(new java.awt.Color(0, 0, 255));
break;
case 11:asec.setForeground(new java.awt.Color(0, 0, 255));
break;
case 12:acot.setForeground(new java.awt.Color(0, 0, 255));
break;
default:break;
}
//set type equal to b
type = b;
//recolor last button black
switch (c) {
case 1: sin.setForeground(new java.awt.Color(0, 0, 0));
break;
case 2: cos.setForeground(new java.awt.Color(0, 0, 0));
break;
case 3: tan.setForeground(new java.awt.Color(0, 0, 0));
break;
case 4: csc.setForeground(new java.awt.Color(0, 0, 0));
break;
case 5: sec.setForeground(new java.awt.Color(0, 0, 0));
break;
case 6: cot.setForeground(new java.awt.Color(0, 0, 0));
break;
case 7: asin.setForeground(new java.awt.Color(0, 0, 0));
break;
case 8: acos.setForeground(new java.awt.Color(0, 0, 0));
break;
case 9: atan.setForeground(new java.awt.Color(0, 0, 0));
break;
case 10:acsc.setForeground(new java.awt.Color(0, 0, 0));
break;
case 11:asec.setForeground(new java.awt.Color(0, 0, 0));
break;
case 12:acot.setForeground(new java.awt.Color(0, 0, 0));
break;
default:break;
}
}
private void sinActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select sine
select(1, 1, type);
}
private void cosActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select cosine
select(2, 2, type);
}
private void tanActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select tangent
select(3, 3, type);
}
private void cscActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select cosecant
select(4, 4, type);
}
private void secActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select secant
select(5, 5, type);
}
private void cotActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select cotangent
select(6, 6, type);
}
private void asinActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select arcsine
select(7, 7, type);
}
private void acosActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select arccosine
select(8, 8, type);
}
private void atanActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select arctangent
select(9, 9, type);
}
private void acscActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select arcsecant
select(10, 10, type);
}
private void asecActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select arccosecant
select(11, 11, type);
}
private void acotActionPerformed(java.awt.event.ActionEvent evt) {
//set color to blue and all other colors to balck, select arccotangent
select(12, 12, type);
}
private void calculateActionPerformed(java.awt.event.ActionEvent evt) {
//use selected method to run the apropriate calculation and output an answer
if (type == 0) {
out.setText("No conversion method was selected");
} else {
sInput = in.getText();
in.setText("");
try {
input = Double.parseDouble(sInput);
} catch (NumberFormatException e) {
out.setText("The input was not a number");
return;
}
switch (type) {
case 1: output = Math.sin(input);
out.setText("The sine of "+input+" is "+output);
break;
case 2: output = Math.cos(input);
out.setText("The cosine of "+input+" is "+output);
break;
case 3: output = Math.tan(input);
out.setText("The tangent of "+input+" is "+output);
break;
case 4: output = (1/Math.sin(input));
out.setText("The secant of "+input+" is "+output);
break;
case 5: output = (1/Math.cos(input));
out.setText("The cosecant of "+input+" is "+output);
break;
case 6: output = (1/Math.tan(input));
out.setText("The cotangent of "+input+" is "+output);
break;
case 7: output = Math.asin(input);
out.setText("The arcsine of "+input+" is "+output+" Radians");
break;
case 8: output = Math.acos(input);
out.setText("The arccosine of "+input+" is "+output+" Radians");
break;
case 9: output = Math.atan(input);
out.setText("The arctangent of "+input+" is "+output+" Radians");
break;
case 10:output = Math.pow((1/Math.asin(input)),-1);
out.setText("The arcsecant of "+input+" is "+output+" Radians");
break;
case 11:output = Math.pow((1/Math.acos(input)),-1);
out.setText("The arccosecant of "+input+" is "+output+" Radians");
break;
case 12:output = Math.pow((1/Math.atan(input)),-1);
out.setText("The arccotangent of "+input+" is "+output+" Radians");
break;
default:out.setText("No calculation type has been chosen");
break;
}
}
}
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(TrigonomityHelperUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TrigonomityHelperUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TrigonomityHelperUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TrigonomityHelperUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new TrigonomityHelperUI().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton acos;
private javax.swing.JButton acot;
private javax.swing.JButton acsc;
private javax.swing.JButton asec;
private javax.swing.JButton asin;
private javax.swing.JButton atan;
private javax.swing.JLabel background;
private javax.swing.JButton calculate;
private javax.swing.JButton cos;
private javax.swing.JButton cot;
private javax.swing.JButton csc;
private javax.swing.JTextField in;
private javax.swing.JLabel out;
private javax.swing.JButton sec;
private javax.swing.JButton sin;
private javax.swing.JButton tan;
private javax.swing.JLabel title;//this is the faulty line of code
// End of variables declaration
所以现在我的问题是,我如何使最后一行代码不是&#34;错误&#34;,因为该程序工作正常。
答案 0 :(得分:2)
您不需要使用NetBean&#34;自动生成&#34;的ActionListeners。而是创建拥有 ActionListener,将其添加到您要检查的所有JButton,并在侦听器中,通过侦听器的ActionEvent参数获取推送按钮,特别是getSource()
方法
例如:
ActionListener buttonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton source = (JButton) e.getSource(); // here's the button that was pressed
source.setForeground(new java.awt.Color(0, 0, 255));
}
}
当然也是如此,为了让这个听众工作,你需要将它添加到JButtons。