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 );
}
}