6 Replies - 301 Views - Last Post: 19 May 2014 - 10:43 AM Rate Topic: -----

#1 shoup0198  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-February 14

Java window settings

Posted 19 May 2014 - 09:15 AM

Pretty much I was just wondering if there was a way to get my Java window to open with a certain width and height. I've been googling and havent found to much valuable information. Maybe you guys could point me in the direction I need to be. Here is what I have so far.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;


public class JavaWindow{
    //Bring up the frame.
    private JFrame f = new JFrame("JavaWindow");
    
    //Insert a menu.
    private JMenuBar mb = new JMenuBar();
    private JMenu mnuFile = new JMenu("File");
    private JMenuItem mnuItemQuit = new JMenuItem("Quit");

    //Construct the GUI.
    public JavaWindow(){
	f.setJMenuBar(mb);

	//Build Menu.
	mnuFile.add(mnuItemQuit);
	mb.add(mnuFile);

	//Set up the main frame.
	f.getContentPane().setLayout(new BorderLayout());

	//Allows closing.
	f.addWindowListener(new ListenCloseWdw());

	//Menu listener.
	mnuItemQuit.addActionListener(new ListenMenuQuit());
    }

    //Close window when the file menu quit is pressed.
    public class ListenMenuQuit implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);         
        }
    }

    //Close window when 'x' is pressed.
    public class ListenCloseWdw extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);         
        }
    }

    public void launchFrame(){
        //Display Frame
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack(); 
        f.setVisible(true);
    }    

    public static void main(String args[]){
        JavaWindow gui = new JavaWindow();
        gui.launchFrame();
    }
}



When you compile that it runs a small window. I would like that window to be bigger when the file opens. Any ideas or direction? Thanks ahead of time guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Java window settings

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 851
  • View blog
  • Posts: 2,599
  • Joined: 29-July 11

Re: Java window settings

Posted 19 May 2014 - 09:16 AM

JFrame has a setSize()

This post has been edited by farrell2k: 19 May 2014 - 10:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Java window settings

Posted 19 May 2014 - 09:22 AM

//Close window when 'x' is pressed.
    public class ListenCloseWdw extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);        
        }
    }


Is also redundant. The program will end anyway when you hit the 'X' as you have set the default close operation to 'exit on close'. No need, at this point at least, to add a window listener at all.
Was This Post Helpful? 0
  • +
  • -

#4 shoup0198  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-February 14

Re: Java window settings

Posted 19 May 2014 - 09:36 AM

Where do I insert setWidth()? And Ryano thanks I'll be sure to remove the excess code....
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Java window settings

Posted 19 May 2014 - 09:43 AM

Add it to your launchFrame method. There is also a setHeight(int) method you can use as well. If you're using both you may want to use setSize(Dimension) which takes a Dimension object (really just a width and a height).
Was This Post Helpful? 1
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 851
  • View blog
  • Posts: 2,599
  • Joined: 29-July 11

Re: Java window settings

Posted 19 May 2014 - 09:55 AM

Yeah, I meant setSize()
Was This Post Helpful? 0
  • +
  • -

#7 shoup0198  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-February 14

Re: Java window settings

Posted 19 May 2014 - 10:43 AM

Hey thanks guys!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1