2017-03-27 208 views
0

我正在做一些計算程序,用戶可以在其中輸入他們想要訂購的飲料的數量,並且按鈕將計算他們需要支付的金額。如果他們檢查了交付箱,將會增加30美元的運費。我正在使用isSelected檢查它們是否勾選了是否勾選,但它不起作用。我得到的錯誤是「方法isSelected()未定義類型複選框」。isSelected()複選框錯誤

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

請使用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)); 
     } 
      } 


} 

你concatinating最終答案即送貨費 我已附上樣品Concatinating answer image

,我已經修改了代碼,並找到截圖爲修改後的代碼 Changed output image

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做你的工作:)

0

SwingAWT複選框不一樣,而JCheckBox確實有​​方法,AWT對應沒有。

無論如何,我會建議使用事件狀態變化的跟蹤,而不是直接訪問一個UI成分,例如:

checkbox.addItemListener(new ItemListener() { 
    public void itemStateChanged(ItemEvent e) { 
     System.out.printf("%s\n", e.getStateChange() == 1 ? "checked" : "unchecked"); 
    } 
});