2016-04-26 63 views
-2

好吧,我的代碼必須選擇路由組合框(檢查)顯示標籤(檢查)有一個返回和單票組合框(檢查)需要它來顯示文本(檢查)我的問題是隻打印與我的一個表述相關的文本,希望有人能告訴我如何修正我的if語句。上的按鈕。它由lable.So讀取代碼拉布勒變化到目前爲止,它只能打印15和習慣打印20,除非我有另外的標籤,但這難道不有意義的程序如果語句,標籤和組合框

package learning; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList.*; 
import java.util.Arrays.*; 
import java.util.List.*; 


@SuppressWarnings("unused") 
public class test { 

    String[] items = {"Tipperary_to_cork","Cork_to_Dublin","Limerick_to_Tipperary","Dublin_to_Cork"}; 
    JComboBox c = new JComboBox(items); 
    JButton b = new JButton("From"); 
    JLabel l = new JLabel(); 

    String[] items2 = {"window","aisle"}; 
    JComboBox m = new JComboBox(items2); 
    JButton n = new JButton("Seat"); 
    JLabel o = new JLabel(); 

    String[] items3 = {"Single","return"}; 
    JComboBox x = new JComboBox(items3); 
    JButton y= new JButton("Ticket"); 
    JLabel z = new JLabel("choose Ticket"); 

    String[] items4 = {"1","2","3","4","5","6","7","8","9","10"}; 
    JComboBox<?> xx = new JComboBox(items4); 
    JButton yy = new JButton("seat"); 
    JLabel zz = new JLabel("Choose a seat"); 
    JLabel hh = new JLabel("cost"); 
    JButton ccc = new JButton("comfirm"); 
    JLabel hhh = new JLabel("");{ 

    } 


    public test(){ 


    frame(); 

    } 
    public void frame(){ 

    JFrame wolf = new JFrame();//frame 
    wolf.setVisible(true); 
    wolf.setSize(350,350); 
    wolf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel p = new JPanel(); 


    p.add(hh); 
    p.add(c);// 
    p.add(b);// 
    p.add(l);//lable1 
    p.add(m);// 
    p.add(n);// 
    p.add(o);//lable 2 
    p.add(x);// 
    p.add(y);// 
    p.add(z);//lable 2 
    p.add(xx);// 
    p.add(yy);// 
    p.add(zz);//lable 2 
    p.add(ccc); 
    p.add(hhh); 
    wolf.add(p); 


    b.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
    String s = c.getSelectedItem().toString(); 
     l.setText(s); 
     } 
    }); 


    n.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
     String s = m.getSelectedItem().toString(); 
      o.setText(s); 
      } 
     }); 

    y.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
     String s = x.getSelectedItem().toString(); 
      z.setText(s); 
      } 
     }); 
    yy.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
     String s = xx.getSelectedItem().toString(); 
      zz.setText(s); 
      } 
     }); 
    } 
    { 

    if(l.getText().equals("Tipperary_to_cork")&&(z.getText().equals("single"))){ 
      ccc.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent e){ 
        hh.setText("15");       //*** 

}});   
      if(l.getText().equals("Tipperary_to_cork")&&(z.getText().equals("return"))){ 
      ccc.addActionListener(new ActionListener(){  
       public void actionPerformed(ActionEvent e){ 
        hh.setText("20");       //**** 

       } 
      });  
      }}} 
public static void main(String[]args){ 



    new test(); 
    } 
} 
+0

好的誦讀困難,所以當我輸入快速有時我鍵入錯誤的鍵抱歉,如果我的問題是壞的,但我發現它很難階段。對我的代碼 – JINX

+2

'l.equals(「Tipperary to cork」)''的任何建議將**總是**返回false。因爲你正在比較'JLabel l'到一個字符串(與'JLabel z'相同) –

+0

好吧,那麼我該怎麼做 – JINX

回答

0

你想,如果某些檢查」條件「,當你點擊按鈕。因此,從actionPerformed方法中的一個簡單if語句開始。您不應該在if語句中添加動作偵聽器,您應該始終執行動作並確定該動作中的事件。

例如

b.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
     String s = c.getSelectedItem().toString(); 
     if (s.equals("Tipperary to cork")) { 
      // TODO: do something 
     } 
    } 
}); 

原來的答覆

這些線恰好工作,因爲你有if(false==false)

if(l.equals("Tipperary to cork")==(z.equals("single"))) { ... } 
if(l.equals("Tipperary to cork")==(z.equals("return"))) { ... } 

他們評估爲false的原因是因爲你比較一個JLabel.equals(String)。你應該使用l.getText().equals("text here"),但...

問題是,你有那些if語句在你的類的構造函數中,這意味着它們是你的代碼中第一個被評估的東西。您應該將更正的if語句移動到各個按鈕的ActionListener

附加說明:您似乎想要"Tipperary to cork""single"。在這種情況下,請使用&&代替==。或者,你可以這樣做(僞碼故意)

if "Tipperary to cork" { 
    if "single" { ... } 
    else if "return" { ... } 
} 

在現實中,雖然,你應該比較c.getSelectedItem().toString()代替標籤的文本,但是這是你的決定。

+0

好的,我改變了我的if語句,但是可以重複「你應該將更正的if語句移動到各個按鈕的ActionListeners中」。 很多動作偵聽器的代碼中的if語句中的那些代碼類似於 wolf.add(p); b.addActionListener(新的ActionListener(){ 公共無效的actionPerformed(ActionEvent的發送){ 的String = c.getSelectedItem()的toString(); l.setText(一個或多個); } }); 我幫你解決了很多問題,我只需要你的幫助,把它們放在正確的位置,我自己就可以完成剩下的工作。 – JINX

+0

刪除按鈕並[處理組合框選擇中的事件](https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#listeners) –

+0

我已交換我的if語句在課堂外,並且按鈕的確認按鈕不再執行。 進入ActionListeners的各個按鈕「可以給我一個例子,因爲即時消息搞砸 – JINX