0

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

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

but the testing2.setVisible(true) says "Cannot make a static reference to the non-static method setVisible(boolean) from the type JComponent" Any help is 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 the paintComponent method
      // THE MAIN DEMO OF THIS 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 is 0,0
        setLocation(10,200); // default is 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 the paintComponent method
      // THE MAIN DEMO OF THIS 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 is 0,0
        setLocation(10,200); // default is 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
user6680
  • 79
  • 6
  • 34
  • 78
  • Before extending this program design further, please have a look at, [The Use of Multiple JFrames: Good or Bad Practice](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice). – Hovercraft Full Of Eels Jul 10 '15 at 11:44
  • 1
    Side note: please follow the java coding conventions. Class names start with upper case letters; not like "testing2". – GhostCat Jul 10 '15 at 11:55

1 Answers1

1

use test, not testing2

            test.setVisible(true); //was testing2.setVisible(true);
ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • Nice catch. But with that changed to test.setVisible(true) I'm not getting any error, but also when I click the button to open the other Jframe class, it isn't opening. Any ideas why? – user6680 Jul 10 '15 at 12:00
  • add a debug/println statement before this. If test is a subclass of `Window`, it will show up on the screen when setVisible is called – ControlAltDel Jul 10 '15 at 12:55
  • Do you mean to write System.err.println(); and if so, what am I putting in the parenthesis? – user6680 Jul 10 '15 at 13:58
  • Yes, system.err.println("Here") – ControlAltDel Jul 10 '15 at 14:02
  • I added System.err.println("Here"); testing2 test = new testing2(); test.setVisible(true); in that order, and when I click the button I get in the console in red text "Here" – user6680 Jul 10 '15 at 16:00
  • because testing2 is a JPanel. You need to put it in a (J)Frame or a (J)Window and call setVisible(true) on that – ControlAltDel Jul 10 '15 at 16:08
  • I moved it from JPanel to JFrame, but now there is no output to the console at all. I updated the code above. (See 2nd class listed) Any ideas why? – user6680 Jul 10 '15 at 16:18
  • If there is no output that means there's a bug in your code, and that you are never executing this block – ControlAltDel Jul 10 '15 at 18:00