gpt4 book ai didi

Java按钮问题

转载 作者:行者123 更新时间:2023-12-02 08:30:37 25 4
gpt4 key购买 nike

我试图让用户输入 2 个字段。一是水池的容积,一是小屋浴缸的容积。然后计算每个的价格,我遇到的问题是,如果用户输入泳池的容量,那么他们就无法输入热水浴缸的任何内容,反之亦然。这是我到目前为止所拥有的。我是否需要为此设置两个单独的字段,或者如何完成?

几乎字符串错误=“”;一旦我弄清楚如何只允许他们输入一组数字,就可以将其删除。这是计算部分,代码的其他部分只是 3 个标签。

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”。如果您遵循按钮的想法并将按钮标记为“泳池”和“热水浴缸”,您甚至可以对结果做一些奇特的事情,例如 setText(e.getActionCommand()+" price:"+price); .

此外,if (errors != "")永远是true 。您正在测试您的 errors 是否对象与新的 String 是同一个对象您正在创建的对象;永远不会。您应该测试 if (!errors.equals(""))if (errors.length()!=0) .

关于Java按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3488859/

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