1

I wrote a program for an applet that is supposed to display different text in a text box when you push a button. My program has no errors when I compiled it, but the text box doesn't display correctly. I don't know what is wrong with it. Here is my code

import java.awt.*;
import java.awt.event.*;

public class colors{
Button button1;
Button button2;
Button button3;
Label label1;
TextField objTextField1; 

public static void main (String args[]){
colors c = new colors();
}
public colors() {
Frame f = new Frame ("Colors");
Button button1 = new Button("Blue");
button1.setBounds(10,305,120,75);
button1.addMouseListener(new MyMouseListener1());
Button button2 = new Button("Red");
button2.setBounds(140,305,120,75);
button2.addMouseListener(new MyMouseListener2());
Button button3 = new Button("Yellow");
button3.setBounds(270,305,120,75);
button3.addMouseListener(new MyMouseListener3());

f.add(button1);
f.add(button2);
f.add(button3);

label1 = new Label("Click a Button to Reveal Text");
label1.setBounds(20,105,200,25);
f.add(label1);


objTextField1 = new TextField("Which Color?", 15);
objTextField1.setBounds(20,75,125,50);
f.add(objTextField1);

f.add(label1);
f.add(objTextField1);

f.addWindowListener(new WindowAdapter()
{
    public void windowClosing(WindowEvent we){
        System.exit(0);
    }
});
f.setSize(400,400);
f.setVisible(true);
}
public class MyMouseListener1 extends MouseAdapter{
        public void mouseClicked(MouseEvent me){
            objTextField1.setText("Blue");
        }
    }
public class MyMouseListener2 extends MouseAdapter{
        public void mouseClicked(MouseEvent me){
            objTextField1.setText("Red");
        }
}

public class MyMouseListener3 extends MouseAdapter{
    public void mouseClicked(MouseEvent me){
        objTextField1.setText("Yellow");
        }
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Alyssa
  • 27
  • 1
  • 5
  • 1
    You should be using [Swing components](http://docs.oracle.com/javase/tutorial/uiswing/components/index.html) instead of AWT components – Paul Samsotha Jul 27 '14 at 04:44
  • I tested your code and it's working as expected. ?*the text box doesn't display correctly* what do you mean by this line? – Braj Jul 27 '14 at 05:03

3 Answers3

2

When a Button is clicked it fires an ActionEvent.

You should use an ActionListener instead of a MouseListener.

public void actionPerformed(ActionEvent e) { 
    ...//code that reacts to the action... 
}

AND don't forget to add

button.addActionListener(instance);
akash
  • 22,664
  • 11
  • 59
  • 87
1

I have tested your code and it's working as expected but I have noticed some of the points in your code as mentioned below:

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
1

To make an applet you should extend javax.swing.JApplet class & override init() method.

To change the color, you must write your logics in actionPerformed() of ActionListener. But it's an interface. So, you can make use of Anonymous Inner class & implement actionPerformed() in it.

So, when you call addActionListener() on a JButton, I recommend you to do that by using Anonymous Inner class. It would be more clear through following code.

My Suggestion: Whenever you write code, always keep OOD principles in your mind. This isn't right place to discuss that, but your code has a Code smell which is Duplication in code.

Below is the best way to do what you want & we're also using DRY Principle.

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

public class ColorChanger extends javax.swing.JApplet {

    private JPanel mainPanel;

    private JButton btnRed;
    private JButton btnGreen;
    private JButton btnBlue;

    @Override
    public void init() {
        super.init();
        mainPanel = new JPanel();

        btnRed = new JButton("Red");
        btnGreen = new JButton("Green");
        btnBlue = new JButton("Blue");

        this.add(mainPanel);
        mainPanel.add(btnRed);
        mainPanel.add(btnGreen);
        mainPanel.add(btnBlue);

        bindActionEvent(btnRed, Color.RED);
        bindActionEvent(btnGreen, Color.GREEN);
        bindActionEvent(btnBlue, Color.BLUE);

    }

    private void bindActionEvent(JButton b1, Color color) {

        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                mainPanel.setBackground(color);
                //Write setText() for your TextField here.
            }
        });
    }       //END Of Helper Method
}
Adarsh Singhal
  • 362
  • 1
  • 3
  • 12