我正在制作某种计算程序,用户可以输入他们想要订购的饮料数量,按钮会计算他们需要支付的金额。如果他们检查了送货箱,将会增加30美元的运费。我使用isSelected检查它们是否打勾,如果勾选,但它不起作用。我得到的错误是"方法isSelected()未定义类型Checkbox"。
import java.awt.*;
import java.awt.event.*;
public class hwextend{
static Frame frm = new Frame("Action Event");
static Checkbox ckb1 = new Checkbox("Delivery");
static Button btn = new Button("Calculate");
static TextField textfield1 = new TextField();
static TextField textfield2 = new TextField();
static TextField textfield3 = new TextField();
static TextField textfield4 = new TextField();
static TextField textfield5 = new TextField();
public static void main(String[] args) {
// TODO Auto-generated method stub
GridLayout grid = new GridLayout (7,3);
btn.addActionListener(new ActListener());
frm.setLayout(grid);
frm.setSize(400,200);
frm.setBackground(Color.pink);
Label lab1 = new Label ("Tea Series");
Font font1 = new Font(null,Font.BOLD,12);
lab1.setFont(font1);
frm.add(lab1);
frm.add(new Label(""));
frm.add(new Label(""));
frm.add(new Label("Black Tea"));
frm.add(new Label("$70"));
frm.add(textfield1);
frm.add(new Label("Green Tea"));
frm.add(new Label("$70"));
frm.add(textfield2);
Label lab2 = new Label ("Special Flavor Series");
lab2.setFont(font1);
frm.add(lab2);
frm.add(new Label(""));
frm.add(new Label(""));
frm.add(new Label("Alpine Tea"));
frm.add(new Label("$80"));
frm.add(textfield3);
frm.add(new Label("Stewed Oolong Tea"));
frm.add(new Label("$80"));
frm.add(textfield4);
frm.add(btn);
frm.add(ckb1);
frm.add(textfield5);
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent Event) {
System.exit (0);
}
}
);
frm.setVisible(true);
}
static class ActListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(textfield1.getText().toString());
int b = Integer.parseInt(textfield2.getText().toString());
int c = Integer.parseInt(textfield3.getText().toString());
int d = Integer.parseInt(textfield4.getText().toString());
if (ckb1).isSelected()){
textfield5.setText(String.valueOf((a+b)*70+(c+b)*80)+30);
}
else {
textfield5.setText(String.valueOf((a+b)*70+(c+b)*80));
}
}
}
}
答案 0 :(得分:0)
编译器没有谎言。 In AWT there is no isSelected() method for CheckBox
您正在寻找ckb1.getState()
答案 1 :(得分:0)
请使用ckb1.getState()而不是ckb1.isselected()
请查看更新的代码正常
static Frame frm = new Frame("Action Event");
static Checkbox ckb1 = new Checkbox("Delivery");
static Button btn = new Button("Calculate");
static TextField textfield1 = new TextField();
static TextField textfield2 = new TextField();
static TextField textfield3 = new TextField();
static TextField textfield4 = new TextField();
static TextField textfield5 = new TextField();
public static void main(String[] args) {
// TODO Auto-generated method stub
GridLayout grid = new GridLayout (7,3);
btn.addActionListener(new ActListener());
frm.setLayout(grid);
frm.setSize(400,200);
frm.setBackground(Color.pink);
Label lab1 = new Label ("Tea Series");
Font font1 = new Font(null,Font.BOLD,12);
lab1.setFont(font1);
frm.add(lab1);
frm.add(new Label(""));
frm.add(new Label(""));
frm.add(new Label("Black Tea"));
frm.add(new Label("$70"));
frm.add(textfield1);
frm.add(new Label("Green Tea"));
frm.add(new Label("$70"));
frm.add(textfield2);
Label lab2 = new Label ("Special Flavor Series");
lab2.setFont(font1);
frm.add(lab2);
frm.add(new Label(""));
frm.add(new Label(""));
frm.add(new Label("Alpine Tea"));
frm.add(new Label("$80"));
frm.add(textfield3);
frm.add(new Label("Stewed Oolong Tea"));
frm.add(new Label("$80"));
frm.add(textfield4);
frm.add(btn);
frm.add(ckb1);
frm.add(textfield5);
frm.addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent Event) {
System.exit (0);
}
}
);
frm.setVisible(true);
}
static class ActListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
int a = Integer.parseInt(textfield1.getText().toString());
int b = Integer.parseInt(textfield2.getText().toString());
int c = Integer.parseInt(textfield3.getText().toString());
int d = Integer.parseInt(textfield4.getText().toString());
if (ckb1.getState()){
int res=((a+b)*70+(c+b)*80)+30;
textfield5.setText(String.valueOf(res));
}
else {
int res=((a+b)*70+(c+b)*80);
textfield5.setText(String.valueOf(res));
}
}
}
答案 2 :(得分:0)
https://docs.oracle.com/javase/7/docs/api/java/awt/Checkbox.html
查看API,似乎Checkbox对象没有isSelected方法。也许您正在寻找的是getState()。它确定复选框是处于开启还是关闭状态。
答案 3 :(得分:0)
尝试这样做:
ckb1.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if(e.getState()) {//checkbox has been selected
//do something...
} else {//checkbox has been deselected
//do something...
};
}
});
首先需要添加项侦听器,然后获取状态
getState()将返回一个布尔值,您可以使用if-else来完成工作:)
答案 4 :(得分:0)
Swing
和AWT
复选框不相同,而JCheckBox
实际上有isSelected()
方法,AWT
对应方没有。
无论如何,我建议使用事件进行状态更改跟踪,而不是直接访问UI
组件,例如:
checkbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.printf("%s\n", e.getStateChange() == 1 ? "checked" : "unchecked");
}
});