Results 1 to 8 of 8
  1. #1
    dawp is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Unhappy [Help] setSize work wrong ???

    I'm trying to make a tiny application : click into a button and JFrame will resize

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) 
    {
         setSize(500,[COLOR="DarkRed"]1000[/COLOR] ); /// apply for Frame
         System.out.println("width" + getWidth());
         System.out.println("height " + getHeight());
         
    }
    The result is : width 500 height 920 :confused: .
    I cannot understand why i setHeight 1000 but result is 920 . Anybody can help me ???

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I usually use setPreferredSize(new Dimension(x, y)) and I usually call it on something other than the JFrame such as the JFrame's contentPane. I then call pack() on the JFrame to get it to listen to my new preferred size.

  3. #3
    dawp is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    I tried
    Java Code:
    setPreferredSize(new Dimension(500, 1000));
    pack();
    But result didn't change : height 920 :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It looks as if you called preferred size on the jframe, not its content pane. Truthfully, it's hard to tell what you're doing without seeing a small compilable program that demonstrates your problem, a minimal working example or SSCCE. I suggest you consider creating this and posting this.

  5. #5
    dawp is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    Here is it . It was creat by NeatBean IDE

    Java Code:
    import java.awt.Dimension;
    
    public class MainFrame extends javax.swing.JFrame {
    
        public MainFrame() {
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosed(java.awt.event.WindowEvent evt) {
                    formWindowClosed(evt);
                }
            });
    
            jButton1.setText("New");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(129, 129, 129)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(148, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(118, 118, 118)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(105, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
    
       [COLOR="Olive"] private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                /* 
                setSize(500,1000);
                System.out.println(getHeight()); */
    
                setPreferredSize(new Dimension(500, 1000));
                pack();
                System.out.println(getHeight());
    
        }   [/COLOR]                                     
    
        
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new MainFrame().setVisible(true);
                }
            });
        }
    SecondFrame a;
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        // End of variables declaration                   
    
    }
    So strange bc when i set Height small it work perfect , but when it large (out of screen ) it work wrong :(

  6. #6
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    but when it large (out of screen ) it work wrong
    Well, maybe that is a clue, it doesn't make sense to make the frame larger than the screen.

    My screen resolution is 1024 x 768 and the size seems to max out a 1036 x 780, so it seems to allow 12 extra pixels. I don't know what the significance of that is.

  7. #7
    dawp is offline Member
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    Maybe that's correct clue. Thanks Fubarable & camickr

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

Similar Threads

  1. what is wrong?
    By pinguxxx in forum Advanced Java
    Replies: 3
    Last Post: 07-15-2009, 12:33 PM
  2. What am I doing wrong??
    By NoNickName in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 11:04 PM
  3. what am i doing wrong here??
    By tornbacchus in forum New To Java
    Replies: 19
    Last Post: 04-16-2009, 03:54 AM
  4. what's wrong?
    By rayda in forum New To Java
    Replies: 3
    Last Post: 04-14-2009, 09:07 PM
  5. what wrong
    By pro85 in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 01:07 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
  •