Sun May 03, 2009 8:52 am
import java.awt.event.*;
import javax.swing.*;
public class TextAreaCtrlEnter extends JFrame implements KeyListener, ActionListener{
JLabel lblDisplay;
JTextArea txtArea;
JButton btnSend;
JPanel panelObject;
public TextAreaCtrlEnter() {
panelObject = new JPanel();
getContentPane().add(panelObject);
lblDisplay = new JLabel("");
txtArea = new JTextArea(5,20);
btnSend = new JButton("Send");
panelObject.add(lblDisplay);
panelObject.add(txtArea);
panelObject.add(btnSend);
setVisible(true);
setSize(250,300);
txtArea.addKeyListener(this);
btnSend.addActionListener(this);
}
//KeyListener Interface
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {
if (e.getSource() == txtArea) {
if ((e.getKeyCode() == KeyEvent.VK_ALT) && (e.getKeyCode() == KeyEvent.VK_ENTER)) {
txtArea.setText(txtArea.getText());
txtArea.setText("\n");
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
lblDisplay.setText(txtArea.getText());
txtArea.setText("");
txtArea.requestFocus();
}
}
}
//ActionListener Interface
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnSend) {
lblDisplay.setText(txtArea.getText());
txtArea.setText("");
txtArea.requestFocus();
}
}
public static void main(String args[]) {
TextAreaCtrlEnter obj = new TextAreaCtrlEnter();
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.