繁体   English   中英

单击按钮时无法在JLabel中显示图像-Java

[英]Having trouble displaying an image in my JLabel when button is clicked - Java

如果可以的话,你能帮我吗? 在尝试学习Java的同时,我正在做自己的小“电影”项目。

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

我已经研究了如何应用我的图像,以便它与文本同时显示,但似乎不起作用。 我还有很多东西要学,任何指导都将不胜感激!!

在此先感谢您的帮助! ]

哈根达斯。

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. 您正在使用label.setIcon(image),但尚未设置标签的值
  2. 该图像应该是新的ImageIcon(imgStr)而不是新的ImageIcon(“ commando.jpg)
  • JLabel label; 没有初始化就必须使用JLabel label = new JLabel();

  • JLabel没有添加到容器中,因此在GUI中没有显示任何代码

  • 使用Standard LayoutManager代替setBounds()

  • JTextArea使用JScrollPane

我已经遍历了您的代码,并且找不到正在创建(或添加) label任何位置,现在假设您在运行程序时未收到任何错误,这表明该代码段不完整。

确保已创建label并将其添加到屏幕上的某些位置。

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

我不知道为什么人们坚持使用空布局管理器,但是我建议首先进行一个与布局管理器一起使用的测试,至少可以消除对组件未正确布局的怀疑。

..位于与我的Java软件包相同的文件夹中。

这个简单的陈述表明了许多可能是问题根源的误解。

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

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

如果是app。 对于除您之外的任何人,它们都应作为易于嵌入的“嵌入式应用程序资源”包含在Jar中。 可以通过URL访问。

如果是app。 仅适用于您,包含图像的目录的硬编码路径就足够了。 通过将String参数传递给ImageIcon构造函数,我们表示我们希望基于该路径的File ,但是“相对路径”很少起作用(除非开发人员特别努力确保图像相对于而言在该路径上可用)。 .current目录)。 相反,最好硬编码包含图像的目录的绝对路径,然后在运行时添加图像文件名。


代码中还有许多其他错误,大多数已在其他答案中得到了解决,但再次出现。 使用布局管理组件的大小和位置。

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

暂无
暂无

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

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