如何在单击另一个Jbutton时更改Jbutton的功能?

时间:2015-03-15 07:45:02

标签: java

我正在编写一个程序,它将体重换算为千克重量,以千克换算成磅重量。输出窗口如下:

enter image description here

当我想将Kilograms转换为Pounds时,我点击开关按钮,输出变化如下:

enter image description here

但问题是,当我点击“转换”时,它仍会将重量转换为千克而不是磅。

我想在单击“切换”按钮时更改“转换”按钮的功能,以便将千克的重量转换为磅。请帮忙。

我的源代码如下:

public class convertApp extends JFrame implements ActionListener {
private JLabel poundlbl = new JLabel("Weight in Pounds");
private JLabel kglbl = new JLabel("Weight in Kilograms");

private JTextField poundbx= new JTextField(12);
private JTextField kgbx= new JTextField(12);

private JButton conbtn=new JButton("Convert");
private JButton switchbtn=new JButton("Switch");
private JButton newconbtn=new JButton("Convert");
private JPanel bxPanel = new JPanel();

public convertApp(){
    super("Weight Converter");
    bxPanel.setLayout(new GridLayout(5,29,5,5));
    bxPanel.add(poundlbl);
    bxPanel.add(poundbx);
    bxPanel.add(kglbl);
    bxPanel.add(kgbx);
    bxPanel.add(conbtn);
    bxPanel.add(switchbtn);

    //bxPanel.setBackground(Color.gray);



    this.setLayout(new GridLayout(1,1,0,0));
    this.add(bxPanel);



    this.setVisible(true);
    //what to do if i close
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //put window in the center
    this.setLocationRelativeTo(null);
    //disable resize
    this.setResizable(false);
    //pack all the components within the window
    this.pack();

    conbtn.addActionListener(this);
    switchbtn.addActionListener(this);


}

public void actionPerformed(ActionEvent evnt){
   if(evnt.getSource()==conbtn){
        String poundtext = poundbx.getText();
        int pound = Integer.parseInt(poundtext);

        double kilo = pound * 0.453592;

        kgbx.setText(""+kilo);
    }

   else if(evnt.getSource()==switchbtn)
    {
        poundlbl.setText("Weight in Kilograms:");
        kglbl.setText("Weight in Pounds:");

        if(evnt.getSource()==conbtn){
        String kilotext = poundbx.getText();
        int kilo = Integer.parseInt(kilotext);

        double pound = kilo * 2.20462;

        kgbx.setText(""+pound);
        }

    }


}

}

3 个答案:

答案 0 :(得分:0)

您无法更改功能本身。当您单击“开关”时,您可以创建一个设置true或false(您的选择)的标记。按钮。然后在点击“转换”时检查该标记的值。并根据您的标志惯例执行适当的转换。

Boolean isPtoK = true;

// [...]

if(isPtoK){
  // convert pounds to kilos
} else{
  // kilos to pounds
}

我会创建一个单独的函数,在'切换'时切换标记的值。点击。

答案 1 :(得分:0)

创建布尔变量

boolean switch=true;

void kiloGramToPound{
//Convert Codes Here
}

void PoundToKiloGram{
//Convert Codes Here
}

转换按钮actionPerformed

if(switch==true){
     kiloGramToPound();
}else{
     PoundToKiloGram();
}

切换按钮actionPerformed

if(switch==true){
    switch=false;
}else{
  switch=true;
}

答案 2 :(得分:0)

不回答您的问题表示抱歉,但我建议您尝试使用KeyListener / KeyAdapter而不是ActionListener和按钮的组合。

示例:

textField1.addKeyListener(new ConverterListener(textField2, "2.20462"));
textField2.addKeyListener(new ConverterListener(textField1, "0.453592"));


class ConverterListener extends KeyAdapter {

    JTextField destination;
    String multiplier;

    public ConverterListener(JTextField destination, String multiplier) {
        this.destination = destination;
        this.multiplier = multiplier;
    }

    @Override
    public void keyReleased(KeyEvent e) {
        JTextField source = (JTextField) e.getSource();
        String result = convert(source.getText(), multiplier);
        destination.setText(result);
    }

    private String convert(String value, String multiplier) {
        String result = "0";

        try {
            double dblValue = value.isEmpty() ? 0d : Double.parseDouble(value);
            double dblMultiplier = value.isEmpty() ? 0d : Double.parseDouble(multiplier);

            result = Double.toString(dblValue * dblMultiplier);
        } catch (Exception e) {
            System.out.println(e);
        }

        return result;
    }

}