2012-08-08 79 views
0

如果可能请帮助我吗? 我正在做我自己的小电影项目,同时尝试学习Java。在单击按钮时在JLabel中显示图像时出现问题 - Java

我设法创建了一个JLabel,当单击一个按钮时它显示我的文本,它基本上是我最喜欢的电影。所以,当我点击Commando按钮时,会弹出一个简短的摘要。 所以我决定添加一个图像,它位于我的Java包所在的同一个文件夹中。

我研究了如何应用我的图像,以便它与文本同时显示,但它似乎不工作。 我还有很多东西要学,任何指导都会非常感谢!

在此先感谢您的帮助! ;]

Daz。

import java.awt.BorderLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextPane; 
import java.awt.event.*; 
import javax.swing.ImageIcon; 

public class Movies { 
    JFrame frame; 
    JLabel label; 

    public static void main(String []args){ 
      Movies gui = new Movies(); 
      gui.go(); 
    } 

    public void go(){ 
     frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JButton commandoButton = new JButton("Commando"); 
     commandoButton.addActionListener(new LabelListener()); 
     frame.setLayout(null); // equivalent to frame.getContentPane().setLayout(null); 
     frame.add(commandoButton); // equivalent to frame.getContentPane().add(button); 
     commandoButton.setBounds(10, 10, 150, 25); 

     JButton predatorButton = new JButton("Predator"); 
     predatorButton.addActionListener(new LabelListenerA()); 
     frame.setLayout(null); // equivalent to frame.getContentPane().setLayout(null); 
     frame.add(predatorButton); // equivalent to frame.getContentPane().add(button); 
     predatorButton.setBounds(10, 40, 150, 25); 

     JButton expendablesButton = new JButton("The Expendables"); 
     expendablesButton.addActionListener(new LabelListenerB()); 
     frame.setLayout(null); // equivalent to frame.getContentPane().setLayout(null); 
     frame.add(expendablesButton); // equivalent to frame.getContentPane().add(button); 
     expendablesButton.setBounds(10, 70, 150, 25); 

     frame.getContentPane().add(BorderLayout.WEST, commandoButton); 
     frame.setSize(1000, 350); 
     frame.setVisible(true); 
    } 

    JTextPane labelCommando = new JTextPane(); 
    class LabelListener implements ActionListener { 
     public void actionPerformed(ActionEvent commandoButton){    
      labelCommando.setText("A retired elite Black Ops Commando launches a one man war against a group of South American criminals who have kidnapped his daughter to blackmail him into starting a revolution and getting an exiled dictator back into power. "); 
      frame.add(labelCommando); 
      labelCommando.setBounds(170, 10, 500, 200); 
      labelCommando.setOpaque(false); 
      String imgStr = "images/commando.jpg"; 
      ImageIcon image = new ImageIcon("commando.jpg"); 
      JLabel labelCommando = new JLabel(" ", image, JLabel.CENTER); 
      label.setIcon(image); 
     } 
    } // Close inner class 

    class LabelListenerA implements ActionListener { 
     public void actionPerformed(ActionEvent predatorButton){    
      labelCommando.setText("Not long after they land, Dutch and his team discover that they have been sent under false pretenses. This deception turns out to be the least of their worries though, when they find themselves being methodically hunted by something not of this world..."); 
      frame.add(labelCommando); 
      labelCommando.setBounds(170, 10, 500, 200); 
      labelCommando.setOpaque(false); 
     } 
    } // Close inner class 

    class LabelListenerB implements ActionListener { 
     public void actionPerformed(ActionEvent expendablesButton){ 

      labelCommando.setText("Barney Ross leads the 'Expendables', a band of highly skilled mercenaries including knife enthusiast Lee Christmas, martial arts expert Yin Yang, heavy weapons specialist Hale Caesar, demolitionist Toll Road and loose-cannon sniper Gunner Jensen. When the group is commissioned by the mysterious Mr. Church to assassinate the merciless dictator of a small South American island, Barney and Lee head to the remote locale to scout out their opposition. Once there, they meet with local rebel Sandra and discover the true nature of the conflict engulfing the city. When they escape the island and Sandra stays behind, Ross must choose to either walk away and save his own life - or attempt a suicidle rescue mission that might just save his soul."); 
      frame.add(labelCommando); 
      labelCommando.setBounds(170, 10, 500, 200); 
      labelCommando.setOpaque(false); 
     } 
    } // Close inner class 
} 

回答

1

两个问题我已经看到了:

  1. 您使用label.setIcon(图像),但你没有设置一个值标记
  2. 的形象应该是新的ImageIcon (imgStr),而不是新的ImageIcon(“commando.jpg)
1

I”已经扫描了您的代码,并且找不到您正在创建(或添加)的任何地方,现在假设您在运行程序时没有收到任何错误,这表明代码片段不完整。

确保label已被创建并添加到屏幕的某个位置。

invalidaterepaint的调用可能也会有帮助,但是如果没有可用的代码示例很难确定。

我不知道为什么人们会坚持使用空布局管理器,但我建议先进行一次测试,然后与布局管理器一起工作,至少消除组件布局不正确的疑问。

+0

”确保标签已经创建并添加到屏幕的某个位置。“ 嗨,我该怎么做? :] – PrimalScientist 2012-08-08 21:36:44

+0

好吧,您已经使用其他组件完成了它,如mKorbel所示,创建一个新实例并将其添加到可见组件中,就像您的框架 – MadProgrammer 2012-08-08 21:51:35

1

..它位于我的Java包所在的文件夹中。

这个简单的声明表明了一些可能成为问题根源的误解。

  1. 该类文件不一定会位于与源文件相同的目录中。
  2. 类文件现在可能是一个Jar中的ZipEntry
  3. “文件夹”是一个非常基于屏幕的概念,更好地将磁盘上的目录视为“目录”(并将zip条目作为zip条目)。
  4. 不要以目录或zip条目来考虑软件包。它可能会变得相关,但又是一回事。

如何访问图像归结为一个重要问题。图像是应用程序资源吗?

如果应用程序。除了你之外,任何人都应该将它们作为一个“嵌入式应用程序资源”包含在Jar中,以便于分发。这些访问由URL

如果应用程序。仅适用于你,包含图像的目录的硬编码路径应该就足够了。通过将String参数传递给ImageIcon构造函数,我们表明我们需要基于该路径的File,但“相对路径”很少有效(除非开发人员需要特别努力确保图像在该路径相对于该路径可用。 .current目录)。相反,最好将包含图像的目录的绝对路径硬编码,然后在运行时添加图像文件名称。


也有一些在代码中其他错误,已大多被在其他的答案解决,但同样的。 使用布局管理组件大小和定位。

它似乎是JTableJList也可能比一堆按钮更好。 “

+0

只是想对此处的所有答复表示感谢。他们都是王牌! – PrimalScientist 2012-08-10 19:09:58

+1

@PrimalScientist:另请参阅[*布局管理器视觉指南*](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)。 – trashgod 2012-08-12 09:50:50

相关问题