/*
File: ButtonPos.java
How to size and position a button
*/
/* ..... Run this applet using the following HTML:
ButtonPos
..... */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class ButtonPos extends Applet {
public void init() {
setLayout(null);
setBackground(Color.lightGray);
setSize(400, 250);
add(aButton);
aButton.setBackground(Color.lightGray);
aButton.setForeground(Color.blue);
aButton.setFont(new Font("Dialog", Font.ITALIC, 12));
aButton.setBounds(nButtonX, nButtonY, nButtonDX, nButtonDY);
aLabel.setText("How to Size and Position a Button");
add(aLabel);
aLabel.setFont(new Font("Dialog", Font.BOLD, 16));
aLabel.setBounds(60, 25, 275, 40);
aButton.addActionListener(new ButtonClicked());
}
public void paint(Graphics g) {
if (bVisible) {
g.drawString("The Button is at "
+ "(" + nButtonX + ","
+ nButtonY + ") size ="
+ nButtonDX + " X "
+ nButtonDY, 70, 200);
}
}
class ButtonClicked implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == aButton) {
bVisible = !bVisible;
if (!bVisible)
aButton.setLabel("Click Me Again!");
if (nButtonX > 10) {
nButtonX -= 5;
nButtonDX += 10;
nButtonY--;
nButtonDY += 2;
}
aButton.setBounds(nButtonX, nButtonY, nButtonDX, nButtonDY);
repaint();
}
}
}
private Button aButton = new Button("Click Me!");
private Label aLabel = new Label();
private boolean bVisible = false;
private int nButtonX = 150;
private int nButtonY = 105;
private int nButtonDX = 85;
private int nButtonDY = 40;
}