Hello Everyone,
I would like to ask some help from you.
I'm Mark from the Philippines.
I'm a newbie programmer and I'm new with java,that's why it's very difficult for me to make a program using java.
Our Teacher had given us an assignment using java,to make a simple chat window within one week. And that day is on friday,but still I'm not finished doing it.
one text field,one text area,three buttons(Send=to send the text in the text area,Clear=to clear out the text in the text area,Cancel=to cancel the text in the text field). .
The instruction is when you type a message in the textfield then press the send button,the message should appear on the textarea but the problem is when I press the Send button,nothing happens. When I press also the buttons Cancel and Clear,still nothing happens.
Here is my code
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ItemEvent.*;
public class cwindow
implements ActionListener, ItemListener
{
private Frame frame;
private MenuBar menbar;
private Menu men1;
private Menu men2;
private Menu men3;
private MenuItem meni1;
private MenuItem meni2;
private MenuItem meni3;
private MenuItem meni4;
private CheckboxMenuItem meni5;
private TextArea txta;
private Button send = new Button("Send");
private Button clear = new Button("Clear");
private Button cancel = new Button("Cancel");
private TextField marky;
public void go()
{
frame = new Frame("chat event");
menbar = new MenuBar();
txta = new TextArea(" ", 4, 20);
send.addActionListener(this);
frame.add(txta, BorderLayout.CENTER);
marky = new TextField(" ", 25);
frame.add(send,BorderLayout.EAST);
marky.addKeyListener( new NameHandler() );
frame.add(marky, BorderLayout.SOUTH);
men1 = new Menu("File");
men2 = new Menu("Edit");
men3 = new Menu("Help");
menbar.add(men1);
menbar.add(men2);
menbar.setHelpMenu(men3);
frame.setMenuBar(menbar);
meni1 = new MenuItem("New");
meni2 = new MenuItem("Save");
meni3 = new MenuItem("Load");
meni4 = new MenuItem("Quit");
meni1.addActionListener(this);
meni2.addActionListener(this);
meni3.addActionListener(this);
meni4.addActionListener(this);
men1.add(meni1);
men1.add(meni2);
men1.add(meni3);
men1.addSeparator();
men1.add(meni4);
meni5 = new CheckboxMenuItem("Persistent");
meni5.addItemListener(this);
men1.add(meni5);
frame.setSize(200,200);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed( ActionEvent ae) {
System.out.println("Button \"" +
ae.getActionCommand() + "\" Pressed.");
if (ae.getActionCommand().equals("Quit")) {
System.exit(0);
}
}
public void itemStateChanged(ItemEvent ie) {
String state = "Deselected";
if (ie.getStateChange() == ItemEvent.SELECTED) {
state = "Selected";
}
System.out.println(ie.getItem() + " " + state);
}
class NameHandler extends KeyAdapter {
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
if ( Character.isDigit(c)) {
e.consume();
}
}
}
public static void main (String args[]) {
cwindow markyMenu = new cwindow();
markyMenu.go();
}
}
Best regards!