[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Class that extends JFrame

Can I get an example of a simple class that extends JFrame without any parameters to its constructor?

Thanks!
0
dshrenik
Asked:
dshrenik
  • 3
1 Solution
 
for_yanCommented:
you mean something like that:

public class MyFrame extends JFrame {

public MyFrame() {
super("My Frame");



}


}

Open in new window

0
 
dshrenikAuthor Commented:
When I add a main method I do not get a frame pop up:

      public static void main(String [] args) {
            MyFrame testMonitor = new MyFrame();
      }
0
 
for_yanCommented:
you need say
this.setVisible(true);
inside constructor
0
 
for_yanCommented:

look at this real example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MousePressedUp extends JFrame implements MouseListener {
           MyMousePanel mmp;
    public MousePressedUp(){

         mmp = new MyMousePanel();
          mmp.addMouseListener(this);
        Container c = this.getContentPane();
        c.add(mmp);
        this.setSize(300,300);
        this.setVisible(true);

    }



  public void mouseClicked(MouseEvent me){}

      public void mousePressed(MouseEvent me){

                  mmp.setString((int)me.getPoint().getX() + "  "  +  (int)me.getPoint().getY());
                mmp.setX((int)me.getPoint().getX());
                  mmp.setY((int)me.getPoint().getY());
           repaint();
      }

      public void mouseReleased(MouseEvent me){
                mmp.setString("");
              repaint();


      }
      public void mouseEntered(MouseEvent me){}
      public void mouseExited(MouseEvent me){}

    public static void main(String[] args) {
        new MousePressedUp();
 }
}

class MyMousePanel extends JPanel {
    String s = "";
    int x = 50;
    int y = 50;


   public void setX(int x){
       this.x = x;
   }
    public void setY(int y){
       this.y = y;
   }


  public void setString(String s){
      this.s = s;
  }
    public void paint(Graphics g){

        g.drawString(s, x,y );

    }

    

}

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now