设置一些已处置或不可见的java

发布于 2024-08-31 03:00:21 字数 2698 浏览 5 评论 0原文

自项目开始运行以来更改了该项目。有点。 图像仍然没有改变。

package icnon;

import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FrameIconExample extends JFrame implements ActionListener {

    JLabel j;
    JPanel p, l, k;
    JButton picOne, picTwo;
    Container cPane;

    public FrameIconExample() {
        JButton picOne = new JButton("picOne");
        JButton picTwo = new JButton("picTwo");
        picOne.setName("picOne");
        picTwo.setName("picTwo");

        picOne.addActionListener(this);
        picTwo.addActionListener(this);

        p = new JPanel(new GridLayout(2, 1));
        l = new JPanel(new FlowLayout());
        k = new JPanel(new FlowLayout());

        cPane = getContentPane();

        j = new JLabel(new ImageIcon(
            "../meet/src/images/beautiful-closeup-portrait-photography.jpg"));

        l.add(j);
        k.add(picOne);
        k.add(picTwo);
        p.add(l);
        p.add(k);

        add(p);
    }

    public static void main(String[] args) {
        FrameIconExample frame = new FrameIconExample();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(new Dimension(300, 800));
        frame.setTitle("Frame Icon Example");

        // Display the form
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton temp = (JButton) e.getSource();
        String src = "../meet/src/images/Majken Kruse portrait - john.jpg";

        //System.out.println(src + " " + temp.getName());

        if (temp.getName().equalsIgnoreCase("picOne")) {
            try {

                l.hide();

                try {

                    src = "../meet/src/images/beautiful-closeup-portrait-photography.jpg";
                    System.out.println(src + " " + temp.getName());
                    Icon img;
                    j = new JLabel(new ImageIcon(src));

                    l.add(j);

                    System.out.println("1");
                } catch (Exception q) {
                    q.printStackTrace();
                }

                if (temp.getName().equalsIgnoreCase("picTwo")) {
                    src = "../icontest/images/Majken Kruse portrait - john.jpg";
                    System.out.println(src + " " + temp.getName());
                    Icon img;
                    j = new JLabel(new ImageIcon(src));
                    l.add(j);
                    System.out.println("2");
                }
            } catch (Exception x) {
                x.printStackTrace();
            }
        }
    }
}

请原谅不好的缩进。我很确定方法 l.add(j);是图像不改变的原因。

有什么想法应该是什么吗?

changed the project since its working now. kinda.
the image still isnt changing.

package icnon;

import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FrameIconExample extends JFrame implements ActionListener {

    JLabel j;
    JPanel p, l, k;
    JButton picOne, picTwo;
    Container cPane;

    public FrameIconExample() {
        JButton picOne = new JButton("picOne");
        JButton picTwo = new JButton("picTwo");
        picOne.setName("picOne");
        picTwo.setName("picTwo");

        picOne.addActionListener(this);
        picTwo.addActionListener(this);

        p = new JPanel(new GridLayout(2, 1));
        l = new JPanel(new FlowLayout());
        k = new JPanel(new FlowLayout());

        cPane = getContentPane();

        j = new JLabel(new ImageIcon(
            "../meet/src/images/beautiful-closeup-portrait-photography.jpg"));

        l.add(j);
        k.add(picOne);
        k.add(picTwo);
        p.add(l);
        p.add(k);

        add(p);
    }

    public static void main(String[] args) {
        FrameIconExample frame = new FrameIconExample();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(new Dimension(300, 800));
        frame.setTitle("Frame Icon Example");

        // Display the form
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton temp = (JButton) e.getSource();
        String src = "../meet/src/images/Majken Kruse portrait - john.jpg";

        //System.out.println(src + " " + temp.getName());

        if (temp.getName().equalsIgnoreCase("picOne")) {
            try {

                l.hide();

                try {

                    src = "../meet/src/images/beautiful-closeup-portrait-photography.jpg";
                    System.out.println(src + " " + temp.getName());
                    Icon img;
                    j = new JLabel(new ImageIcon(src));

                    l.add(j);

                    System.out.println("1");
                } catch (Exception q) {
                    q.printStackTrace();
                }

                if (temp.getName().equalsIgnoreCase("picTwo")) {
                    src = "../icontest/images/Majken Kruse portrait - john.jpg";
                    System.out.println(src + " " + temp.getName());
                    Icon img;
                    j = new JLabel(new ImageIcon(src));
                    l.add(j);
                    System.out.println("2");
                }
            } catch (Exception x) {
                x.printStackTrace();
            }
        }
    }
}

excuse the bad indentation. im pretty sure the method l.add(j); is the reason the image doesnt change.

any ideas what it should be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各自安好 2024-09-07 03:00:21

注意:这个答案是针对问题的修订版 1 和 2 做出的。

这不是 Awt 错误,而是 NullPointerException。

您的字段 l 为空,因为在您认为创建了它的那一刻,您实际上用局部变量掩盖了它。

JPanel p = new JPanel(new GridLayout(2, 1));
JPanel l = new JPanel(new FlowLayout());
JPanel k = new JPanel(new FlowLayout());

应该是:

p = new JPanel(new GridLayout(2, 1));
l = new JPanel(new FlowLayout());
k = new JPanel(new FlowLayout());

使用堆栈跟踪再次读取错误。在本例中,它会告诉您哪一行出了问题,并且错误的类型会告诉您发生了什么。

Note: this answer was made for the revisions 1 and 2 of the question.

It's not an Awt error, it's a NullPointerException.

Your field l is null, because on the moment you thought you created it, you actually masked it with a local variable.

JPanel p = new JPanel(new GridLayout(2, 1));
JPanel l = new JPanel(new FlowLayout());
JPanel k = new JPanel(new FlowLayout());

Should be:

p = new JPanel(new GridLayout(2, 1));
l = new JPanel(new FlowLayout());
k = new JPanel(new FlowLayout());

Read again the error, with the stack trace. It tells you which line is the problem, and the type of the error tells you what happened, in this case.

書生途 2024-09-07 03:00:21

您遇到的问题是您声明了一个名为 l 的全局 JPanel,但是当您在构造函数中实例化 JPanel 时,您声明并分配一个名为 l 的本地范围 JPanel。当您尝试在 actionPerformed 中添加组件时,您正在尝试将其添加到 null 全局变量中。

The problem you are having is that you declare a global JPanel named l, but then when you instantiate JPanels in your constructor, you declare and assign a local scope JPanel named l. When you try to add a component in your actionPerformed, you are attempting to add it to the null global var.

短暂陪伴 2024-09-07 03:00:21

我假设您没有设置正确的图像位置。您确定图像位于您指定的确切位置吗?你使用什么IDE?如果您使用 Eclipse,刷新项目可能会有所帮助。

I assume you did not set the correct image location. Are you sure the images are in the exact location you specified? What IDE are you using? If you use Eclipse, a refresh of your project might help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文