package test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import com.l2fprod.common.swing.JButtonBar;
public class ButtonBarExample extends JPanel implements ActionListener {
private JButtonBar buttonBar;
private JToggleButton button1, button2, button3;
private JPanel currentPanel;
public ButtonBarExample(JFrame frame) {
frame.setTitle("ButtonBar Example");
this.setLayout(new BorderLayout());
buttonBar = new JButtonBar(JButtonBar.VERTICAL);
buttonBar.setBackground(Color.ORANGE);
buttonBar.setBorder(BorderFactory.createRaisedBevelBorder());
buttonBar.add(button1 = new JToggleButton("Quadrat", new Icon1()));
buttonBar.add(button2 = new JToggleButton("Kreis", new Icon2()));
buttonBar.add(button3 = new JToggleButton("Halbkreis", new Icon3()));
this.add(buttonBar, "West");
this.add(currentPanel = new RedPanel(), "Center");
ButtonGroup bg = new ButtonGroup();
bg.add(button1);
bg.add(button2);
bg.add(button3);
button1.setSelected(true);
button1.setActionCommand("b1");
button2.setActionCommand("b2");
button3.setActionCommand("b3");
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setAlwaysOnTop(true);
frame.setLocationRelativeTo(null);
frame.add(new ButtonBarExample(frame));
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
if (s.equals("b1")) {
this.remove(currentPanel);
this.add(currentPanel = new RedPanel(), "Center");
this.updateUI();
} else if (s.equals("b2")) {
this.remove(currentPanel);
this.add(currentPanel = new BluePanel(), "Center");
this.updateUI();
} else {
this.remove(currentPanel);
this.add(currentPanel = new PinkPanel(), "Center");
this.updateUI();
}
}
class RedPanel extends JPanel {
public RedPanel() {
this.setBackground(Color.RED);
this.setPreferredSize(new Dimension(300, 300));
}
}
class BluePanel extends JPanel {
public BluePanel() {
this.setBackground(Color.BLUE);
this.setPreferredSize(new Dimension(300, 300));
}
}
class PinkPanel extends JPanel {
public PinkPanel() {
this.setBackground(Color.PINK);
this.setPreferredSize(new Dimension(300, 300));
}
}
class Icon1 implements Icon {
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.RED);
g.fillRect(x, y, getIconWidth(), getIconHeight());
}
public int getIconWidth() {
return 48;
}
public int getIconHeight() {
return 48;
}
}
class Icon2 implements Icon {
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.BLUE);
g.fillOval(x, y, getIconWidth(), getIconHeight());
}
public int getIconWidth() {
return 48;
}
public int getIconHeight() {
return 48;
}
}
class Icon3 implements Icon {
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(Color.PINK);
g.fillArc(x, y, getIconWidth(), getIconHeight(), 45, 180);
}
public int getIconWidth() {
return 48;
}
public int getIconHeight() {
return 48;
}
}
}