Java 从JComboBox转换为JTextField

Java 从JComboBox转换为JTextField,java,swing,Java,Swing,我知道这是个问题。因为我试图将JComboBox转换为JTextField,但这不起作用。我在网上找不到如何做到这一点。有人能帮忙吗 origPrice是一个jtextfield,discombo是组合框,salesPrice也是一个jtextfield。我希望能够将用户输入的原始价格乘以,然后乘以他们从下拉组合框中获得的所选项目,然后将结果输入salesPrice jtextfield无需将JComboBox转换为任何内容,因为您最好不要使用它,只需提取它所包含的数据当你想要的时候,它会保持不

我知道这是个问题。因为我试图将JComboBox转换为JTextField,但这不起作用。我在网上找不到如何做到这一点。有人能帮忙吗


origPrice是一个jtextfield,discombo是组合框,salesPrice也是一个jtextfield。我希望能够将用户输入的原始价格乘以,然后乘以他们从下拉组合框中获得的所选项目,然后将结果输入salesPrice jtextfield

无需将JComboBox转换为任何内容,因为您最好不要使用它,只需提取它所包含的数据当你想要的时候,它会保持不变。这可以通过
getSelectedItem()
完成,检查它是否为空,然后使用它

例如:

 origPrice * discCombo = salesPrice;
导入java.awt.event.ActionEvent;
导入java.text.DecimalFormat;
导入java.text.NumberFormat;
导入javax.swing.*;
公共类ComboDemo扩展了JPanel{
私有整数[]项={0,1,2,3,4,5,6,7,8,9,10};
private DefaultComboxModel ComboxModel=新的DefaultComboxModel(项目);
私有JComboBox组合=新JComboBox(组合模型);
私有JFormattedTextField orgPriceField=新的JFormattedTextField(新的十进制格式(“0.00”);
private JFormattedTextField finalPriceField=new JFormattedTextField(NumberFormat.getCurrencyInstance());
公共组合演示(){
finalPriceField.setFocusable(假);
orgPriceField.setColumns(10);
最终价格字段设置列(10);
orgPriceField.setText(“0.00”);
添加(orgPriceField);
添加(新的JLabel(“x”);
添加(组合);
添加(新JLabel(“=”);
添加(最终价格字段);
添加(新的JButton(新的CalculateAction());
}
私有类CalculateAction扩展了AbstractAction{
公共计算(){
超级(“计算”);
}
@凌驾
已执行的公共无效操作(操作事件e){
数字orgPrice=(数字)orgPriceField.getValue();
整数乘数=((整数)comboModel.getSelectedItem()).intValue();
double result=orgPrice.doubleValue()*乘数;
finalPriceField.setValue(结果);
}
}
私有静态void createAndShowGui(){
ComboDemo mainPanel=新建ComboDemo();
JFrame=新JFrame(“ComboDemo”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGui();
}
});
}
}

这里不清楚您要问的是什么,请尝试通过添加上下文来开发,您已经尝试了什么,以及它如何不起作用JComboBox不能转换为JTextField。但是您可以设置它们的内容。还要提到哪个是JTextField。如果“salesPrice”是JTextField,那么您必须这样做。salesPrice.setText((整数)discCombo.getSelectedItem())*origPrice+“”;
import java.awt.event.ActionEvent;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.*;

public class ComboDemo extends JPanel {
   private Integer[] items = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   private DefaultComboBoxModel<Integer> comboModel = new DefaultComboBoxModel<>(items);
   private JComboBox<Integer> combo = new JComboBox<>(comboModel);
   private JFormattedTextField orgPriceField = new JFormattedTextField(new DecimalFormat("0.00")); 
   private JFormattedTextField finalPriceField = new JFormattedTextField(NumberFormat.getCurrencyInstance());

   public ComboDemo() {
      finalPriceField.setFocusable(false);
      orgPriceField.setColumns(10);
      finalPriceField.setColumns(10);
      orgPriceField.setText("0.00");

      add(orgPriceField);
      add(new JLabel("x"));
      add(combo);
      add(new JLabel("="));
      add(finalPriceField);

      add(new JButton(new CalculateAction()));
   }

   private class CalculateAction extends AbstractAction {

      public CalculateAction() {
         super("Calculate");
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         Number orgPrice = (Number) orgPriceField.getValue();
         Integer multiplier = ((Integer) comboModel.getSelectedItem()).intValue();

         double result = orgPrice.doubleValue() * multiplier;
         finalPriceField.setValue(result);
      }
   }

   private static void createAndShowGui() {
      ComboDemo mainPanel = new ComboDemo();

      JFrame frame = new JFrame("ComboDemo");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}