0

我有一个任务,我必须设置获取用户输入,然后显示输入以及与输入关联的图像。我被我的代码卡住了,不知道问题出在哪里。我认为我遇到的主要问题是获取 tf1.getText() == st1.getInfo() 值。

我还检查以确保图像位于正确的文件夹中并且标签正确。

很抱歉不得不发布这么多代码。谢谢你的帮助。

代码:

我的面板1

public class MyJPanel1 extends JPanel implements ChangeListener, ActionListener
{
    JTextField tf1,tf2;
    JSlider js1;
    JLabel lab6;
    JTextField message;
    JButton ok;
    myJPanel2  p2;
    int count2;

   public MyJPanel1(MyJPanel2 informedp2)
    {
    p2 = informedp2;
    setLayout(new GridLayout(7,1));
    lab6 = new JLabel("Enter Student Name");
    add(lab6,"North");

    tf1 = new JTextField(10);

    add(tf1);

    tf2 = new JTextField(15);

    add(tf2);


     message = new JTextField("",50);

js1 = new JSlider(JSlider.HORIZONTAL,0,50,20);
js1.setBorder(BorderFactory.createTitledBorder("Use Slider to Enter Age"));
    js1.setMajorTickSpacing(1);
    js1.setPaintTicks(true);
    add(js1);
add(message);

    js1.addChangeListener(this); 

     ok = new JButton("Ok");
    ok.addActionListener(this);
    add(ok);



    }
    public void stateChanged(ChangeEvent e) 
{
    JSlider obj = (JSlider)e.getSource();
    int count = obj.getValue();
    if(obj == js1)
    {
        message.setText("Age =" + count);
    }
        count2 = count;
    }
     public void actionPerformed(ActionEvent event) 
    {
     Object obj = event.getSource();
     String result = p2.st1.getInfo(); // value stored should be "Fred" 

     if (obj == ok)
     {
            p2.j2.setText("Student Name = " + tf1.getText()+" " + tf2.getText()+ " "+ "Age = " + count2);


     if (result ==tf1.getText()) // tf1.getText() value should be "Fred" which should then display the image. 
     {

         p2.j3.setIcon(p2.imageFred);
     }
     }
    }

}

我的JPanel2

public class myJPanel2 extends JPanel
{

student st1 = new student("Fred"); // "Fred" value should be the st1.getInfo() value 
    JButton j2 = new JButton("the user clicks on the button in the UPPER panel" );
    JButton j3 = new JButton("Pic");
     ImageIcon imageFred = new ImageIcon("images/fred.gif");
public myJPanel2()
{
    super();

    setBackground(Color.pink);
    setLayout(new GridLayout(3,1));
            add(j2);
            add(j3);
    }
}

学生:

 public class student 
{
    String firstName;
    String lastName;
    int age;

    public student(String a)// String b, int x)
    {   
        super();
        firstName = a;
        //lastName = b;
        //age = x;

    }

    String getInfo()
    {
        return firstName; // This value should return "Fred" since st1 = new student("Fred")correct? 
    }



    String whatsUp()
    {
        double r = Math.random();
        int myNumber = (int) (r * 3f); //comment: a random number between 0 and 2
        String answer = "I don't know";
        if(myNumber == 0) answer = "searching the web";
        if(myNumber == 1) answer = "doing Java";
        if(myNumber == 2) answer = "Listening to endless lecture";
        return answer;
    }

}

4

2 回答 2

1

您不能使用==. 这不会比较两个字符串的内容,而是比较它们的“地址”(即,您正在比较它们是否是同一个对象)。您可以在此处找到更详细的说明。

要比较它们,请使用以下String.equals方法:

  if ( tf1.getText().equals(st1.getInfo()) ) {
   // (...) 
  }
于 2013-10-12T21:01:07.610 回答
0

这是错误的:

if (result == tf1.getText()) {
  p2.j3.setIcon(p2.imageFred);
}

不要使用==. 请改用equals(...)orequalsIgnoreCase(...)方法。了解 == 检查两个对象是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这在这里很重要。所以而不是

if (fu == "bar") {
  // do something
}

做,

if ("bar".equals(fu)) {
  // do something
}

或者,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}
于 2013-10-12T21:00:58.860 回答