Credits

Saturday, February 13, 2010

Java's Text Components

An interactive Java  would require a text input in single or  multiple lines. Java has text fields and text areas where inputs of all sorts can be used.


A text field  can be defined using JTextField and a text area under JTextArea classes. Check the sample program below that uses text fields, text areas, labels and buttons. 

[+/-] show/hide



 
/*adding of components to frame without a panel*/
import java.awt.FlowLayout;
import javax.swing.*;
public class GUIQuiz2 extends JFrame{
// private LayoutManager FlowLayout;
public GUIQuiz2(){
super("Message Form");
setSize(220,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
JLabel toLabel= new JLabel("To: ",SwingConstants.LEFT);
JLabel fromLabel= new JLabel ("From:",SwingConstants.LEFT);
JLabel messageLabel = new JLabel (" Message:",JLabel.LEFT);
JTextField toField=new JTextField("Receiver's name",15);
JTextField fromField = new JTextField("Sender's name",15);
JTextArea msgArea= new JTextArea("Write your message here.",4,15);
msgArea.setLineWrap(true);
msgArea.setWrapStyleWord(true);
JScrollPane scrollpane=new JScrollPane(msgArea);
FlowLayout flo=new FlowLayout();
    
setLayout(flo);
add(toLabel);
add(toField);
add(fromLabel);
add(fromField);
add(messageLabel);
add(scrollpane);
    
setVisible(true);
}
public static void main (String args[]){
GUIQuiz2 myFrame=new GUIQuiz2();
}
}

AddThis