Java用户界面,一个监听器,12个显示图像和描述的对象
我在stackoverflow的第一篇帖子,已经关注了一段时间了! 我被这件事缠住了,今晚就到期了 我不知道如何使用action listener在fish对象中循环。。。我如何让imageicon和descption标签更改为下一条鱼,具体取决于季节 上面说没有阵列 我真的被难住了伙计们 actionlistener只有那些if语句,因为我正在尝试让它工作的方法 感谢您的帮助和建议 -------(问题2提纲)------- 你决定带家人去度假,去深海钓鱼。你已经计划好了在阳光下逃跑。您与其中一家捕鱼公司达成协议,帮助他们展示他们当前捕获的鱼。您将创建一个简单的图形程序,该程序将显示您的所有照片以及随附的标题 您需要创建一个简单的GUI应用程序,根据一年中的时间创建3个不同的fish photos对象。 您应该允许用户(通过文本框)输入一年中的时间。用户应该能够输入秋季、冬季、春季或夏季。 您应该创建并实例化一个Fish类。 你应该显示鱼的图片和标题/描述。 你应该有两个按钮在照片之间前进和后退。 每次按下“前进”按钮时,都会出现一张新照片,包括图片和标题。 每次按下“后退”按钮时,应显示上一张图片和标题。 不能使用数组。 两个按钮都必须有一个按钮侦听器Java用户界面,一个监听器,12个显示图像和描述的对象,java,user-interface,object,jframe,jlabel,Java,User Interface,Object,Jframe,Jlabel,我在stackoverflow的第一篇帖子,已经关注了一段时间了! 我被这件事缠住了,今晚就到期了 我不知道如何使用action listener在fish对象中循环。。。我如何让imageicon和descption标签更改为下一条鱼,具体取决于季节 上面说没有阵列 我真的被难住了伙计们 actionlistener只有那些if语句,因为我正在尝试让它工作的方法 感谢您的帮助和建议 -------(问题2提纲)------- 你决定带家人去度假,去深海钓鱼。你已经计划好了在阳光下逃跑。您与其中
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;
}
}
public类主面板扩展了JPanel
{
JLabel myTitleLabel=新JLabel();
JButton nextButton=新JButton();
JButton backButton=新JButton();
JLabel buttonLabel=新的JLabel();
ImageIcon image=新的ImageIcon(“,”图像将显示在此”);
JLabel myPictureLabel=新的JLabel(图像);
JLabel fishDescription=新的JLabel();
JTextField=new JTextField();
//图像图标,用于保存每条鱼的图像
ImageIcon image1=新的ImageIcon(“src/Images/fish1.jpg”);
ImageIcon image2=新的ImageIcon(“src/Images/fish2.jpg”);
ImageIcon image3=新的ImageIcon(“src/Images/fish3.jpg”);
ImageIcon image4=新的ImageIcon(“src/Images/fish4.jpg”);
ImageIcon image5=新的ImageIcon(“src/Images/fish5.jpg”);
ImageIcon image6=新的ImageIcon(“src/Images/fish6.jpg”);
ImageIcon image7=新的ImageIcon(“src/Images/fish7.jpg”);
ImageIcon image8=新的ImageIcon(“src/Images/fish8.jpg”);
ImageIcon image9=新的ImageIcon(“src/Images/fish9.jpg”);
ImageIcon image10=新的ImageIcon(“src/Images/fish10.jpg”);
ImageIcon image11=新的ImageIcon(“src/Images/fish11.jpg”);
ImageIcon image12=新的ImageIcon(“src/Images/fish12.jpg”);
//10个带有图像、描述和季节的鱼对象
Fish fish1=新鱼(图1,“这是一条鳟鱼”,“秋天”);
Fish fish2=新鱼(图2,“这是一条大嘴鲈鱼”,“秋天”);
Fish fish3=新鱼(图3,“这是一条蓬巴托鱼”,“秋天”);
Fish fish4=新鱼(图4,“这是淡水鼓形鱼”,“冬季”);
Fish fish5=新鱼(图5,“这是一条黄色的鲈鱼”,“冬天”);
Fish fish6=新鱼(图6,“这是爱荷华州的镖镖头鱼”,“冬天”);
Fish fish7=新鱼(图7,“这是一条牛鳟鱼”,“春天”);
Fish fish8=新鱼(图8,“这是一条奇努克鲑鱼”,“春天”);
Fish fish9=新鱼(图9,“这是一条海峡鲶鱼”,“春天”);
Fish fish10=新鱼(图10,“这是一只黄色的牛头鱼”,“夏天”);
Fish fish11=新鱼(图11,“这是一条北方梭子鱼”,“夏天”);
Fish fish12=新鱼(图12,“这是一只老虎麝香枪”,“夏天”);
公共主面板()
{
Font Font=新字体(“Verdana”,Font.BOLD,20);
设置首选尺寸(新尺寸(500800));
myTitleLabel.setText(“按季节捕鱼
”
+“进入一个季节,看看捕获的鱼”);
myTitleLabel.setFont(字体);
nextButton=新的JButton(“--------------下一步!---------------”;
backButton=新的JButton(“--------------BACK!-----------”);
setFont(字体);
seasureText.setText(“---输入一个季节---”;
addActionListener(newbuttonListener());
addActionListener(newbuttonListener());
myPictureLabel.setIcon(图像);
添加(myTitleLabel);
添加(下一个按钮);
添加(后退按钮);
添加(myPictureLabel);
添加(说明);
添加(文本);
}
公共选择器()
{
整数计数=0;
计数++;
}
私有类buttonListener实现ActionListener
{
@凌驾
已执行的公共无效操作(操作事件)
{
//这可能是不正确的…我正在尝试一些东西。。。
if(event.getSource()==“--------------下一步!---------------”)
myPictureLabel.setText(“转到下一个”);
fishDescription.setText(fish1.toString());
如果(event.getSource()=“--------------返回!---------------”)
myPictureLabel.setText(“返回”);
}
}
}
公鱼{
私有图像图标myImage;
私有字符串图像描述;
私人鱼线季节=“冬、夏、秋、春”;
公共鱼(ImageIcon myImage、String imageDescription、String fishSeason)
{
this.myImage=myImage;
this.imageDescription=imageDescription;
this.fish季候=fish季候;
}
公共字符串toString()
{
返回图像描述;
}
}
正如您所建议的-操作侦听器中的处理不正确:
getSource()返回一个组件(JButton),而不是文本。因此,您有两种选择:
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");
}
没有阵列?使用链接列表。