TopButtonPanel.java
Appearance
Back to Into Java - Part XVI
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class TopButtonPanel extends JPanel
implements ActionListener {
private PaintBox owner;
private JButton lineButton;
private JButton rectButton;
private JButton ovalButton;
private JButton filledButton;
private JLabel shape;
private boolean filled = true;
public TopButtonPanel (PaintBox o) {
owner = o;
setLayout(new GridLayout(1, 5));
lineButton = new JButton("Line");
lineButton.addActionListener(this);
add(lineButton);
rectButton = new JButton("Rectangle");
rectButton.addActionListener(this);
add(rectButton);
ovalButton = new JButton("Oval");
ovalButton.addActionListener(this);
add(ovalButton);
filledButton = new JButton("Filled");
filledButton.addActionListener(this);
add(filledButton);
shape = new JLabel(" Rectangle");
shape.setBackground(Color.white);
shape.setForeground(Color.black);
shape.setOpaque(true);
shape.setBorder(BorderFactory.createBevelBorder(
BevelBorder.LOWERED));
add(shape);
}
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == filledButton) {
if (filled) { // filled true
filled = false;
owner.setFilled(filled);
filledButton.setText("Not filled");
} else {
filled = true;
owner.setFilled(filled);
filledButton.setText("Filled");
}
/* constants found in PaintPanel are used*/
} else if (obj == lineButton) {
owner.setShape(PaintPanel.LINE);
shape.setText(" Line");
} else if (obj == rectButton) {
owner.setShape(PaintPanel.RECTANGLE);
shape.setText(" Rectangle");
} else { // must be ovalButton
owner.setShape(PaintPanel.OVAL);
shape.setText(" Oval");
}
}
}