Results 1 to 2 of 2
  1. #1
    umaza is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Expand & Shrink Buttons

    I need to write a program with an expand & shrink button, the purpose of these buttons is to expand or shrink the size of the frame by 10%.
    How can I some how do JFrame.setSize() within the actionPerformed() method? This code just expands & shrinks the inside of the frame, not the actual frame.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ExpandShrink extends JFrame implements ActionListener
    {
      JButton expand;
      JButton shrink;
      Double w = 300.0, l = 200.0;
      
      ExpandShrink(String title) 
      {
        super(title);                          
        
        expand = new JButton("Expand");
        shrink = new JButton("Shrink");
        
        expand.setActionCommand("expand");
        shrink.setActionCommand("shrink");
        
        expand.addActionListener(this);
        shrink.addActionListener(this);
        
        setLayout(new FlowLayout());
        
        add(expand);
        add(shrink);
        
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
      }
      
      public void actionPerformed(ActionEvent evt)
      {
        
        try
        {
          if (evt.getActionCommand().equals("expand"))
          {
            w = w*1.1;
            l = l*1.1;
          }
          else if (evt.getActionCommand().equals("shrink"))
          {
            w = w*0.9;
            l = l*0.9;
          }
          getContentPane().setSize(w.intValue(),l.intValue());
        }
        catch ( Exception ex )
        {  
        }  
      }
      
      public static void main ( String[] args )
      {
        ExpandShrink frm = new ExpandShrink("Expand & Shrink");
        
        frm.setSize( 300, 200 );     
        frm.setVisible( true );
      }
    }
    Last edited by umaza; 10-31-2012 at 03:19 AM.

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

    Default Re: Expand & Shrink Buttons

    I suggest:

    • Set the preferredSize always, not the size.
    • Do this on the JFrame, not it's contentPane (or visa versa, but be *consistent* throughout your program).
    • Call pack() before setVisible(true)
    • Call pack() after changing the preferredSize of the JFrame.

Similar Threads

  1. Compress/Expand data visualization
    By susieferrari in forum JavaFX
    Replies: 4
    Last Post: 07-23-2012, 10:55 AM
  2. Replies: 9
    Last Post: 09-11-2011, 05:27 PM
  3. How to expand the JPanels dynamically?...
    By Vin in forum New To Java
    Replies: 6
    Last Post: 01-27-2010, 12:58 PM
  4. shrink and expand nemu when user click
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-06-2009, 07:02 AM
  5. SwT Expand items problem in Solaris.
    By Sureshgurram in forum SWT / JFace
    Replies: 0
    Last Post: 01-27-2009, 10:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •