Results 1 to 7 of 7
  1. #1
    Jkey is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Using swing components on multiple classes

    Hey guys, im trying to make my own program, but i've problems calling swing functions to main, i just get the new jlabel in other window. What's wrong?

    public class Client extends JFrame {

    static Dimension res = Toolkit.getDefaultToolkit().getScreenSize();


    private JLabel new;

    public void hehe(){
    setLayout(null);
    new = new JLabel("test jlabel new");
    new.setForeground(Color.orange);
    new.setBounds(res.width/2-250, 0, 500, 50);
    new.setFont(new Font("Tahoma",0,36));
    add(new);
    }

    Now i try to get it inside of my main class:

    public Gest(){
    Client asf = new Client();
    asf.setVisible(true);

    }
    heres my main:

    public static void main(String[] args) {
    Gest window1 = new Gest();
    window.setBounds(0,0,res.width,res.height);
    System.out.println(res.width/2);
    window.setResizable(false);
    window.setUndecorated(true);
    window.setVisible(true);


    This is only a part of the program, thanks in advance :)

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Using swing components on multiple classes

    It is very difficult to figure out what is going on with the fragments you provided. You have a Gest constructor with no class shown. You should also enclose your code in code tags to properly format it. [CODE][/CODE]. To help others help you, please include an Short, Self Contained, Correct Example. Here are some basic guidelines.

    1. Don't extend JFrame. Just declare an instance of it to add other components as appropriate.
    2. Let Client extend JPanel assuming you want to add other components and possibly paint to it.
    3. Add client instance to the JFrame instance.
    4. Add the labels to the client.
    5. Add other panels as necessary to the JFrame and or Client
    6. Ensure your instance fields are declared just within their enclosing class. Try to initialize them in the constructors as appropriate

    These are just guidelines and can be varied (except for #1 for which the need is rare).
    Check out the examples in the The Java™ Tutorials for creating GUI like applications.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Jkey is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Using swing components on multiple classes

    Ok mate, im sorry.

    Here's my main class:

    Java Code:
    package Gest;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Toolkit;
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.print.PageFormat;
    import java.awt.print.Printable;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Gest extends JFrame implements ActionListener,Printable{
    	
    	private static final long serialVersionUID = 1L;
    
    	
    	// variables JLabel and menus
    	
    	private JLabel tag1,resolution;
    	private JMenu menu1;
    	private JMenuBar bar;
    	private JMenuItem mi1, mi2, mi3;
    	static Dimension res = Toolkit.getDefaultToolkit().getScreenSize();
    	
    	public Gest(){
    		Client vm = new Client();
    		vm.setVisible(true);
    	
    		// JLabel
    		setLayout(null);
    		tag1 = new JLabel("My program", SwingConstants.CENTER);
    		tag1.setForeground(Color.orange);
    		tag1.setFont(new Font("Tahoma",0,36));
    		tag1.setBounds(res.width/2-250, 0, 500, 50);
    		tag1.setVisible(true);
    		add(tag1);
    		
    		// res
    		setLayout(null);
    		resolution = new JLabel("Your resolution: \n" +res.width+ " Width " +res.height+ " Height");
    		resolution.setForeground(Color.black);
    		resolution.setBounds(res.width/2, 0, 100, 0);
    		resolution.setFont(new Font("Tahoma",1,14));
    		resolution.setVisible(true);
    		add(resolution);	
    		
                     //menu
    		bar = new JMenuBar();
    		setJMenuBar(bar);
    		
    		//	Clients
    		menu1 = new JMenu("Clients");
    		bar.add(menu1);
    		
    		// New
    		mi1 = new JMenuItem("New");
    		mi1.addActionListener(this);
    		menu1.add(mi1);
    		
    		// Modify
    		mi2 = new JMenuItem("Modify");
    		mi2.addActionListener(this);
    		menu1.add(mi2);
    		
    		// Exit
    		mi3 = new JMenuItem("Exit");
    		mi3.addActionListener(this);
    		menu1.add(mi3);
    	
    	}
    	
    	public void actionPerformed(ActionEvent e) {
    		Container f=this.getContentPane();
    		if (e.getSource()==mi1){
    
    		}
    		if (e.getSource()==mi2){
    
    		}
    		if (e.getSource()==mi3){
    			System.exit(0);
    		}
    	}
    	
    
    	//  main
    	public static void main(String[] args) {
    		Gest window1 = new Gest();
    		window1.setBounds(0,0,res.width,res.height);
    		window1.setResizable(false);
    		window1.setUndecorated(true);
    		vwindow1.setVisible(true);
    		
    	}

    And now my other class:


    Java Code:
    package Gest;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Toolkit;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    
    @SuppressWarnings("serial")
    
    public class Client extends JFrame {
    	
    
    
    	private JLabel ps;
    	
    	public void jeje(){
    		setLayout(null);
    		ps = new JLabel("New jlabel test");
    		ps.setForeground(Color.orange);
    		ps.setBounds(res.width/2-250, 0, 500, 50);
    		ps.setFont(new Font("Tahoma",0,36));
    		add(ps);
    	}
    
    	}
    Last edited by Jkey; 06-07-2013 at 11:17 PM.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Using swing components on multiple classes

    First, you client class is messed up. I think there are some XML type constructs in there. Also, res is not visible in Client because it is declared in Gest. I was able to run the Gest class but that is about it (I had to remove the Printable interface since it wasn't implemented yet).

    Perhaps if you provide a summary of what you want to accomplish someone can help. I do not believe you want to use two JFrames but I will wait and see.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Jkey is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Using swing components on multiple classes

    Oh well, thanks you, those errors like res and printable are just some test that i did, i just want know how to structure my program, i thought that using multiple classes to organize all menus would be nice, but i have no idea at the moment :(

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Using swing components on multiple classes

    You can use as many classes as necessary. But typically you only need one JFrame to contain the other components.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Jkey is offline Member
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Using swing components on multiple classes

    Ahhhh ok ok mate, very thanks you for your pacience and understanding! :)

Similar Threads

  1. How to add multiple components to a tab pane?
    By LucienMontierre in forum AWT / Swing
    Replies: 3
    Last Post: 05-04-2012, 01:39 AM
  2. Multiple components in Jpanel
    By shrutisurulkar in forum AWT / Swing
    Replies: 1
    Last Post: 01-07-2011, 03:13 PM
  3. Jtable with Multiple Components in a ROw
    By kedia_rohit in forum AWT / Swing
    Replies: 1
    Last Post: 04-25-2010, 01:44 PM
  4. Printing Swing components in multiple pages
    By Rajeswari in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 06:49 PM
  5. Printing Swing components in multiple pages
    By Rajeswari in forum AWT / Swing
    Replies: 0
    Last Post: 07-27-2007, 06:01 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
  •