1

This is my second school assignment that I need to complete. Below is the assignment info.

Create a frame with two buttons, called Expand and Shrink. When the Expand button is clicked, the frame expands by 10 percent. When the Shrink button is clicked, the frame shrinks by 10 percent. Do this in the actionPerformed() method by using setSize(). Keep track of the current size of the frame with two instance variables of type int. When you increase them or decrease them by 10 percent, you will have to use integer arithmetic or use a type cast.

I already had the frame and the buttons set up. Of course from online research, but I just cant get it right. Ex. The inside frame shrink every time I click the shrink buttons, also the expanded did the same (expanded inside the frame). Please take a look at the code. By the way, please show me a better way to create the frame and the buttons with less 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)
{  
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 );
}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1725732
  • 113
  • 2
  • 2
  • 12
  • 1
    Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Mar 19 '13 at 04:09
  • Yeah, I agree with MadProg below . Overall, the line `getContentPane().setSize(w.intValue(),l.intValue());` has no effect on changing button sizes . It's getting the whole thing, nnot the buttons – Caffeinated Mar 19 '13 at 04:18
  • See this for tips also - http://stackoverflow.com/questions/218155/how-do-i-change-jpanel-inside-a-jframe-on-the-fly – Caffeinated Mar 19 '13 at 04:18

2 Answers2

2
  1. I don't know why you would need use Double for this, double will be more then fine.
  2. Create your frame within the context of the Event Dispatching Thread (Take a look at Initial Threads for details
  3. Changing the size of the content pane will have no effect, you actually need to change the size of the frame itself
  4. Since you extending from JFrame anyway, you should endevour to initializes the frame within the constructor (set it's initial size for example)

You main method should look more like...

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }

            Expandshrink frm = new Expandshrink("Expand & Shrink");
        }
    });
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • MadProgrammer, I am not understand the code sample code that you provide. This is a far away from my knowledge. But thanks for helping. – user1725732 Mar 19 '13 at 04:32
  • 1
    Make it knowledge. This is very important. Failure to understand why/how this little piece of code works will run you into deep water very quickly – MadProgrammer Mar 19 '13 at 04:35
  • Sure, every comment and example on stackoberflow.com are valuable. – user1725732 Mar 19 '13 at 05:08
2

Change:

getContentPane().setSize(w.intValue(),l.intValue());

to:

// layout managers are more likely to honor the preferred size
//getContentPane().setSize(w.intValue(),l.intValue());
getContentPane().setPreferredSize(new Dimension(w.intValue(),l.intValue()));
Expandshrink.this.pack();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433