OK, this is REALLY basic stuff for you Java gurus, but I'm just learning. Here is the problem as it appears in my book:
<< You can change the size of an applet by using the setSize(int h, int v) method. Write an applet that contains two Buttons, labeled "Big" and "Small." Whenever the user clicks on small, set the applet's dimensions to 200 x 100, and whenever the user clicks on big, set the dimensions to 300 x 200. >>
And this is the code that I have so far:
<<
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleApplet extends Applet implements ActionListener
{
private Button changeBig;
private Button changeSmall;
public void init()
{
changeBig = new Button("Big"
;
changeBig.addActionListener(this);
changeSmall = new Button("Small"
;
changeSmall.addActionListener(this);
add(changeBig);
add(changeSmall);
setSize(300, 200);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == changeBig)
setSize(300,200);
else if (e.getSource() == changeSmall)
setSize(200, 100);
}
} >>
When I run this in Internet Explorer, it gives me a grey box with the two buttons towards the top, but clicking on them does nothing. I don't think the setSize() method is doing anything though, because the initial setSize() under the init() doesn't even work. No matter what I change that value to, the applet is always the default size when it starts up, then pressing the buttons does nothing. What am I doing wrong here?
<< You can change the size of an applet by using the setSize(int h, int v) method. Write an applet that contains two Buttons, labeled "Big" and "Small." Whenever the user clicks on small, set the applet's dimensions to 200 x 100, and whenever the user clicks on big, set the dimensions to 300 x 200. >>
And this is the code that I have so far:
<<
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleApplet extends Applet implements ActionListener
{
private Button changeBig;
private Button changeSmall;
public void init()
{
changeBig = new Button("Big"
changeBig.addActionListener(this);
changeSmall = new Button("Small"
changeSmall.addActionListener(this);
add(changeBig);
add(changeSmall);
setSize(300, 200);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == changeBig)
setSize(300,200);
else if (e.getSource() == changeSmall)
setSize(200, 100);
}
} >>
When I run this in Internet Explorer, it gives me a grey box with the two buttons towards the top, but clicking on them does nothing. I don't think the setSize() method is doing anything though, because the initial setSize() under the init() doesn't even work. No matter what I change that value to, the applet is always the default size when it starts up, then pressing the buttons does nothing. What am I doing wrong here?