Results 1 to 6 of 6

Thread: JFrame help

  1. #1
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Post JFrame help

    Hey guys... I'm trying to create a program that has two labels... one in the top left and one in the top right... so far when i run it only the one in the top right (label2) shows... also In the program there will be multiple button and when I click a button it will show a different panel and then i can go back to the first panel to select other panels... so far i haven't figured out how to make panels visibility go false/true with actionlistener. last thing... when i have more then one panel added to the frame none of them show up. how can i fix these problems? thanks for the help ;) please leave example code

    Java Code:
    //Matthew
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test{
    
    	public static void main(String[] args){
       
    		JFrame frame = new JFrame("Test");
    		frame.setSize(600,400);
    		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		frame.setVisible(true);
          
          JPanel panel = new JPanel();
          JPanel panel2 = new JPanel();
          panel.setVisible(true);
          panel2.setVisible(false);
    
    		JLabel label = new JLabel("Chemistry Mole Converter",SwingConstants.LEFT);
          JLabel label2 = new JLabel("Made by: Matthew",SwingConstants.RIGHT);
          
    		frame.add(label, BorderLayout.NORTH);
          frame.add(label2, BorderLayout.NORTH);
          
    		JButton button = new JButton("Grams to Moles");
          JButton button2 = new JButton("Liters to Moles");
          
    		frame.add(panel);
          frame.add(panel2);
          
    		panel.add(button);
          panel.add(button2);
          
          button.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
               //panel.setVisible(false);
               //panel2.setVisible(true);
             }
          });
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: JFrame help

    You can't add more than one component to the same region of a BorderLayout. You might have to use a nested layout instead. Recommended reading: Layouts - Tutorials - Static Void Games
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: JFrame help

    Also, I would recommend adding your components to the JPanels before you add them to the JFrame. It's just good practice.

  4. #4
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default Re: JFrame help

    how do i make it so that you have the main panel that your first see then i click a button and i see new options but i can still go back to the old ones?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: JFrame help

    Quote Originally Posted by mbschutlz97 View Post
    how do i make it so that you have the main panel that your first see then i click a button and i see new options but i can still go back to the old ones?
    Sounds like a job for a CardLayout, or for the setVisible() method.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    2

    Default Re: JFrame help

    You said you don't know how to make them go visible in you code but you clearly have it in you code. lines 36,37. Add as for the layout issue Kevin is right you can't do it with border layout. You might have to change the zcomponent too. To do this use eather add(JComponent,zposition); or JComponentZPosition(int i);
    With the right know how, anything is possible

Similar Threads

  1. Click in JFrame Titlebar sets the JFrame Opaque
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 02-02-2013, 07:29 AM
  2. Replies: 3
    Last Post: 11-27-2011, 05:18 AM
  3. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •