Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HelloComponent3 extends JComponent
- implements MouseMotionListener, ActionListener
- {
- int messageX = 75, messageY= 175;
- String theMessage;
- String redString = "red", blueString = "blue", greenString = "green";
- String magentaString = "magenta", blackString = "black", resetString = "reset";
- JButton resetButton;
- JRadioButton redButton, blueButton, greenButton, magentaButton, blackButton;
- ButtonGroup colorButtons;
- public HelloComponent3(String message) {
- theMessage = message;
- //intialize the reset button
- resetButton = new JButton("Reset");
- resetButton.setActionCommand(resetString);
- resetButton.addActionListener(this);
- //intialize our radio buttons with actions and labels
- redButton = new JRadioButton("Red");
- redButton.setActionCommand(redString);
- ...
- redButton.addActionListener(this);
- blueButton.addActionListener(this);
- ...
- public void actionPerformed(ActionEvent e){
- if (e.getActionCommand().equals(resetString)) {
- messageX = 75; messageY = 175;
- setForeground(Color.black);
- blackButton.setSelected(true);
- repaint();
- return;
- }
- if ( e.getActionCommand().equals(redString) ) {
- setForeground(Color.red);
- repaint();
- return;
- }
- if ( e.getActionCommand().equals(blueString) ) {
- setForeground(Color.blue);
- repaint();
- return;
- }
- if ( e.getActionCommand().equals(greenString) ) {
- setForeground(Color.green);
- repaint();
- return;
- }
- if ( e.getActionCommand().equals(magentaString) ) {
- setForeground(Color.magenta);
- repaint();
- return;
- }
- if ( e.getActionCommand().equals(blackString) ) {
- setForeground(Color.black);
- repaint();
- return;
- }
- }
- resetButton.addActionListener(this);
- resetButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- resetButtonActionPerformed(evt);
- }
- });
- public void resetButtonActionPerformed(ActionEvent evt) {
- messageX = 75; messageY = 175;
- setForeground(Color.black);
- blackButton.setSelected(true);
- repaint();
- }
- resetButton = new JButton(new ResetAction());
- redButton = new JButton(new ColorAction("Red", Color.red));
- private class ResetAction extends AbstractAction {
- public ResetAction() {
- super("Reset");
- }
- public void actionPerformed(ActionEvent e) {
- messageX = 75; messageY = 175;
- setForeground(Color.black);
- blackButton.setSelected(true);
- repaint();
- }
- }
- private class ResetAction extends AbstractAction {
- private Color color;
- public ColorAction(String title, Color color) {
- super(title);
- this.color = color;
- }
- public void actionPerformed(ActionEvent e) {
- setForeground(color);
- repaint();
- }
- }
- addColorButton("Green" , Color.GREEN );
- addColorButton("Red" , Color.RED );
- addColorButton("Yellow", Color.YELLOW);
- addColorButton("Blue" , Color.BLUE );
- ...
- private void addColorButton(String label, Color color) {
- JRadioButton button = new JRadioButton(label);
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent event) {
- target.setForeground(color);
- target.repaint();
- }
- });
- colorGroup.add(button);
- panel.add(button);
- }
Add Comment
Please, Sign In to add comment