Jump to content

Creating additional windows

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
Been doing some work with GUI's but cannot get a new window created from the another. As an example once a button is pressed i want a new window to open.

Driver Class
import java.awt.*;

import javax.swing.*;


public class DriverClass

{

	public static void main(String[]args)

	{

		JFrame frame = new JFrame("Main Panel");

		frame.setVisible(true);

		frame.setSize(500,500);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Panel1 myPanel = new Panel1();

		frame.getContentPane().add(myPanel);

	}

}

Panel1 Class
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;


public class Panel1 extends JPanel implements ActionListener

{

	JPanel mainPanel;

	JButton button1;

	

	public Panel1()

	{

		button1 = new JButton("Press Me!");

		mainPanel = new JPanel();

		

		add(mainPanel);

		mainPanel.add(button1);

		button1.addActionListener(this);

	

	}


	public void actionPerformed(ActionEvent e)

	{

		if(e.getSource()==button1)

		{

			Panel2 myPanel2 = new Panel2();

			JFrame secondFrame = new JFrame("New Panel");

			JPanel secondPanel = new JPanel();

			secondFrame.getContentPane().add(secondPanel);

			secondFrame.setBackground(Color.RED);

			secondFrame.setVisible(true);

			secondFrame.setSize(500,500);

			secondFrame.setBackground(Color.GREEN);

			

			}

		}

	}	


Panel2 class - once button is pressed a new window is to open
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;


public class Panel2 extends JPanel

{

	private JPanel mainPanel;

	

		private JButton button2;

		public Panel2()

	{

		mainPanel = new JPanel();

			

		add(mainPanel);

		mainPanel.setBackground(Color.YELLOW);

			

		button2 = new JButton("It Works");

		mainPanel.add(button2);

	}


}

thanks in advance for helping

#2
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Here is a quick and dirty way to do it.
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test2 implements ActionListener{
    
    public JFrame f1 = new JFrame("Frame 1");
    public JFrame f2 = new JFrame("Frame 2");
    
    public void startFrame(){
        f1.setSize(500, 500);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel p1 = new JPanel();
        JButton b1 = new JButton("Press Me!");
        b1.addActionListener(this);
        p1.add(b1);
        f1.add(p1);
        f1.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        f2.setSize(500,500);
        f2.setBackground(Color.green);
        f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f2.setVisible(true);
    }
    
    public static void main(String args[]){
        Test2 t = new Test2();
        t.startFrame();
    }
}

twas brillig

#3
GMVResources

GMVResources

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
lol, gj tate.

#4
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
Ok thanks. But im still on the same step. After creating to second window I cant add any components to the frame. For the test i made above im trying to get the second window to be yellow. And prefferably an object of Panel2, not an ordinary frame. Any ideas? or is there another way of doing things?

#5
tate

tate

    Learning Programmer

  • Members
  • PipPipPip
  • 90 posts
Um... your second frame doesn't even add your panel2 object above. Your second frame is an instance variable of Panel1's actionPerformed method and not an object of really anything. You can put a JFrame object in Panel2 so you can access it later if that is what you want.
twas brillig

#6
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
hmm seems like i have to rethink a bit lol. thanks tate :)

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Maybe this tutorial is helpful: http://forum.codecal...ly-windows.html

#8
H2O Pure

H2O Pure

    Newbie

  • Members
  • PipPip
  • 28 posts
Now if had of found this before i would have been right haha
Thanks for your help