Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6777229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:07:23+00:00 2026-05-26T16:07:23+00:00

I found this code in internet. I complied it and it executes fine. Then

  • 0

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

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:07:24+00:00Added an answer on May 26, 2026 at 4:07 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i have problem with this code, i found it on the internet and
This is some code I found on the internet. I'm not sure how it
I found some mapkit code on the internet that looks like this: - (void)recenterMap
I found this code on the internet on how to to display message/pop up
I found this code over the internet but am unsure about how to use
I found this code on internet : Class Book{ Public: void operator()(int Counter) const
I've found this code in the internet: $.tablesorter.addWidget({ id: memorizeSortOrder, format: function(table) { if
I found this code snipped on the internet: digits 0 = [0] digits n
After a while of searching the internet, I found this piece of code that
I found this handy code on the internet, - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.