我的jbutton不能按要求工作吗?

时间:2016-12-09 19:21:36

标签: java interface jframe jpanel jbutton

这是我的代码,它可以像以下一样工作: 当我点击按钮" taux de change"另一个框架应该是apear并且当前框架会消失,但按钮deasn不工作,当我点击它时会出现错误信息plzzz帮助我弄错了什么是错误的????

that's my code:
package ResourceTP3;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class ConversionDevise extends JFrame implements ActionListener {

    double tt=120.0;
    Pane4 mr;

    JPanel pane1=new JPanel(), pane2=new JPanel(),pane3=new       JPanel(),pane4=new JPanel(),p=new JPanel();

    public JButton b1=new JButton("euro->dinar"),b2=new JButton("dinar- >euro"),b3=new JButton("taux de change");
    public JLabel lab1=new JLabel("dinar :"),lab2=new JLabel("euro :");
    public JTextField tx1=new JTextField(13),tx2=new JTextField(13);

    public ConversionDevise(){
        setTitle("Conversion Devise");

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);

        pane1.doLayout();
        pane1.add(lab2);
        pane1.add(tx1);

        pane2.doLayout();
        pane2.add(b1);
        pane2.add(b2);

        pane3.doLayout();
        pane3.add(lab1);
        pane3.add(tx2);

        p.doLayout();
        p.add(b3);

        pane4.setLayout(new GridLayout(4,1));
        pane4.add(pane1);
        pane4.add(pane2);
        pane4.add(pane3);
        pane4.add(p);

          setDefaultCloseOperation(EXIT_ON_CLOSE);
          getContentPane().add(pane4);
          setLocationRelativeTo(null);
          pack();
          setVisible(true);
    }

    public void actionPerformed(ActionEvent b) {
        JButton a=(JButton)b.getSource();

        if(a.equals(b1)){
            int x;
            double y;
            x=Integer.parseInt(tx1.getText());
            y=(double) (x/tt);
            String msg="votre montant est = "+y;
            JOptionPane.showConfirmDialog(null, msg,"montant aprés  changement  ",JOptionPane.PLAIN_MESSAGE);   
        }
        else{
            int x;
            double y;
            x=Integer.parseInt(tx2.getText());
            y=(double) (x*tt);
            String msg="votre montant est = "+y;
            JOptionPane.showConfirmDialog(null, msg,"montant aprés changement ",JOptionPane.PLAIN_MESSAGE); 
        }


        if(a.equals(b3)){
            this.removeAll();

            mr=new Pane4();
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            getContentPane().add(mr);
              setLocationRelativeTo(null);
             pack();
             setVisible(true);

            }

        if(a.equals(b)){
            tt=Integer.parseInt(mr.t.getText());
            }

        }


    public static void main(String[] args) {
        new ConversionDevise();
    }
}

that's the error that comes when i click on the button

1 个答案:

答案 0 :(得分:0)

尝试更改JButton a =(JButton)b.getSource(); to String a = b.getActionCommand();并且在if条件而不是if(a.equals(b3))中写if(a.equals(“taux de change”))。它会起作用。