设置一些处置或隐形的java

OVE*_*ONE 2 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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

任何想法它应该是什么?

Gno*_*upi 6

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

它不是Awt错误,它是NullPointerException.

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

JPanel p = new JPanel(new GridLayout(2, 1));
JPanel l = new JPanel(new FlowLayout());
JPanel k = new JPanel(new FlowLayout());
Run Code Online (Sandbox Code Playgroud)

应该:

p = new JPanel(new GridLayout(2, 1));
l = new JPanel(new FlowLayout());
k = new JPanel(new FlowLayout());
Run Code Online (Sandbox Code Playgroud)

使用堆栈跟踪再次读取错误.它告诉你问题的哪一行,错误的类型告诉你发生了什么,在这种情况下.