Java中如何将下拉菜单中的字符串数据提取出来运用到文本框的数字运算显示中?

图片说明
/*上部的功能已经实现了,但是下面除了那个复选框,下拉菜单的值我还是没办法运用到文本框中,ActionEvent只能将字符串直接传给文本框,不能提取实现数字运算,而且方法也不支持return返回值,抛异常我也尝试过,但是接口的的实现上又出了毛病,实在没辙了,求问解决办法,感激不尽
/
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import java.text.ParseException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;

class layout extends JFrame implements ActionListener
{
int propor;
String help;
private JPanel mb1,mb2,mb3;
JLabel bq1,bq2,bq3,bq3_5,bq4,bq5,bq6;
private JFormattedTextField rmb;
private JFormattedTextField coupon;
private JFormattedTextField gold;
private JFormattedTextField gold_2;
private JFormattedTextField exp;

private JComboBox pro;
JCheckBox link;
public layout()
{
    super("坦克世界计算器");

    mb1 = new JPanel();
    mb2 = new JPanel();

    rmb= new JFormattedTextField(new java.text.DecimalFormat("#0"));
    coupon= new JFormattedTextField(new java.text.DecimalFormat("#0"));
    gold= new JFormattedTextField(new java.text.DecimalFormat("#0"));
    gold_2= new JFormattedTextField(new java.text.DecimalFormat("#0"));
    exp= new JFormattedTextField(new java.text.DecimalFormat("#0"));

    rmb.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
    coupon.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
    gold.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
    gold_2.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
    exp.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

    rmb.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            String old = rmb.getText();
            JFormattedTextField.AbstractFormatter formatter = rmb.getFormatter();
            if (!old.equals("")) { 
                if (formatter != null) {
                    String str = rmb.getText();
                    try {
                        long page = (Long) formatter.stringToValue(str);
                        coupon.setText(20*page + "");
                        gold.setText(40*page + "");
                    } catch (ParseException pe) {
                        rmb.setText("0");
                    }
                }
            }
        }
    });
    coupon.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            String old = coupon.getText();
            JFormattedTextField.AbstractFormatter formatter = coupon.getFormatter();
            if (!old.equals("")) { 
                if (formatter != null) {
                    String str = coupon.getText();
                    try {
                        long page = (Long) formatter.stringToValue(str);
                        rmb.setText(page/20 + "");
                        gold.setText(page/20 + "");
                    } catch (ParseException pe) {
                        coupon.setText("0");
                    }
                }
            }
        }
    });
    gold.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            String old = gold.getText();
            JFormattedTextField.AbstractFormatter formatter = gold.getFormatter();
            if (!old.equals("")) { 
                if (formatter != null) {
                    String str = gold.getText();
                    try {
                        long page = (Long) formatter.stringToValue(str);
                        rmb.setText(page/40 + "");
                        coupon.setText(page/20 + "");
                    } catch (ParseException pe) {
                        gold.setText("0");
                    }
                }
            }
        }
    });
    gold_2.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            String old = gold_2.getText();
            JFormattedTextField.AbstractFormatter formatter = gold_2.getFormatter();
            if (!old.equals("")) { 
                if (formatter != null) {
                    String str = gold_2.getText();
                    try {
                        long page = (Long) formatter.stringToValue(str);
                        exp.setText(page*propor + "");
                    } catch (ParseException pe) {
                        gold_2.setText("0");
                    }
                }
            }
        }
    });
    exp.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            String old = exp.getText();
            JFormattedTextField.AbstractFormatter formatter = exp.getFormatter();
            if (!old.equals("")) { 
                if (formatter != null) {
                    String str = exp.getText();
                    try {
                        long page = (Long) formatter.stringToValue(str);
                        gold_2.setText(page/propor + "");
                    } catch (ParseException pe) {
                        exp.setText("0");
                    }
                }
            }
        }
    });


    bq1=new JLabel("人民币",SwingConstants.CENTER);
    bq2=new JLabel("点券",SwingConstants.CENTER);
    bq3=new JLabel("金币",SwingConstants.CENTER);
    bq3_5=new JLabel("       ");
    bq4=new JLabel("金币",SwingConstants.CENTER);
    bq5=new JLabel("兑换比例",SwingConstants.CENTER);
    bq6=new JLabel("经验",SwingConstants.CENTER);

    pro= new JComboBox();
    pro.setModel(new DefaultComboBoxModel(new String[] { "1:40", "1:35", "1:25"}));
    pro.setActionCommand("1");//设置动作命令
    pro.addActionListener(this);//设置事件响应器

    link=new JCheckBox("是否链接");

    mb1.setLayout(new GridLayout(2,3,60,0));
    mb2.setLayout(new GridLayout(2,4,10,0));

    mb1.add(bq1);  mb1.add(bq2);  mb1.add(bq3);
    mb1.add(rmb);  mb1.add(coupon);  mb1.add(gold);
    mb2.add(bq3_5);mb2.add(bq4);  mb2.add(bq5);  mb2.add(bq6);          
    mb2.add(link);  mb2.add(gold_2);  mb2.add(pro);  mb2.add(exp); 

    this.setLayout(new GridLayout(2,1));
    this.add(mb1);
    this.add(mb2);


    this.setBounds(300, 280, 400, 200);//相当于setLocation+setSize
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){//当鼠标选择下拉菜单的值时
    String comd =e.getActionCommand();//得到动作命令
    if(comd.equals("1")){
        help=((String) pro.getSelectedItem());//设置文本框的内容
    }
}

}

public class calculator {
public static void main(String[] args)
{
new layout().setVisible(true);
}
}

/*一般来说不可以,可以考虑用异常来达到目的,如:
class MyException extends Exception
{
public MyException(String message)
{
super(message);
}
}
class MyClass
{
public void doSomething()throws MyException
{
//do something
String mystr="mystr"
throw new MyException(mystr);
}
}*/

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐