Java按钮问题
我试图让用户输入两个字段。一个是游泳池的容积,一个是小屋浴缸的容积。然后计算每一个的价格,我遇到的问题是,如果用户为游泳池输入音量,那么他们就不能为热水浴缸输入任何内容,反之亦然。这就是我目前所拥有的。我是否需要为此设置两个单独的字段,或者如何设置 几乎所有的字符串错误都是“”;一旦我知道如何只允许他们输入一组数字,就可以删除。这是计算部分,代码的另一部分就是3个标签Java按钮问题,java,button,Java,Button,我试图让用户输入两个字段。一个是游泳池的容积,一个是小屋浴缸的容积。然后计算每一个的价格,我遇到的问题是,如果用户为游泳池输入音量,那么他们就不能为热水浴缸输入任何内容,反之亦然。这就是我目前所拥有的。我是否需要为此设置两个单独的字段,或者如何设置 几乎所有的字符串错误都是“”;一旦我知道如何只允许他们输入一组数字,就可以删除。这是计算部分,代码的另一部分就是3个标签 pricePanel = new JPanel(new FlowLayout()); final JRadio
pricePanel = new JPanel(new FlowLayout());
final JRadioButton poolPrice= new JRadioButton("Pool");
final JRadioButton tubPrice = new JRadioButton("Hot Tub");
poolPrice.setSelected(true);
ButtonGroup group = new ButtonGroup();
group.add(poolPrice);
group.add(tubPrice);
pricePanel.add(poolPrice);
pricePanel.add(tubPrice);
pricePanel.add(new JLabel("Enter the pool's volume: "));
final JTextField poolField = new JTextField(10);
pricePanel.add(poolField);
pricePanel.add(new JLabel("Enter the tub's volume: "));
final JTextField tubField = new JTextField(10);
pricePanel.add(tubField);
JButton calculatePrice = new JButton("Calculate Price");
calculatePrice.setMnemonic('C');
pricePanel.add(calculatePrice);
pricePanel.add(createExitButton());
pricePanel.add(new JLabel("The price is:$ "));
final JTextField priceField = new JTextField(10);
priceField.setEditable(false);
pricePanel.add(priceField);
calculatePrice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
double pool = Double.parseDouble (poolField.getText());
double tub = Double.parseDouble(tubField.getText());
double price;
if (poolPrice.isSelected()) {
price = pool * 100;
} else {
price = tub * 75;
}
priceField.setText(df.format(price));
}
});
};
ActionListener priceListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == poolPrice) {
tubLabel.setEnabled(false);
tubField.setEnabled(false);
messageArea.setVisible(true);
} else if (e.getSource() == tubPrice) {
poolLabel.setEnabled(false);
poolField.setEnabled(false);
messageArea.setVisible(true);
}
}
};
poolPrice.addActionListener(priceListener);
tubPrice.addActionListener(priceListener);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hotTubsPanel = new JPanel(new FlowLayout());
final JRadioButton roundTub = new JRadioButton("Round Tub");
final JRadioButton ovalTub = new JRadioButton("Oval Tub");
roundTub.setSelected(true);
ButtonGroup group = new ButtonGroup();
group.add(roundTub);
group.add(ovalTub);
hotTubsPanel.add(roundTub);
hotTubsPanel.add(ovalTub);
hotTubsPanel.add(new JLabel("Enter the tub's length: "));
final JTextField lengthField = new JTextField(10);
hotTubsPanel.add(lengthField);
final JLabel widthLabel = new JLabel("Enter the tub's width*: ");
widthLabel.setEnabled(false);
hotTubsPanel.add(widthLabel);
final JTextField widthField = new JTextField(10);
widthField.setEnabled(false);
hotTubsPanel.add(widthField);
hotTubsPanel.add(new JLabel("Enter the tub's depth: "));
final JTextField depthField = new JTextField(10);
hotTubsPanel.add(depthField);
JButton calculateVolume = new JButton("Calculate Volume");
calculateVolume.setMnemonic('C');
hotTubsPanel.add(calculateVolume);
hotTubsPanel.add(createExitButton());
hotTubsPanel.add(new JLabel("The tub's volume is: "));
final JTextField volumeField = new JTextField(10);
volumeField.setEditable(false);
hotTubsPanel.add(volumeField);
final JTextArea messageArea = createMessageArea(1, 25,
"*Width will be set to the same value as length");
hotTubsPanel.add(messageArea);
calculateVolume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (roundTub.isSelected()) {
widthField.setText(lengthField.getText());
}
ValidationResult result = validateFields(new JTextField[] {
lengthField, widthField, depthField });
String errors = "";
if (result.filled != 3) {
errors += "Please fill out all fields! ";
}
if (result.valid != 3 && result.filled != result.valid) {
errors += "Please enter valid numbers!";
}
if (errors != "") {
messageArea.setText(errors);
messageArea.setVisible(true);
} else {
messageArea.setVisible(false);
double length = Double.parseDouble(lengthField.getText());
double width = Double.parseDouble(widthField.getText());
double depth = Double.parseDouble(depthField.getText());
double volume;
if (roundTub.isSelected()) {
volume = Math.PI * Math.pow(length / 2.0, 2) * depth;
} else {
volume = Math.PI * Math.pow(length * width, 2) * depth;
}
volumeField.setText(df.format(volume));
}
}
});
ActionListener tubsListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == roundTub) {
widthLabel.setEnabled(false);
widthField.setEnabled(false);
widthField.setText(lengthField.getText());
messageArea.setText("Tub's width set to length");
messageArea.setVisible(true);
} else if (e.getSource() == ovalTub) {
widthLabel.setEnabled(true);
widthField.setEnabled(true);
messageArea.setVisible(false);
}
}
};
roundTub.addActionListener(tubsListener);
ovalTub.addActionListener(tubsListener);
}
两个文本字段和一个结果字段可能会让用户感到困惑。如果只有一个结果字段,那么也应该只有一个文本字段。斯蒂安建议的单选按钮会起作用,甚至有两个按钮,一个用于游泳池,一个用于热水浴缸(只需点击一次即可获得另一个价格)。在结果字段中还应该有一些内容,指示计算了哪一个,例如“泳池价格:$XX.XX”或“热水浴缸价格:$XX.XX”。如果您使用按钮的想法,并将按钮标记为“Pool”和“Hot-Tub”,您甚至可以对结果进行一些奇特的操作,例如
setText(例如getActionCommand()+“price:”+price)代码>
而且,if(errors!=“”)
总是true
。您正在测试您的错误
对象是否与您正在创建的新字符串
对象相同;永远不会。您应该改为测试if(!errors.equals(“”)
或if(errors.length()!=0)
也许您可以只有一个文本字段,然后有单选按钮,以便用户可以选择他们想要的价格(如果我正确理解您的问题,这有点让人困惑)。只有一个输入的原因是,他们可以选择热水浴缸价格或游泳池价格,并且两个字段只有一个输出框。我可以有一个游泳池价格区和一个热水浴缸价格区,但我认为只有一个会更有效。我可以用单选按钮让他们选择游泳池或热水浴缸,这是我没有想到的。好吧,所以我编辑了这个程序,使用单选按钮设置,但出于某种原因,我在结尾处得到了这个错误:poolPrice.addActionListener(priceListener);tubPrice.addActionListener(priceListener);令牌“priceListener”上的语法错误,此令牌后应为VariableDeclaratorId。。。但是这个设置是我用于另一个工作正常的方法的,所以我很困惑。当你声明一个参数的类型(在方法定义中)而不是它的名称时,通常会发生这个错误。我不认为这会发生,所以老实说我也有点困惑。你能多发一点你的代码吗,这样我就可以看到上下文了?这几乎就是整个代码,它是一个部分,一个标签,用于更好地理解一个程序。我可以发布程序的另一个选项卡,但它对这个选项卡应该没有任何重要性。是的,这是我得到的:线程“main”java.lang中的异常。错误:未解决的编译问题:在guiTut.guiTut.main(guiTut.java:41)我得到的所有错误都是ActionListener的红色下划线,我发布了main,在底部的代码下面有错误。