Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: GUI HELP

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GUI HELP

    I've just started to learn GUI and I'm trying to learn how to make the window open full screen, could someone please add it in cause GUI makes no sense to me.

    import javax.swing.*;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class HelloWorld implements ActionListener{
    	static String text= "hello world";
    	public static int count1;
    	public static int count2;
    	JFrame frame;
    	JPanel content;
    	JLabel lblGeneral1;
    	JLabel lblGeneral2;
    	JButton btnTest1;
    	JButton btnTest2;
    	JLabel label1;
    	JLabel label2;
     
    	public HelloWorld(){
    		//create frame
    		frame=new JFrame ("Hello World");	
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
     
    		//create panel
    		content= new JPanel();
    		content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
     
    		//set button1
    		btnTest1=new JButton("Superior");
    		btnTest1.setActionCommand("1");
    		btnTest1.addActionListener(this);
    		btnTest1.setAlignmentY(Component.CENTER_ALIGNMENT);
    		btnTest1.setAlignmentX(Component.LEFT_ALIGNMENT);
    		content.add(btnTest1);
     
    		//set button2
    		btnTest2=new JButton("Superior");
    		btnTest2.setActionCommand("2");
    		btnTest2.addActionListener(this);
    		btnTest2.setAlignmentY(Component.CENTER_ALIGNMENT);
    		btnTest2.setAlignmentX(Component.RIGHT_ALIGNMENT);
    		content.add(btnTest2);
     
    		//create label
    		lblGeneral1=new JLabel(Integer.toString(count1));
    		lblGeneral1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    		lblGeneral1.setAlignmentX(Component.LEFT_ALIGNMENT);
    		content.add(lblGeneral1);
     
    		//create label
    		lblGeneral2=new JLabel(Integer.toString(count2));
    		lblGeneral2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    		lblGeneral2.setAlignmentX(Component.RIGHT_ALIGNMENT);
    		content.add(lblGeneral2);
     
    		//add picture box
    		String imgStr = "Penguins.jpg";		
    		ImageIcon image = new ImageIcon(imgStr);
    		JLabel label1 = new JLabel(image);
    		label1.setAlignmentY(Component.TOP_ALIGNMENT);
    		label1.setAlignmentX(Component.LEFT_ALIGNMENT);
    		content.add(label1);
     
    		//add picture box
    		String imgStr2 = "Penguins.jpg";		
    		ImageIcon image2 = new ImageIcon(imgStr);
    		JLabel label2 = new JLabel(image);
    		label2.setAlignmentY(Component.TOP_ALIGNMENT);
    		label2.setAlignmentX(Component.RIGHT_ALIGNMENT);
    		content.add(label1);
     
    		//content to frame
    		frame.setContentPane(content);
     
    		//set display
    		frame.pack();
    		frame.setVisible(true);
    	}
     
    	//handle button click
    	public void actionPerformed(ActionEvent event){
    		String btn=event.getActionCommand();
    		if (btn.equals("1"))
    			count1++;
    		else
    			count2++;
    		lblGeneral1.setText(Integer.toString(count1));
    		lblGeneral2.setText(Integer.toString(count2));
     
    	}
     
    	//show the GUI
    	public static void runGUI(){
    		JFrame.setDefaultLookAndFeelDecorated(true);
    		HelloWorld greeting=new HelloWorld();
    	}
     
    	public static void main (String[] args){
    		//methods that show GUI should be run from a event dispatcher
    		count1=0;
    		count2=0;
    		javax.swing.SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				runGUI();
    			}
    		});
    	}	
    }
    Last edited by Kshahzada; February 3rd, 2012 at 08:08 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: GUI HELP

    make the window open full screen, could someone please add it in cause GUI makes no sense to me.
    We are not a code service, but we will help you should you have problems in your attempts. Yet I fail to see what you've attempted. Did you search the internet or look through the API to look for a solution? With what you found, what did you try and what happened?
    And please wrap your code in the proper tags([highlight=java][/highlight])

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI HELP

    I searched up how to make a GUI jframe open in a maximized window setting, but i'm new to programming and didn't really understand what the suggestions were. I tried some method called setExtendedState(MAXIMIZED_BOTH), but the different ways I integrated it either crashed the program or had no effect. I'm just trying to find out the command I should use and how to properly integrate it.

    (p.s. this was my first ever post on a programming forum, sorry for forgetting proper etiquette)

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI HELP

    I searched up how to make a GUI jframe open in a maximized window setting, but i'm new to programming and didn't really understand what the suggestions were. I tried some method called setExtendedState(MAXIMIZED_BOTH), but the different ways I integrated it either crashed the program or had no effect. I'm just trying to find out the command I should use and how to properly integrate it.

    (p.s. this was my first ever post on a programming forum, sorry for forgetting proper etiquette)

Tags for this Thread