简体   繁体   中英

Frame shows up only when constructor is called

I found this code in internet. I complied it and it executes fine. Then i moved the part of constructor to a method and creating a object i tried to call the method. I don't know what i am missing but the Frame just wont show up. What is wrong when transferring constructor to a method?

Working

import java.awt.event.MouseEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    import java.io.IOException;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import javax.imageio.ImageIO;
    public class MyFrame extends JFrame
    {
        Container cont;
        JLabel label = new JLabel();

        public MyFrame() throws IOException
        {   

            setSize(300,300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BufferedImage img = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"));
            System.out.println(img.toString());

            label.setIcon(new ImageIcon(img));

            cont = (Container) getContentPane().add(label);
            cont.setFocusable(true);
            InputHandler hand = new InputHandler();
            cont.addKeyListener(hand);
            cont.addMouseListener(hand);
            cont.setBackground(Color.BLACK);
        }
        public class InputHandler implements KeyListener, MouseListener
        {
            public void keyTyped(KeyEvent kt)
            {
                System.out.println(kt.getKeyChar());
            }
            public void keyPressed(KeyEvent kp)
            {
                if(kp.getKeyChar() == 'w')
                {
                    cont.setBackground(Color.PINK);
                    repaint();
                }
            }
            public void keyReleased(KeyEvent kr)
            {
                System.out.println(kr.getKeyChar());
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("Mouse CLicked");
                    }

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("Mosue Pressed");
                    }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("Mosue Released");
                    }

            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.println("Mouse Entered");
                    }

            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("Mosue Exited");
                    }
        }

        public static void main(String[] args) throws IOException {
            new MyFrame().setVisible(true);

        }
    }

EDIT: Changed code (Not Working)

import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class MyFrame extends JFrame
{
    Container cont;
    JLabel label = new JLabel();

   void MyFrame(){

   }
    public void Display() throws IOException
    {   

        setSize(280,480);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BufferedImage img = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"));
        System.out.println(img.toString());

        label.setIcon(new ImageIcon(img));

        cont = (Container) getContentPane().add(label);
        cont.setFocusable(true);
        InputHandler hand = new InputHandler();
        cont.addKeyListener(hand);
        cont.addMouseListener(hand);
        cont.setBackground(Color.BLACK);
       rootPane.setVisible(true);


    }
    public class InputHandler implements KeyListener, MouseListener
    {
        public void keyTyped(KeyEvent kt)
        {
            System.out.println(kt.getKeyChar());
        }
        public void keyPressed(KeyEvent kp)
        {
            if(kp.getKeyChar() == 'w')
            {
                cont.setBackground(Color.PINK);
                repaint();
            }
        }
        public void keyReleased(KeyEvent kr)
        {
            System.out.println(kr.getKeyChar());
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println("Mouse CLicked");
                }

        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println("Mosue Pressed");
                }

        @Override
        public void mouseReleased(MouseEvent e) {
            System.out.println("Mosue Released");
                }

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("Mouse Entered");
                }

        @Override
        public void mouseExited(MouseEvent e) {
            System.out.println("Mosue Exited");
                }
    }

    public static void main(String[] args) throws IOException {
        MyFrame mf = new MyFrame();
        mf.Display();

    }
}

Just replace rootPane.setVisible(true); with setVisible(true); in your Display() method and it should display your JFrame .

You probably didn't do it right.

http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html

public static void main(String [] args){
    JFrame jf = new JFrame("JChooser");
    jf.setSize(400,400);
    jf.setVisible(true);
}

This works and its in the main method.

you are getting an exception because of this line:

BufferedImage img = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"));

cause it cant load it... as you probably dont have this file on your computer

to see this working remove the following lines:

BufferedImage img = ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"));
System.out.println(img.toString());

label.setIcon(new ImageIcon(img));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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