这是我的货币转换器计划代码。 早期的空指针异常发生,但它已被纠正。现在程序中没有错误。程序运行但转换按钮不执行任何操作。我认为问题在于ActionPerformed功能,但我无法识别它。
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class Trial extends JFrame implements ActionListener{
public String[] list={"DOLLARS","EUROS ","YEN","POUNDS","RUPEES"};
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
public JComboBox cb,cb1;
public JTextField tf1;
public JTextField tf2;
public JLabel label3;
public JLabel label4;
JPanel panel3 = new JPanel();
public JButton button1;
public JButton button2;
public Trial(){
setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)
);
JLabel label1 = new JLabel("FROM :");
label1.setFont(new Font("Serif", Font.BOLD, 15));
JComboBox cb = new JComboBox();
cb= new JComboBox(list);
cb.getSelectedIndex();
panel1.setBackground(Color.RED);
panel1.add(label1);
panel1.add(cb);
JLabel label2 = new JLabel("TO :");
label2.setFont(new Font("Serif", Font.BOLD, 15));
JComboBox cb1 = new JComboBox();
cb1=new JComboBox(list);
cb1.getSelectedIndex();
cb.addActionListener(this);
cb1.addActionListener(this);
panel1.add(label2);
panel1.add(cb1);
add(panel1);
JLabel label3 = new JLabel("\n\nENTER THE AMOUNT :");
label3.setFont(new Font("Serif", Font.BOLD, 15));
JTextField tf1 = new JTextField(15);
// label3.setHorizontalAlignment(SwingConstants.CENTER);
// label3.setVerticalAlignment(SwingConstants.CENTER);
panel2.add(label3);
panel2.add(tf1);
JLabel label4 = new JLabel("CONVERTED AMOUNT :");
label4.setFont(new Font("Serif", Font.BOLD, 15));
label4.setDisplayedMnemonic(KeyEvent.VK_O);
JTextField tf2 = new JTextField(15);
tf1.addActionListener(this);
tf2.addActionListener(this);
panel2.add(label4);
panel2.add(tf2);
panel2.setBackground(Color.BLUE);
add(panel2);
JButton button1 = new JButton("CONVERT");
button1.setFont(new Font("Serif", Font.BOLD, 15));
button1.setMnemonic(KeyEvent.VK_K);
panel3.add(button1);
JButton button2 = new JButton("CLEAR ");
button2.setFont(new Font("Serif", Font.BOLD, 15));
button2.setMnemonic(KeyEvent.VK_C);
button1.addActionListener(this);
button2.addActionListener(this);
panel3.add(button2);
panel3.setBackground(Color.GREEN);
add(panel3);
}
public void actionPerformed(ActionEvent e) {
double a,b=0;
Object src = e.getSource();
if(src.equals(button1)){
a=Double.valueOf(tf1.getText());
try{
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==1)
{b=a*0.89;}
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==2)
{b=a*124.75;}
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==3)
{b=a*0.65;}
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==4)
{b=a*64.08;}
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==0)
{b=a*1.13;}
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==2)
{b=a*140.49;}
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==3)
{b=a*0.74;}
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==4)
{b=a*71.34;}
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==0)
{b=a*0.0080;}
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==1)
{b=a*0.0071;}
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==3)
{b=a*0.0052;}
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==4)
{b=a*0.51;}
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==0)
{b=a*1.53;}
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==1)
{b=a*1.36;}
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==2)
{b=a*191.26;}
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==4)
{b=a*97.88;}
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==0)
{b=a*0.0156;}
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==1)
{ b=a*0.014;}
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==2)
{b=a*1.9607;}
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==3)
{b=a*0.0108;}
tf2.setText(String.valueOf(b));
}catch(Exception x){System.out.println("Error");}
}
if(src.equals(button2)){
tf1.setText("0000");
tf2.setText("0000");
}
}
}
答案 0 :(得分:1)
你是shadowing变量button1
。替换
JButton button1 = new JButton("CONVERT");
带
button1 = new JButton("CONVERT");
答案 1 :(得分:0)
使用以下代码:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Trial extends JFrame implements ActionListener
{
public String[] list={"DOLLARS","EUROS ","YEN","POUNDS","RUPEES"};
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
public JComboBox cb,cb1;
public JTextField tf1;
public JTextField tf2;
public JLabel label3;
public JLabel label4;
JPanel panel3 = new JPanel();
public JButton button1;
public JButton button2;
public Trial(){
setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)
);
JLabel label1 = new JLabel("FROM :");
label1.setFont(new Font("Serif", Font.BOLD, 15));
cb = new JComboBox();
cb= new JComboBox(list);
cb.getSelectedIndex();
panel1.setBackground(Color.RED);
panel1.add(label1);
panel1.add(cb);
JLabel label2 = new JLabel("TO :");
label2.setFont(new Font("Serif", Font.BOLD, 15));
cb1 = new JComboBox();
cb1=new JComboBox(list);
cb1.getSelectedIndex();
cb.addActionListener(this);
cb1.addActionListener(this);
panel1.add(label2);
panel1.add(cb1);
add(panel1);
label3 = new JLabel("\n\nENTER THE AMOUNT :");
label3.setFont(new Font("Serif", Font.BOLD, 15));
tf1 = new JTextField(15);
// label3.setHorizontalAlignment(SwingConstants.CENTER);
// label3.setVerticalAlignment(SwingConstants.CENTER);
panel2.add(label3);
panel2.add(tf1);
label4 = new JLabel("CONVERTED AMOUNT :");
label4.setFont(new Font("Serif", Font.BOLD, 15));
label4.setDisplayedMnemonic(KeyEvent.VK_O);
tf2 = new JTextField(15);
tf1.addActionListener(this);
tf2.addActionListener(this);
panel2.add(label4);
panel2.add(tf2);
panel2.setBackground(Color.BLUE);
add(panel2);
button1 = new JButton("CONVERT");
button1.setFont(new Font("Serif", Font.BOLD, 15));
button1.setMnemonic(KeyEvent.VK_K);
panel3.add(button1);
button2 = new JButton("CLEAR ");
button2.setFont(new Font("Serif", Font.BOLD, 15));
button2.setMnemonic(KeyEvent.VK_C);
button1.addActionListener(this);
button2.addActionListener(this);
panel3.add(button2);
panel3.setBackground(Color.GREEN);
add(panel3);
}
public void actionPerformed(ActionEvent e) {
double a,b=0;
Object src = e.getSource();
if(src == button1){
a=Double.valueOf(tf1.getText());
try{
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==1)
{b=a*0.89;}
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==2)
{b=a*124.75;}
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==3)
{b=a*0.65;}
if(cb.getSelectedIndex()==0 && cb1.getSelectedIndex()==4)
{b=a*64.08;}
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==0)
{b=a*1.13;}
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==2)
{b=a*140.49;}
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==3)
{b=a*0.74;}
if(cb.getSelectedIndex()==1 && cb1.getSelectedIndex()==4)
{b=a*71.34;}
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==0)
{b=a*0.0080;}
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==1)
{b=a*0.0071;}
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==3)
{b=a*0.0052;}
if(cb.getSelectedIndex()==2 && cb1.getSelectedIndex()==4)
{b=a*0.51;}
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==0)
{b=a*1.53;}
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==1)
{b=a*1.36;}
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==2)
{b=a*191.26;}
if(cb.getSelectedIndex()==3 && cb1.getSelectedIndex()==4)
{b=a*97.88;}
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==0)
{b=a*0.0156;}
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==1)
{ b=a*0.014;}
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==2)
{b=a*1.9607;}
if(cb.getSelectedIndex()==4 && cb1.getSelectedIndex()==3)
{b=a*0.0108;}
tf2.setText(String.valueOf(b));
}catch(Exception x){System.out.println("Error");}
}
if(src.equals(button2)){
tf1.setText("0000");
tf2.setText("0000");
}
}
}
代码问题是,有很多局部变量,其名称与实例变量相同。那些局部变量使实例变量隐藏起来。在构造函数中,您编写了类似JComboBox cb = new JComboBox();
的语句。此cb变量是一个局部变量。语句panel1.add(cb);
将一个本地cb变量添加到panel1。但是在action cb.getSelectedIndex()==0
执行它时使用声明为public JComboBox cb,cb1;
的实例变量。因为这个实例变量永远不会添加到gui。 comboBox用户选择的是本地可变cb而不是实例变量cb。