4 Replies - 418 Views - Last Post: 15 November 2012 - 06:11 PM Rate Topic: -----

#1 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

JButton to make another GUI appear

Posted 15 November 2012 - 01:52 PM

Hello, say if i wanted to create a Jbutton on an existing GUI named instructions. I want the actionlistener to open another small GUI that just has one JLabel on it that could give instructions. how would I go about doing this? ill show you what I have so far. I am trying to create a game that allows you to combine different components(JRadioButtons) and depending on what you have checked will create an image which I will eventually store to a gridlayout.
the main GUI
class WorkBench extends JFrame implements ActionListener{
	private JPanel main,left,top,right,bottom;
	private JButton augment, instr;
	private JRadioButton claw, beak, fang; //Radio Buttons for MIGHT
	private JRadioButton evil, lost, righteous; //Radio Buttons for SOUL
	private JRadioButton scholar, traveler, artisan; //Radio Buttons for WISDOM
	private JLabel jlbmight, jlbsoul, jlbwisdom;
	WorkBench(){
		//Set up JButtons
		augment = new JButton("Augment");
		instr= new JButton("Instructions");
		
		//Set up panels
		top = new JPanel();
		right = new JPanel();
		left = new JPanel();
		main = new JPanel();
		bottom = new JPanel();
		bottom.setLayout(new FlowLayout(FlowLayout.CENTER));
		bottom.add(augment);
		bottom.add(instr);
		main.setLayout(new BorderLayout());
		left.setLayout(new GridLayout(3,1));
		right.setLayout(new GridLayout(3,1));
		top.setLayout(new FlowLayout(FlowLayout.CENTER));
		top.setBackground(Color.cyan);
		right.setBackground(Color.yellow);
		left.setBackground(Color.red);
		jlbmight = new JLabel("<Aspects of MIGHT",JLabel.CENTER);
		jlbsoul = new JLabel("<Aspects of SOUL>",JLabel.CENTER);
		jlbwisdom = new JLabel("<Aspects of WISDOM>",JLabel.CENTER);
		//Set up Radio Buttons
		
		ButtonGroup might = new ButtonGroup();
		ButtonGroup soul = new ButtonGroup();
		ButtonGroup wisdom = new ButtonGroup();
		
		might.add(claw = new JRadioButton("CLAW"));
		might.add(beak = new JRadioButton("BEAK"));
		might.add(fang = new JRadioButton("FANG"));
		left.add(claw);
		left.add(beak);
		left.add(fang);
		main.add(left,BorderLayout.WEST);
		
		soul.add(evil = new JRadioButton("EVIL"));
		soul.add(lost = new JRadioButton("LOST"));
		soul.add(righteous = new JRadioButton("RIGHTEOUS"));
		top.add(evil);
		top.add(lost);
		top.add(righteous);
		main.add(top,BorderLayout.NORTH);
		
		wisdom.add(scholar = new JRadioButton("SCHOLAR"));
		wisdom.add(traveler = new JRadioButton("TRAVELER"));
		wisdom.add(artisan = new JRadioButton("ARTISAN"));
		right.add(scholar);
		right.add(traveler);
		right.add(artisan);
		main.add(right,BorderLayout.EAST);
		main.add(bottom,BorderLayout.SOUTH);
		add(main);
		
		
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == instr){
			Instructions i = new Instructions();
			i.setVisible(true);
			i.setSize(300,300);
			i.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			add(i);
		}
		
	}
	
}


The instructions GUI
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;


class Instructions extends JFrame {
	private static JLabel instructions;
	
	Instructions(){
		instructions = new JLabel();
		instructions.setText("YOU are the Arcane Inventor... A magical source that is responsible" +
							" for creating all of the life-forms in existence. It is the Arcane " +
						     "Inventor's job to combine elements of MIGHT, SOUL, and WISDOM to bring " +
							"These beings into existence. Good Luck...");
		setLayout(new BorderLayout());
		add(instructions, BorderLayout.CENTER);
		add(this);
				
				
				
	}


}



Is This A Good Question/Topic? 0
  • +

Replies To: JButton to make another GUI appear

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: JButton to make another GUI appear

Posted 15 November 2012 - 02:04 PM

add(this);  


add(this) to what

You are adding a Jframe to itself

Now all depends if you want the new created JFrame to stay visible or not after it is displayed before returning to the original JFrame.
If it is the case, better to extends JDialog that is modal and was create d just for that.

If you want it to stay, you are almost done but it is not
add(i);
but i.setVisible(true);

You cannot have a JLabel with mu;ltiple line as you did unless you put your text as HTML
label.setText(<html>Bla bla bla <p>second line...</html>);
Was This Post Helpful? 1
  • +
  • -

#3 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Re: JButton to make another GUI appear

Posted 15 November 2012 - 02:39 PM

Hmm, I am still a little confused. What i really want is for the WorkBench GUI to stay visible and when I press instructions I want a seperate GUI to just lay on top of it. So pretty much I want them to both be visible. Is this possible?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: JButton to make another GUI appear

Posted 15 November 2012 - 02:59 PM

Yes, in your actionPerformed() you simply have to create a new Instruction()
set it's size
and set it visible(true);
Was This Post Helpful? 1
  • +
  • -

#5 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Re: JButton to make another GUI appear

Posted 15 November 2012 - 06:11 PM

This worked perfectly. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1