Results 1 to 8 of 8
  1. #1
    impact is offline Member
    Join Date
    May 2008
    Posts
    18
    Rep Power
    0

    Default [SOLVED] How to set the frame size?

    Hi,
    Can some one show me how to set the frame size in this program? History: I have created a window and added a button but its to small. So I want to increase the size of the frame to at least 600X400 pixel.

    Java Code:
    package test;
    
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    
    
    public class Main extends JPanel
                            implements ActionListener {
        protected JButton b1;
    
        public Main() {
           
            b1 = new JButton("Disable middle button");
            b1.setVerticalTextPosition(AbstractButton.CENTER);
            b1.setHorizontalTextPosition(AbstractButton.LEADING); //aka LEFT, for left-to-right locales
            b1.setMnemonic(KeyEvent.VK_D);
            b1.setActionCommand("disable");
    
    
            //Listen for actions on buttons 1 and 3.
            b1.addActionListener(this);
           
    
            b1.setToolTipText("Go");
            
    
            //Add Components to this container, using the default FlowLayout.
            add(b1);
           
        }
    
        public void actionPerformed(ActionEvent e) {
            if ("disable".equals(e.getActionCommand())) {
                
                b1.setEnabled(true);
                
            } else {
               
                b1.setEnabled(true);
                
            }
        }
    
        /** Returns an ImageIcon, or null if the path was invalid. */
      
    
        /**
         * Create the GUI and show it.  For thread safety, 
         * this method should be invoked from the 
         * event-dispatching thread.
         */
        [B]private static void createAndShowGUI()[/B] {
    
            //Create and set up the window.
            JFrame frame = new JFrame("ButtonDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            Main newContentPane = new Main();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI(); 
                }
            });
        }
    }
    I hope you understand this code,I could not find a better code tag to insert code into my post. Please give me some example code to do this as I am new to java.

    Thank you very much

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Hi,

    Welcome to our community. :)

    Since you use a different method to make GUI, set the preferred size there.

    Java Code:
        private static void createAndShowGUI() {
    
            //Create and set up the window.
            JFrame frame = new JFrame("ButtonDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            Main newContentPane = new Main();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            frame.setPreferredSize(new Dimension(300, 300));
    
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }

  3. #3
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    use YOUR_FRAME_NAME.setSize(500,480); method
    sanjeev,संजीव

  4. #4
    impact is offline Member
    Join Date
    May 2008
    Posts
    18
    Rep Power
    0

    Default

    Aha, that is it all?

    Thanks bunch.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by impact View Post
    Aha, that is it all?

    Thanks bunch.
    Hey Pal,

    Mark the Thread SOLVED If you got the answer.
    sanjeev,संजीव

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    impact is offline Member
    Join Date
    May 2008
    Posts
    18
    Rep Power
    0

Similar Threads

  1. close a frame..
    By tajinvillage in forum New To Java
    Replies: 5
    Last Post: 04-27-2008, 11:22 PM
  2. Custom icon for Frame
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 09:38 AM
  3. Resize frame
    By lenny in forum AWT / Swing
    Replies: 1
    Last Post: 07-30-2007, 12:18 AM
  4. Frame Query
    By Daniel in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 07:27 PM
  5. Frame problems
    By gary in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2007, 02:21 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
  •