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

标签 java swing jframe visible

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

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);这就是图像没有改变的原因。

有什么想法吗?

最佳答案

注意:此答案是针对问题的修订版 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());

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

关于java - 设置一些已处置或不可见的java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2765127/

相关文章:

java - immutableList 但仍然可以修改列表中的元素

java - context.xml 的 glassfish 替代品是什么

java - JAXB RI ClassFactory 中的空指针异常

java - Swing:如何使JFrame滚动流畅?

java - 如何在不退出应用程序的情况下根据 JPanel 上的事件关闭特定的 JFrame?

java - 从数据库创建 Hibernate 映射文件和 PJOS 时出错

Java swing 单选按钮,带有不断变化的、可点击的图标

java - 如何将 JTable 中第一行的颜色设置为任何颜色并保留表中之前设置的颜色

java - 在 Java JFrame 上添加图像 - Netbeans

java - removeAll() 卡住游戏