java - Opening JFrame window on jbutton click -


i'm having issues opening new jframe window on click. think i'm doing right, i'm not sure. in action listener (in first class listed below)

testing2 test = new testing2();                 testing2.setvisible(true); 

but testing2.setvisible(true) says "cannot make static reference non-static method setvisible(boolean) type jcomponent" appreciated.

import java.awt.*;     import java.awt.event.*;     import javax.swing.*;      /*************************************************************          *  textpanel class (with main method)      *************************************************************/      class testing2 extends jpanel {         public testing2() {              jbutton btntesting = new jbutton("testing 2");             add(btntesting);         }       // override paintcomponent method       // main demo of example:        public void paintcomponent(graphics g) {         super.paintcomponent(g);         font f = new font("sansserif", font.bold, 14);         font fi = new font("sansserif", font.bold + font.italic, 14);         fontmetrics fm = g.getfontmetrics(f);         fontmetrics fim = g.getfontmetrics(fi);         int cx = 75; int cy = 100;         g.setfont(f);         g.drawstring("hello, ", cx, cy);         cx += fm.stringwidth("hello, ");         g.setfont(fi);         g.drawstring("world!", cx, cy);       } //paintcomponent        //=============================================       ///////////// main ////////////////////////////        public static void main(string[] args) {         jframe f = new myframe("my hello world frame");         f.show();       } //main      } //class textpanel      /*************************************************************             myframe class      *************************************************************/      class myframe extends jframe {       public myframe(string s) {         // frame parameters         settitle(s);         setsize(300,200); // default size 0,0         setlocation(10,200); // default 0,0 (top left corner)          // window listeners         addwindowlistener(new windowadapter() {           public void windowclosing(windowevent e) {             system.exit(0);           } //windowclosing         }); //addwindowlister          // add panels         container contentpane = getcontentpane();         contentpane.add(new testing());        } //constructor myframe     } //class myframe 

import java.awt.*;     import java.awt.event.*;     import javax.swing.*;      /*************************************************************          *  textpanel class (with main method)      *************************************************************/      class testing extends jpanel {         public testing() {              jbutton btntesting = new jbutton("testing 2");             btntesting.addactionlistener(new actionlistener() {                 public void actionperformed(actionevent e) {                  }             });             add(btntesting);         }       // override paintcomponent method       // main demo of example:        public void paintcomponent(graphics g) {         super.paintcomponent(g);         font f = new font("sansserif", font.bold, 14);         font fi = new font("sansserif", font.bold + font.italic, 14);         fontmetrics fm = g.getfontmetrics(f);         fontmetrics fim = g.getfontmetrics(fi);         int cx = 75; int cy = 100;         g.setfont(f);         g.drawstring("hello, ", cx, cy);         cx += fm.stringwidth("hello, ");         g.setfont(fi);         g.drawstring("world!", cx, cy);       } //paintcomponent        //=============================================       ///////////// main ////////////////////////////        public static void main(string[] args) {         jframe f = new myframe("my hello world frame");         f.show();       } //main      } //class textpanel      /*************************************************************             myframe class      *************************************************************/      class myframe extends jframe {       public myframe(string s) {         // frame parameters         settitle(s);         setsize(300,200); // default size 0,0         setlocation(10,200); // default 0,0 (top left corner)          system.err.println("here");         testing2 test = new testing2();         test.setvisible(true);           // window listeners         addwindowlistener(new windowadapter() {           public void windowclosing(windowevent e) {             system.exit(0);           } //windowclosing         }); //addwindowlister          // add panels         container contentpane = getcontentpane();         contentpane.add(new testing());        } //constructor myframe     } //class myframe 

use test, not testing2

            test.setvisible(true); //was testing2.setvisible(true); 

Comments