Java用户界面,一个监听器,12个对象,用于显示图像和描述

时间:2015-11-20 23:33:30

标签: java user-interface object jframe jlabel

首先在stackoverflow发帖给我,已经关注了一段时间了! 我今晚仍然坚持这个和它应得的。

我不确定如何通过动作监听器来循环穿过鱼类物体......我将如何根据季节将imageicon和descption标签更改为下一条鱼!

它说没有数组 我真的难倒在这里......

actionlistener只有那些if语句,因为我正在尝试实现这个方法。

感谢任何帮助或建议!

-------(问题2大纲)-------

你决定带着你的家人去度假,做深海捕鱼。您已经计划好在阳光下度假。您与其中一家渔业公司达成协议,帮助他们展示他们目前捕获的鱼类。您将创建一个简单的图形程序,它将显示您的所有照片和随附的标题。

您需要创建一个简单的GUI应用程序,根据一年中的时间创建3个不同的鱼照片对象。 您应该允许用户输入(通过文本框)一年中的时间。用户应该能够进入秋季,冬季,春季或夏季。 您应该创建一个Fish类并进行实例化。 您应该显示图片和鱼的标题/描述。 你应该有两个按钮在照片之间前进和后退。 每次按下前进按钮时,都会出现一张新照片,包括照片和标题。 每次按下后退按钮,都会出现上一张图片和标题。 你不能使用数组。 您必须为两个按钮都有一个按钮监听器。

public class MainPanel extends JPanel
{
    JLabel myTitleLabel = new JLabel ();

    JButton nextButton = new JButton();
    JButton backButton = new JButton();
    JLabel buttonLabel = new JLabel();
    ImageIcon image = new ImageIcon ("", "Image Will Display Here");
    JLabel myPictureLabel = new JLabel (image);
    JLabel fishDescription = new JLabel ();
    JTextField seasonText = new JTextField();

//imageicons that hold the images of each fish  
    ImageIcon image1 = new ImageIcon("src/Images/fish1.jpg");
    ImageIcon image2 = new ImageIcon("src/Images/fish2.jpg");
    ImageIcon image3 = new ImageIcon("src/Images/fish3.jpg");
    ImageIcon image4 = new ImageIcon("src/Images/fish4.jpg");
    ImageIcon image5 = new ImageIcon("src/Images/fish5.jpg");
    ImageIcon image6 = new ImageIcon("src/Images/fish6.jpg");
    ImageIcon image7 = new ImageIcon("src/Images/fish7.jpg");
    ImageIcon image8 = new ImageIcon("src/Images/fish8.jpg");
    ImageIcon image9 = new ImageIcon("src/Images/fish9.jpg");
    ImageIcon image10 = new ImageIcon("src/Images/fish10.jpg");
    ImageIcon image11 = new ImageIcon("src/Images/fish11.jpg");
    ImageIcon image12 = new ImageIcon("src/Images/fish12.jpg");


    //10 fish objects with an image, description, and a season
    Fish fish1 = new Fish (image1, "this is a trout ", "fall");
    Fish fish2 = new Fish (image2, "This is a big mouth bass ", "fall");
    Fish fish3 = new Fish (image3, "this is a pompato", "fall");
    Fish fish4 = new Fish (image4, "This is a freshwater drum fish", "winter");
    Fish fish5 = new Fish (image5, "this is a yellow perch ", "winter");
    Fish fish6 = new Fish (image6, "This is a Iowa Darter ", "winter");
    Fish fish7 = new Fish (image7, "this is a bull trout ", "spring");
    Fish fish8 = new Fish (image8, "This is a chinook salmon", "spring");
    Fish fish9 = new Fish (image9, "this is a Channel Catfish ", "spring");
    Fish fish10 = new Fish (image10, "This is a Yellow Bullhead", "summer");
    Fish fish11 = new Fish (image11, "this is a Northern Pike", "summer");
    Fish fish12 = new Fish (image12, "This is a Tiger Muskellunge", "summer");


public MainPanel ()
{

    Font font = new Font ("Verdana", Font.BOLD, 20);
    setPreferredSize(new Dimension (500,800));

    myTitleLabel.setText("<html>Fishing by Seasons<br> "
            + "Enter a season to see the fish caught then</html>");

    myTitleLabel.setFont(font);


    nextButton = new JButton ("-----------NEXT!-----------");
    backButton = new JButton ("-----------BACK!------------");

    seasonText.setFont(font);

    seasonText.setText("  ---  Enter a Season  ---  ");

    nextButton.addActionListener(new buttonListener());
    backButton.addActionListener(new buttonListener());

    myPictureLabel.setIcon(image);

    add(myTitleLabel);
    add(nextButton);
    add(backButton);
    add(myPictureLabel);
    add(fishDescription);
    add(seasonText);
}




public void fishSelector()
{
    int count = 0;
    count++;
}



private class buttonListener implements ActionListener
{

    @Override
    public void actionPerformed(ActionEvent event) 
    {
        //this is probably incorrect.. was trying stuff out...
        if (event.getSource()== "-----------NEXT!-----------")
            myPictureLabel.setText("going to next");
            fishDescription.setText(fish1.toString());


        if (event.getSource() == "-----------BACK!------------")
            myPictureLabel.setText ("going back");



    }

}

}



public class Fish {



private ImageIcon myImage;
private String imageDescription;
private String fishSeason = "winter, summer, fall, spring";



public Fish(ImageIcon myImage, String imageDescription, String fishSeason) 
{

    this.myImage = myImage;
    this.imageDescription = imageDescription;
    this.fishSeason = fishSeason;


}


public String toString ()
{
    return imageDescription;
}
}

1 个答案:

答案 0 :(得分:0)

正如您所建议的那样 - 动作侦听器中的处理不正确: event.getSource()返回一个组件(JButton),而不是文本。所以你有两个选择:

  1. 检查按下哪个按钮(更好)
  2. 按按钮名称检查
  3. 以这两种方式首先需要找到按下的按钮:

        private class buttonListener implements ActionListener {
    
        @Override
        public void actionPerformed(ActionEvent event) {
            // this is probably incorrect.. was trying stuff out...
            // if (event.getSource() == "-----------NEXT!-----------")
            // checking the button is better than checking it's text
            //
            if ((JButton)event.getSource() == nextButton)
                myPictureLabel.setText("going to next");
            fishDescription.setText(fish1.toString());
    
            if ((JButton)event.getSource() == backButton)
                myPictureLabel.setText("going back");
    
        }