gpt4 book ai didi

java - 如何在java中添加更新的JTextfield值? [文档监听器]

转载 作者:行者123 更新时间:2023-12-01 13:19:33 25 4
gpt4 key购买 nike

我已经阅读了大量有关如何使用 DocumentListener 的帖子,但我的代码无法工作。基本上我已经制作了一个食物计算器,因此当在第一个 JTextField 中插入一些食物时,它会计算每 x 克所需产品的所有食物值。

底部有一个 JTextfield,我想用它来存储您消耗的所有产品的 kcal 值的总和。 简而言之,每当您更改产品并按 Enter 键时,它都会更改 kcal 值,因此我需要按下 Enter 按钮的所有产品的总和。

这是其工作原理的简单图像:

enter image description here

如果您知道,请告诉我,因为我为此工作了太多时间。 简单地说,我需要为我按 Enter 键的每个产品添加 JTextfield。

package Grafika;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.EventObject;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class Food_Calculator {

public static final String[] FOOD_NAME = { "Jogurt, sadni", "Jogurt 1,6%",
"Smetana, sladka 30%", "Sir,sveži", "Sir, mozzarella",
"Sir, ovčji", "Sir, parmezan", "Sirotka", "Brokoli", "Buče",
"Čebula, bela", "Čebula, rdeča", "Česen", "Endivija",
"Fižol, sveži", "Gobe, sveže", "Grah", "Jajčevec", "Krompir",
"Kumare, sveže", "Kumarice, kisle", "Motovilec", "Paprika, rumena",
"Paprika, zelena", "Paradižnik", "Pesa, kuhana", "Peteršilj",
"Por", "Rabarbara", "Radič, rdeči", "Radič, zeleni",
"Solata, zelena", "Špinača", "Zelena", "Zelje, glavnato belo",
"Zelje, glavnato rdeče", "Zelje, kitajsko", "Ananas", "Avokado",
"Banana", "Borovnice", "Breskev", "Brusnice", "Češnje", "Fige",
"Grenivka", "Grozdje, belo", "Grozdje, črno", "Hruška (cela)",
"Jabolko (celo)", "Jagode", "Kivi", "Kokos", "Lešniki, suhi",
"Limona", "Lubenica", "Mandarina", "Mandlji", "Maline", "Marelica",
"Nektarina", "Olive, zrele", "Orehi, suhi", "Pomaranča",
"Ribez, črni", "Ribez, rdeči", "Robide", "Rozine", "Slive",
"Šipek, suhi", "Višnje", "Goska, cela", "Kokoš, cela",
"Konjsko meso", "Piščanec, cel", "Piščanec, belo meso",
"Piščanec, temno meso", "Pršut", "Puran, cel", "Puran, belo meso",
"Puran, temno meso", "Raca, cela", "Svinjina, nemastna",
"Teletina, nemastna", "Vratovina, sušena", "Hobotnica", "Lignji",
"List", "Losos", "Postrv, potočna", "Sardelice", "Tuna",
"Tunina v olju", "Ajda", "Koruzni kosmiči", "Kruh, pšenični, črni",
"Ovseni kosmiči", "Prepečenec, beli", "Pšenična moka, bela",
"Pšenični otrobi", "Pšenica, suho zrnje", "Riž, neglaziran",
"Riž, glaziran", "Riževa moka", "Rž", "Borovničev sok",
"Breskov sok", "Cola", "Čaj, indijski", "Grozdni sok",
"Hruškov sok", "Jabolčni sok", "Kava", "Marelični sok",
"Pesin sok", "Pomarančni sok, sveži", "Čokolada, jedilna",
"Čokolada, mlečna", "Čokolada, mlečna, z lešniki", "Med" };

public static final double[] FOOD_VALUES = { 99.0, 43.0, 284.9, 95.0,
224.0, 447.0, 411.0, 25.9, 25.0, 15.0, 35.0, 23.0, 136.0, 11.0,
143.0, 35.9, 67.0, 25.0, 87.0, 14.0, 10.0, 20.0, 22.0, 16.0, 14.0,
32.0, 20.0, 28.0, 12.0, 12.0, 24.0, 15.1, 21.9, 12.0, 22.0, 20.0,
12.0, 46.0, 126.0, 79.0, 56.0, 37.0, 45.7, 48.0, 41.0, 28.0, 60.0,
54.0, 29.0, 40.0, 26.0, 40.0, 346.0, 701.0, 26.9, 21.0, 34.0,
565.0, 25.0, 50.7, 50.0, 128.5, 648.0, 35.0, 28.0, 21.0, 29.0,
246.0, 56.6, 169.2, 57.8, 198.0, 186.0, 110.0, 230.0, 116.0, 126.0,
496.0, 143.0, 134.0, 186.0, 341.0, 161.0, 113.0, 450.0, 73.0, 77.0,
83.0, 217.0, 86.0, 98.0, 225.0, 287.0, 333.7, 368.0, 222.0, 372.0,
354.0, 350.0, 206.0, 326.0, 329.0, 349.0, 349.0, 347.5, 45.0, 48.0,
39.0, 57.8, 65.7, 51.9, 49.7, 301.0, 56.6, 36.0, 45.0, 475.0,
517.9, 529.8, 302.7 };

public static final double[] FOOD_VALUES_OH = { 18.6, 3.9, 1.0, 3.0, 0.0,
0.0, 2.0, 5.1, 2.5, 3.4, 8.5, 5.2, 28.0, 1.1, 21.8, 5.8, 10.6, 5.6,
20.8, 1.8, 2.0, 3.0, 5.0, 2.8, 2.8, 7.0, 1.0, 5.0, 2.0, 1.7, 3.2,
2.8, 3.9, 2.0, 3.8, 3.5, 2.0, 11.6, 7.0, 19.2, 14.3, 9.1, 10.8,
11.7, 9.5, 7.0, 15.3, 13.5, 7.6, 10.0, 6.2, 9.0, 3.7, 6.0, 8.2,
5.3, 8.0, 4.3, 5.6, 12.8, 12.4, 2.6, 8.8, 5.8, 6.6, 4.4, 6.4, 64.4,
14.6, 79.2, 14.3, 0.0, 0.2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 1.1, 0.8, 1.0, 0.9, 0.0, 0.0, 0.0, 0.0, 0.0, 72.9,
85.1, 43.0, 72.8, 77.5, 80.1, 26.8, 72.0, 72.0, 78.0, 78.0, 74.0,
11.1, 11.7, 10.0, 0.0, 16.6, 3.2, 13.8, 28.5, 14.6, 8.0, 10.4,
46.8, 56.9, 51.3, 82.3 };

public static final double[] FOOD_VALUES_P = { 1.0, 3.3, 22.0, 14.0, 19.9,
25.6, 40.0, 0.9, 3.0, 0.6, 0.9, 0.9, 6.0, 0.9, 10.1, 2.8, 5.8, 1.2,
2.1, 0.7, 0.5, 2.0, 0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 1.0, 1.8, 0.0, 0.0,
0.0, 0.0, 0.0, 0.2, 0.0, 15.4, 0.1, 0.0, 0.2, 39.7, 32.3, 35.6, 0.0 };

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JPanel plosca = new MainPanel();
GraficnoOgrodje.ustvariOkno(
"Kalkulator Hranilnih Vrednosti v1.0", plosca, false);
}
});
}

private static class MainPanel extends JPanel implements ActionListener {

private JButton enter;
private JTextField calculations, food_field, value2, value3, value4,
value5, grams, total1;
private JLabel food, food_field_entered, kcal, value1, OH, P, FATT,
blank, blank1, blank2, portion, total;

public MainPanel() {
setLayout(new BorderLayout());

// First panel on Main panel
JPanel PanelOne = new JPanel();
this.add(PanelOne, BorderLayout.NORTH);
PanelOne.setBackground(Color.lightGray);
PanelOne.setLayout(new FlowLayout());

food = new JLabel();
food.setText("Insert food type: ");
portion = new JLabel(" insert a portion in grams (g): ");
grams = new JTextField(10);
food_field = new JTextField(10);
food_field.setEditable(true);
enter = new JButton("Enter");

PanelOne.add(food);
PanelOne.add(food_field);
PanelOne.add(portion);
PanelOne.add(grams);
PanelOne.add(enter);

// Second Panel on Main panel
JPanel PanelTwo = new JPanel();
this.add(PanelTwo, BorderLayout.CENTER);
Dimension two = new Dimension(500, 150);
PanelTwo.setPreferredSize(two);
PanelTwo.setLayout(new GridLayout(3, 8, 0, 10));

food_field_entered = new JLabel(" Food: ");
value1 = new JLabel();
blank = new JLabel();
blank1 = new JLabel();
kcal = new JLabel(" Kcal: ");
value2 = new JTextField(10);
OH = new JLabel(" OH: ");
value3 = new JTextField(10);
P = new JLabel(" Proteins: ");
value4 = new JTextField(10);
FATT = new JLabel(" Fatt: ");
value5 = new JTextField(10);

PanelTwo.add(food_field_entered);
PanelTwo.add(value1);
PanelTwo.add(blank);
PanelTwo.add(blank1);
PanelTwo.add(kcal);
PanelTwo.add(value2);
PanelTwo.add(OH);
PanelTwo.add(value3);
PanelTwo.add(P);
PanelTwo.add(value4);
PanelTwo.add(FATT);
PanelTwo.add(value5);

// Third Panel on Main panel
JPanel PanelThree = new JPanel();
this.add(PanelThree, BorderLayout.SOUTH);
PanelThree.setBackground(Color.lightGray);
PanelThree.setLayout(new FlowLayout());
Dimension three = new Dimension(getWidth(), 30);
PanelThree.setPreferredSize(three);

total = new JLabel("You consumed: ");
total1 = new JTextField(10);
total1.setEditable(true);

PanelThree.add(total);
PanelThree.add(total1);

// Fourth Panel on Main panel
JPanel PanelFour = new JPanel();
this.add(PanelFour, BorderLayout.EAST);
PanelFour.setPreferredSize(new Dimension(5, getHeight()
- PanelThree.getHeight()));

// Fifth Panel on Main panel
JPanel PanelFive = new JPanel();
this.add(PanelFive, BorderLayout.WEST);
PanelFive.setBackground(Color.lightGray);
PanelFive.setPreferredSize(new Dimension(0, getHeight()
- PanelThree.getHeight()));

// adding ActionListener to the MainPanel, for the button
enter.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();

if (source == enter) {

String foodText = food_field.getText();
String gramsText = grams.getText();
double grams2 = Double.parseDouble(grams.getText());

value1.setText("[ " + foodText + " / " + gramsText
+ " gramov ]");

// calculations of food values for x grams...
double kcal1 = 0.0;
double oh = 0.0;
double protein = 0.0;
double fatt = 0.0;

for (int i = 0; i < FOOD_NAME.length; i++) {

if (foodText.equals(FOOD_NAME[i])) {
kcal1 = grams2 / 100 * FOOD_VALUES[i];
oh = grams2 / 100 * FOOD_VALUES_OH[i];
protein = grams2 / 100 * FOOD_VALUES_P[i];
fatt = grams2 / 100 * FOOD_VALUES_M[i];
}
}
value2.setText(String.format(" %.1f ", kcal1));
value3.setText(String.format(" %.1f g", oh));
value4.setText(String.format(" %.1f g", protein));
value5.setText(String.format(" %.1f g", fatt));

}

value2.getDocument().addDocumentListener(new DocumentListener() {

@Override
public void changedUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
warn();
}

@Override
public void insertUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
warn();
}

@Override
public void removeUpdate(DocumentEvent e) {
// TODO Auto-generated method stub
warn();
}

public void warn() {
double total = 0.0;
double kcal1 = Double.parseDouble(value2.getText());
total = kcal1 + total;
total1.setText(String.format(" %.1f Kcal", total));
}
});
}
}

}

最佳答案

添加 ActionListener到您的 JButton 输入,如下所示:

Button enter = new JButton("Enter");
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
total1.setText(Double.parseDouble(value2.getText())+
Double.parseDouble(value3.getText())+
Double.parseDouble(value4.getText())+
Double.parseDouble(value5.getText()))
}
});

这只是使得每次按下“输入”按钮时,JTextField Total1 将显示 JTextField 的 value2-5 中的值的总和。首先必须将值解析为 double ,否则将使用字符串加法(连接而不是算术相加)。

关于java - 如何在java中添加更新的JTextfield值? [文档监听器],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22155599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com