新代码:
import java.awt.*;
import java.util.*;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JPasswordField;
import javax.swing.JOptionPane;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
public class Gui extends JFrame {
static JTextField text;
static JTextField text2;
static JTextField text3;
static JCheckBox tsp;
static JCheckBox tbsp;
static JCheckBox cup;
static JCheckBox pint;
static JCheckBox quart;
static JCheckBox liter;
static JCheckBox gallon;
static JCheckBox oz;
static String unit;
static int conv;
public Gui(){
super("My title");
setLayout(new FlowLayout());
final JTextField text0 = new JTextField("Enter the unit you are using at the moment (Tsp, Tbsp, Cup, Pint, Quart, Liter, Gallon, Ounce)");
text0.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
text0.setText("");
}
});
add(text0);
unit = text0.getText(); ///////////////////////////UNIT\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
final JTextField text = new JTextField("Enter the amount you have ( Must be a number.)",20);
text.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
text.setText("");
}
});
add(text);
String str = text.getText();
try {
conv = Integer.parseInt(str); //////////////////////////////////AMOUNT TO CONVERT\\\\\\\\\\\\\\\\\\\\\\\\\
}
catch(NumberFormatException e){
}
text2=new JTextField("Which unit do you want to use:",20);
text2.setEditable(false);
add(text2);
//text3=new JTextField("You need "+conv+"",20);
// text3.setEditable(false);
// add(text3);
tsp = new JCheckBox("Tsp");
tbsp = new JCheckBox("Tbsp");
cup = new JCheckBox("Cup");
pint = new JCheckBox("Pint");
quart = new JCheckBox("Quart");
liter = new JCheckBox("Liter");
gallon = new JCheckBox("Gallon");
oz = new JCheckBox("Ounce");
add(tsp);
add(tbsp);
add(cup);
add(pint);
add(quart);
add(liter);
add(gallon);
add(oz);
HaandlerClass handler = new HaandlerClass();
tsp.addItemListener(handler);
tbsp.addItemListener(handler);
cup.addItemListener(handler);
pint.addItemListener(handler);
quart.addItemListener(handler);
liter.addItemListener(handler);
gallon.addItemListener(handler);
oz.addItemListener(handler);
HaandlerClaass actionListener = new HaandlerClaass();
tsp.addActionListener(actionListener);
tbsp.addActionListener(actionListener);
cup.addActionListener(actionListener);
pint.addActionListener(actionListener);
quart.addActionListener(actionListener);
liter.addActionListener(actionListener);
gallon.addActionListener(actionListener);
oz.addActionListener(actionListener);
}
private class HaandlerClaass implements ActionListener{
public JTextField text3;
public void actionPerformed(ActionEvent actionEvent) {
if(tsp.isSelected())
{text3.setVisible(true);}
if(tbsp.isSelected())
{text3.setVisible(true);}
if(cup.isSelected())
{text3.setVisible(true);}
if(pint.isSelected())
{text3.setVisible(true);}
if(quart.isSelected())
{text3.setVisible(true);}
if(liter.isSelected())
{text3.setVisible(true);}
if(gallon.isSelected())
{text3.setVisible(true);}
if(oz.isSelected())
{text3.setVisible(true);}
}
}
private class HaandlerClass implements ItemListener{
public JTextField text3;
public void itemStateChanged(ItemEvent event){
if((unit.compareTo("Tsp")==0) || unit.compareTo("tsp")==0){
if(tsp.isSelected())
conv*=1;
else if(tbsp.isSelected())
conv/=3;
else if(cup.isSelected())
conv*=.0208;
else if(pint.isSelected())
conv*=.0104;
else if(quart.isSelected())
conv*=.0052;
else if(liter.isSelected())
conv*=.0049;
else if(gallon.isSelected())
conv*=.0469;
else if(oz.isSelected())
conv/=6;
//prints converted unit
text3=new JTextField("You need "+conv+"",20);
text3.setEditable(false);
add(text3);
}
if((unit.compareTo("Tbsp")==0) || unit.compareTo("tbsp")==0){
if(tsp.isSelected())
conv*=3;
else if(tbsp.isSelected())
conv*=1;
else if(cup.isSelected())
conv*=0.625;
else if(pint.isSelected())
conv*=0.125;
else if(quart.isSelected())
conv*=0.0156;
else if(liter.isSelected())
conv*=0.147;
else if(gallon.isSelected())
conv*=0.0039;
else if(oz.isSelected())
conv*=0.5;
//prints converted unit
text3=new JTextField("You need "+conv+"",20);
text3.setEditable(false);
add(text3);
}
if((unit.compareTo("Cup")==0) || unit.compareTo("cup")==0){
if(tsp.isSelected())
conv*=48;
else if(tbsp.isSelected())
conv*=16;
else if(cup.isSelected())
conv*=1;
else if(pint.isSelected())
conv*=0.5;
else if(quart.isSelected())
conv*=0.25;
else if(liter.isSelected())
conv*=0.12;
else if(gallon.isSelected())
conv/=16;
else if(oz.isSelected())
conv*=8;
//prints converted unit
text3=new JTextField("You need "+conv+"",20);
text3.setEditable(false);
add(text3);
}
else if((unit.compareTo("Pint")==0) || unit.compareTo("pint")==0){
if(tsp.isSelected())
conv*=78;
else if(tbsp.isSelected())
conv*=32;
else if(cup.isSelected())
conv*=2;
else if(pint.isSelected())
conv*=1;
else if(quart.isSelected())
conv*=0.5;
else if(liter.isSelected())
conv*=0.47;
else if(gallon.isSelected())
conv/=8;
else if(oz.isSelected())
conv*=16;
//prints converted unit
text3=new JTextField("You need "+conv+"",20);
text3.setEditable(false);
add(text3);
}
else if((unit.compareTo("Quart")==0) || unit.compareTo("quart")==0){
if(tsp.isSelected())
conv*=192;
else if(tbsp.isSelected())
conv*=64;
else if(cup.isSelected())
conv*=4;
else if(pint.isSelected())
conv*=2;
else if(quart.isSelected())
conv*=1;
else if(liter.isSelected())
conv*=.94;
else if(gallon.isSelected())
conv*=0.25;
else if(oz.isSelected())
conv*=32;
//prints converted unit
text3=new JTextField("You need "+conv+"",20);
text3.setEditable(false);
add(text3);
}
else if((unit.compareTo("Liter")==0) || unit.compareTo("liter")==0){
if(tsp.isSelected())
conv*=204;
else if(tbsp.isSelected())
conv*=68;
else if(cup.isSelected())
conv*=4.29;
else if(pint.isSelected())
conv*=2.125;
else if(quart.isSelected())
conv*=18/17;
else if(liter.isSelected())
conv*=1;
else if(gallon.isSelected())
conv*=0.25;
else if(oz.isSelected())
conv*=34;
//prints converted unit
text3=new JTextField("You need "+conv+"",20);
text3.setEditable(false);
text3.setVisible(true);
add(text3);
}
else if((unit.compareTo("Gallon")==0) || unit.compareTo("gallon")==0){
if(tsp.isSelected())
conv*=788;
else if(tbsp.isSelected())
conv*=256;
else if(cup.isSelected())
conv*=16;
else if(pint.isSelected())
conv*=8;
else if(quart.isSelected())
conv*=4;
else if(liter.isSelected())
conv/=3;
else if(gallon.isSelected())
conv*=1;
else if(oz.isSelected())
conv*=128;
//prints converted unittext3.setVisible(true);
text3=new JTextField("You need "+conv+"",20);
text3.setEditable(false);
add(text3);
}
else if((unit.compareTo("Oz")==0) || unit.compareTo("oz")==0){
if(tsp.isSelected())
conv*=6;
else if(tbsp.isSelected())
conv*=2;
else if(cup.isSelected())
conv/=8;
else if(pint.isSelected())
conv/=16;
else if(quart.isSelected())
conv/=32;
else if(liter.isSelected())
conv/=34;
else if(gallon.isSelected())
conv/=128;
else if(oz.isSelected())
{ conv*=1;
text3=new JTextField("You need "+conv+"",20);
text3.setEditable(false);
text3.setVisible(true);
add(text3);
}
}
}
}
}
添加了使用text3.setVisible(true)的actionlistener text3.setVisible(true)也用于itemStateChanged 单击JCheckBox时仍然不显示JTextBox 运行main时,cmd中会弹出一个错误 以及我如何重新验证" JFrame的?
答案 0 :(得分:1)
如何在JTextField中输入变量时使GUI进行数学运算?
输入值时。
您需要在JTextField
使用DocumentListener
的文档上添加监听器。
你可以这样做
jtextField.getDocument().addDocumentListener(new DocumentListener() {
public void changedUpdate(DocumentEvent e) {
calc();
}
public void removeUpdate(DocumentEvent e) {
calc();
}
public void insertUpdate(DocumentEvent e) {
calc();
}
public void calc() {
//Do calculation
}
}
});
单击(选择)JCheckBox
时如何弹出JTextField
选中时,您可以jtextfield.setVisible(true)
选中actionperformed
复选框,取消选中时设置为false。