简体   繁体   English

if 语句、标签和组合框

[英]if statements,lables and combo boxes

Ok my code has to pick route combo box (check) display in label(check) have a return and single ticket combobox(check) need it to display text(check) my problem is it only prints text related to one of my statments hope someone can tell me how to fix my if statments.好的,我的代码必须选择路线组合框(选中)显示在标签(选中)中有一个返回和单票组合框(选中)需要它来显示文本(选中)我的问题是它只打印与我希望的一个报表相关的文本有人可以告诉我如何修复我的 if 语句。 The lable changes on a button .It reads code by lable.So far it only prints 15 and wont print 20 unless i had another label but this wouldnt make sense for the program按钮上的标签发生变化。它按标签读取代码。到目前为止,它只打印 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(); 
    }
}

You want to check "if some condition" when you click the button.当您单击按钮时,您想检查“是否有条件”。 So, start with one simple if statement inside one of the actionPerformed methods.因此,从actionPerformed方法之一中的一个简单 if 语句开始。 You shouldn't add an action listener inside an if statement, you should always perform an action, and determine the event inside that action.您不应该在 if 语句中添加动作侦听器,您应该始终执行一个动作,并确定该动作中的事件。

For example例如

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

Original answer原答案

These line just happen to work because you have if(false==false)这些行恰好可以工作,因为您有if(false==false)

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

The reason they evaluate to false is because you are comparing a JLabel.equals(String) .他们评估为false的原因是因为您正在比较JLabel.equals(String) You should use l.getText().equals("text here") , but...您应该使用l.getText().equals("text here") ,但是...

The problem is that you have those if statements inside the constructor for your class, meaning that they are the first thing that is evaluated in your code.问题是你的类的构造函数中有那些 if 语句,这意味着它们是你的代码中评估的第一件事。 You should move the corrected if statements into the ActionListener s for the respective buttons.您应该将更正的 if 语句移动到相应按钮的ActionListener

Additional note: You seem to want "Tipperary to cork" AND "single" .附加说明:您似乎想要"Tipperary to cork""single" In that case, use && in place of == .在这种情况下,使用&&代替== Alternatively, you could do this (psuedocode intentional)或者,您可以这样做(有意使用伪代码)

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

In reality, though, you should compare c.getSelectedItem().toString() instead of the text of the label, but that's your decision.但实际上,您应该比较c.getSelectedItem().toString()而不是标签的文本,但这是您的决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM