if语句,标签和组合框

时间:2016-04-26 21:40:43

标签: java if-statement jcombobox

好吧我的代码必须选择路由组合框(检查)显示标签(检查)有一个返回和单票组合框(检查)需要它显示文本(检查)我的问题是它只打印相关的文本之一我的陈述希望有人可以告诉我如何修复我的if语句。一个按钮上的标签更改。它通过标签读取代码。到目前为止它只打印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(); 
    }
}

1 个答案:

答案 0 :(得分: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 s中。

附加说明:您似乎想要"Tipperary to cork""single"。在这种情况下,请使用&&代替==。或者,你可以这样做(psuedocode故意)

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

但实际上,您应该比较c.getSelectedItem().toString()而不是标签的文字,但这是您的决定。