Java 如何声明变量来保存JTextField的值?

Java 如何声明变量来保存JTextField的值?,java,swing,Java,Swing,我正在为食品订购GUI编写代码。我想问几个问题。我想问,我应该如何声明变量来保存tfPrice1,tfPrice2,tfPrice3的值?我应该怎么做才能使“下订单”按钮在按下时汇总JTextFields中包含的值?下面是我的代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FoodOrder1 extends JFrame { JButton riceBtn,noodle

我正在为食品订购GUI编写代码。我想问几个问题。我想问,我应该如何声明变量来保存
tfPrice1
tfPrice2
tfPrice3
的值?我应该怎么做才能使“下订单”按钮在按下时汇总
JTextField
s中包含的值?下面是我的代码

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

public class FoodOrder1 extends JFrame
{
    JButton riceBtn,noodleBtn,soupBtn;
    JTextField display,total,tfPrice1,tfPrice2,tfPrice3;
    JPanel mp,p1,p2,p3,p4,p5,p6,p7,p8;
    JLabel dsp,ttl,rLbl,nLbl,sLbl,prc1,prc2,prc3;
    int rice=3 , noodle=3 , soup=4;
    int Total , price1=Integer.parseInt(tfPrice1), price2=Integer.parseInt(tfPrice2) , price3=Integer.parseInt(tfPrice3);
    public FoodOrder1()
    {
        Container pane = getContentPane();
        mp = new JPanel();
        mp.setLayout(new GridLayout(14,1));
        pane.add(mp);

        p1 = new JPanel();
        p1.setLayout(new GridLayout(1,1));
        rLbl = new JLabel("Rice");
        rLbl.setFont(new Font("Myraid Pro",Font.BOLD,14));
        riceBtn = new JButton("Fried Rice " + "RM3");
        p1.add(riceBtn);
        riceBtn.addActionListener(new MyAction());

        p1.add(rLbl);
        p1.add(riceBtn);

        p2 = new JPanel();
        p2.setLayout(new GridLayout(1,2));
        nLbl = new JLabel("Noodle");
        nLbl.setFont(new Font("Myraid Pro",Font.BOLD,14));
        noodleBtn = new JButton("Tomato Noodle " + "RM3");
        noodleBtn.addActionListener(new MyAction());

        p2.add(nLbl);
        p2.add(noodleBtn);

        p3 = new JPanel();
        p3.setLayout(new GridLayout(1,2));
        sLbl = new JLabel("Soup");
        sLbl.setFont(new Font("Myraid Pro",Font.BOLD,14));
        soupBtn = new JButton("Tomyam Soup " + "RM4");
        soupBtn.addActionListener(new MyAction());
        p3.add(sLbl);
        p3.add(soupBtn);

        p4 = new JPanel();
        p4.setLayout(new GridLayout(1,2));
        prc1 = new JLabel("Price of Fried Rice");
        prc1.setFont(new Font("Myraid Pro",Font.BOLD,14));
        tfPrice1 = new JTextField(10);

        p4.add(prc1);
        p4.add(tfPrice1);
        tfPrice1.setEditable(false);

        p5 = new JPanel();
        p5.setLayout(new GridLayout(1,2));
        prc2 = new JLabel("Price of Tomato Noodle");
        prc2.setFont(new Font("Myraid Pro",Font.BOLD,14));
        tfPrice2 = new JTextField(10);

        p5.add(prc2);
        p5.add(tfPrice2);
        tfPrice2.setEditable(false);

        p6 = new JPanel();
        p6.setLayout(new GridLayout(1,2));
        prc3 = new JLabel("Price of Tomyam Soup");
        prc3.setFont(new Font("Myraid Pro",Font.BOLD,14));
        tfPrice3 = new JTextField(10);

        p6.add(prc3);
        p6.add(tfPrice3);
        tfPrice3.setEditable(false);

        p7 = new JPanel();
        p7.setLayout(new FlowLayout());
        poBtn = new JButton("Place Order");
        poBtn.setFont(new Font("Myraid Pro",Font.PLAIN,14));
        poBtn.addActionListener(new MyAction2());
        rstBtn = new JButton("Reset");
        rstBtn.setFont(new Font("Myraid Pro",Font.PLAIN,14));
        rstBtn.addActionListener(new MyAction3());

        p7.add(poBtn);
        p7.add(rstBtn);

        p8 = new JPanel();
        p8.setLayout(new GridLayout(1,2));
        ttl = new JLabel("Total (RM)");
        ttl.setFont(new Font("Myraid Pro",Font.BOLD,14));
        total = new JTextField(10);

        p8.add(ttl);
        p8.add(total);
        total.setEditable(false);

        mp.add(p1);
        mp.add(p2);
        mp.add(p3);
        mp.add(p4);
        mp.add(p5);
        mp.add(p6);
        mp.add(p7);
        mp.add(p8);
    }

    public class MyAction implements ActionListener
    {
        int counter=0;
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == riceBtn)
            {
                counter++;
                tfPrice1.setText("RM" + String.valueOf(counter*rice));
            }

            if (e.getSource() == noodleBtn)
            {
                counter++;
                tfPrice2.setText("RM" + String.valueOf(counter*noodle));
            }

            if (e.getSource() == soupBtn)
            {
                counter++;
                tfPrice3.setText("RM" + String.valueOf(counter*soup));
            }
        }
    }

    public class MyAction2 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            price1.setText(String.valueOf(tfPrice1));
            price2.setText(String.valueOf(tfPrice2));           
            price3.setText(String.valueOf(tfPrice3));

            Total = price1+price2+price3;
            total.setText(String.valueOf(Total));
        }
    }

    public class MyAction3 implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == rstBtn)
            {
                tfPrice1.setText("");
                tfPrice2.setText("");
                tfPrice3.setText("");
                total.setText("");
            }
        }
    }

    public static void main(String [] args)
    {
        FoodOrder1 f = new FoodOrder1();
        f.setVisible(true);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(1000,800);
    }      
}

不需要变量来保存值。只要在需要值时从文本框中提取文本即可

double totalPrice = 0.0;

totalPrice += Double.parseDouble(tfPrice1.getText());
totalPrice += Double.parseDouble(tfPrice2.getText());
totalPrice += Double.parseDouble(tfPrice3.getText());

total.setText(String.valueOf(totalPrice));

下面是将textfield值保存到某个字符串的代码

String data = txtdata.getText();

将输入到txtdatajTextField的数据转换为字符串数据类型的data,其中有大量代码,您可能会希望将其精简为一个可以证明您正试图解决的问题。嗨。但我该如何提取呢?我对java还是新手…顺便说一句,谢谢你的帮助。声明int变量以在JTextField中保存值如何?textfield始终存储为字符串类型根据输入的数据类型转换变量,假设你正在使用int typecasted_data=integer.parseInt(数据)输入整数修改代码;你不应该在评论中使用“谢谢”之类的字眼,你可以通过投票或接受答案来表达你的感激之情