Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to make a button close a GUI?  (Read 1176 times)
0 Members and 1 Guest are viewing this topic.
Offline loliown

Senior Newbie





« Posted 2011-05-13 06:19:43 »

Anyways i have yet to figure this out but i'm trying to make a GUI that'll allow login and such.

Anyways here is methods
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
private void initialize() {
      frame = new JFrame();
      container = frame.getContentPane();
      container.setLayout(new FlowLayout());
      idLabel = new JLabel("Id:");
      id = new JTextField("", 12);
      passwordLabel = new JLabel("Password:");
      password = new JPasswordField(8);
      password.setEchoChar('*');
      button2 = new JButton("Log in");
      container.add(idLabel);
      container.add(id);
      container.add(passwordLabel);
      container.add(password);
      container.add(button2);
      //container.add(legal, BorderLayout.CENTER);
     frame.setSize( 250,200 );
      frame.setVisible( true );
      frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
      button2.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent ae) {
            if (ae.getSource() == button
                  && id.getText().equals("1")
                  && (new String(password.getPassword()))
                  .equals("2"))
            container.remove(idLabel);
            container.remove(id);
            container.remove(passwordLabel);
            container.remove(password);
            container.remove(button2);
            secondScreen();
            frame.setSize(300, 300);
         }
      });
   }

   
   public void secondScreen() {
      //frame = new JFrame();
     //container.add(text);
     //layout = new FlowLayout();
     container.remove(idLabel);
      container.remove(id);
      container.remove(passwordLabel);
      container.remove(password);
      container.remove(button2);
      frame.setSize(300, 300);
      frame.setTitle("second screen");
      container = frame.getContentPane();
      JContentPane = new JPanel();
      JContentPane.setLayout(new BorderLayout());
      frame.setResizable(false);
      frame.pack();
      area = new JTextArea();
      area.setEditable(false);
      text = new JTextField("testing");
      frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
      frame.setVisible(true);
      button = new JButton("testing");
      button4 = new JButton("testing");
      button3 = new JButton("testing");
      container.add(button, BorderLayout.SOUTH);
      container.add(button3, BorderLayout.EAST);  
      container.add(button4, BorderLayout.WEST);
      container.add(area, BorderLayout.SOUTH);
      button.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent ae) {
            if ( ae.getSource( ) == button )
               sendMessage("Opening webpage.");
               openWebpage();                    
         }
      });
     
   }  


yes, it's really "nooby" as i'm still learning but what i want to do is once you hit the button "login" it'll close that JFrame and secondScreen(); will open a new one with a different layout, or can someone tell me how to make it switch from a FlowLayout to a BorderLayout without causing any nullpointerexceptions.

None of my books have anything related to my problem and i don't know what to do exactly. Help is VERY appreciated.
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #1 - Posted 2011-05-13 06:35:52 »

Have a look at CardLayout. CardLayout is, just like its name suggests, a layout where each component that you add is a card. Only 1 card can be shown at a time and you can easily flip between each. So you can add a JPanel that has all your components, and then when you click on Login, it will "flip" to a new card that has a different JPanel. I suggest reading through the Java tutorials on CardLayout to familiarize yourself with it.

On the other hand, if you want the effect of closing a window and opening another window, then it simply requires you to call setVisible(false) on the first one, then setVisible(true) on the new one.

Offline loliown

Senior Newbie





« Reply #2 - Posted 2011-05-13 06:45:46 »

Wow, thank you very much.

Currently going to use cardLayout as it seems it'll be better to use then setVisible for what i'm planning on doing.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #3 - Posted 2011-05-13 06:59:11 »

Glad to help Smiley

Offline ToXSiK

Senior Newbie





« Reply #4 - Posted 2011-05-13 20:01:22 »

Also, it's never safe to be initializing multiple JFrames within the same program, especially if you want them to be able to "Talk".  For your situation, I would really recommend initializing your JFrame, do setVisible(false);, and then use a JDialog component to get the login information.  If it's correct, poof!  the JFrame is visible, if not, well say wrong password or something!  Cheesy

I tried making an application using JFrames that when certain buttons were pushed spawned more JFrames, but it's just a bad idea and without the use of static variables, it's really hard to get them to communicate.


Here is a really good tut on working with very basic Dialog boxes, just make one to grab the login information.

http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html


Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2011-05-13 20:05:03 »

There is absolutely no technical issue of using multiple JFrames. It just works. No need for hacks or anything.

How well it integrates with you code, depends on the quality of your code.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ToXSiK

Senior Newbie





« Reply #6 - Posted 2011-05-13 20:14:07 »

I am recommending this because with using multiple JFrames, especially if you are retreiving information, you have to use static variables to give one jframe access to another jframe's variables.  I never said it didn't work, it's just not practical.  With dialogs, you can instantiate a dialog from a jframe class and easily pull information from it.

ToXSiK
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2011-05-13 20:15:29 »

I am recommending this because with using multiple JFrames, especially if you are retreiving information, you have to use static variables to give one jframe access to another jframe's variables.
Not true.

As said, it highly depends on the quality of your code. If your code requires you to connect JFrames with static variables, then it shows you have a problem structuring your UI classes in an Object Oriented way. I develop in Swing for intranet applications these days, and I only use static for constants.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #8 - Posted 2011-05-13 21:15:56 »

not really related to JFrame but I find that often very usefull to use a main controller, exemple to help talk from GUI to DataSection or DataSection to GUI

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
public class SoftController
{
   MyJFrame frame1;
   MyJFrame frame2;
   Data data;
    SoftController()
   {
   this.frame1=new MyJFrame(this)
   }

   public MyJFrame getAJFrame(.....)
   {
   
   }
   
   public MyJFrame getFrame1(.....)
   {
   
   }
   
   public MyJFrame getFrame2(.....)
   {
   
   }
   
   public Data getData(.....)
   {
   
   }
   
   public OtherClass getOtherClass(.....)
   {
   
   }
   
   //Etc....
}

//Base/super class
public class MyJFrame extends JFrame
{
 private SoftController softController;
 
 public MyJFrame(SoftController softController)
 {
  super();
  this.softController=softController;
 }
 
 public SoftController getSoftController()
 {
  return this.softController;
 }
}

public class OtherClass extends OtherClassParent
{
 private SoftController softController;
 
 public OtherClass(SoftController softController)
 {
  super();
  this.softController=softController;
 }
 
 public SoftController getSoftController()
 {
  return this.softController;
 }
}

public class MyJFrame2 extends MyJFrame
{
 public MyJFrame(Controller controller)
 {
  super(controller);
 }

  //Somewhere requiering other JFrame or any other component
 this.getController().getJFrame(...).doSomething();
  this.getController().getOtherClass(...).doSomething();
}


Offline ToXSiK

Senior Newbie





« Reply #9 - Posted 2011-05-13 21:58:53 »

You're missing my point... But I'm not the one to argue, so I'll just let it be.

I am recommending this because with using multiple JFrames, especially if you are retreiving information, you have to use static variables to give one jframe access to another jframe's variables.
Not true.

As said, it highly depends on the quality of your code. If your code requires you to connect JFrames with static variables, then it shows you have a problem structuring your UI classes in an Object Oriented way. I develop in Swing for intranet applications these days, and I only use static for constants.

Perhaps the constructor
Quote
JDialog(Frame owner)
Will explain what I am trying to say?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zoto

Senior Member


Medals: 4



« Reply #10 - Posted 2011-05-14 16:35:15 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
import javax.swing.JFrame;

public class MyFrame extends JFrame {
   
   
   public void handleMessage(Object message){
      // Message passing without statics
  }

}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (55 views)
2014-04-15 18:08:23

BurntPizza (53 views)
2014-04-15 03:46:01

UprightPath (66 views)
2014-04-14 17:39:50

UprightPath (49 views)
2014-04-14 17:35:47

Porlus (66 views)
2014-04-14 15:48:38

tom_mai78101 (90 views)
2014-04-10 04:04:31

BurntPizza (151 views)
2014-04-08 23:06:04

tom_mai78101 (247 views)
2014-04-05 13:34:39

trollwarrior1 (204 views)
2014-04-04 12:06:45

CJLetsGame (211 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!