Installing something hosted or invisible java

changed the project since its inception. like. the image still doesn't change.

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();
            }
        }
    }
}

      

Sorry for the bad indentation. im sure the method l.add (j); the reason the image doesn't change.

any ideas what it should be?

+2


a source to share


3 answers


Note: This answer was made for revisions of questions 1 and 2.

This is not an Awt error, it is a NullPointerException.

Your l field is null because 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 the stack trace error again. It tells you which line is the problem and the type of error tells you what happened in that case.

+6


a source


The problem is that you are declaring a global JPanel

named l

, but then when you instantiate JPanel

in your constructor, you are declaring and assigning a local JPanel

named scope l

. When you try to add a component to yours actionPerformed

, you try to add it to the global var null

.



+2


a source


I am assuming that you have not set the correct image position. Are you sure the images are in the location you specified? Which IDE are you using? If you are using Eclipse, updating your project might help.

+1


a source







All Articles