TextDemo.java import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class TextDemo implements ActionListener { private JFrame frame; private JTextField tf1, tf2, tf3; private JTextArea ta; private JLabel label, label2; private JButton b; public TextDemo() { frame = new JFrame("A Simple Swing App"); //frame.setSize(600, 400); Toolkit tk = frame.getToolkit(); Dimension dim = tk.getScreenSize(); int width = (int)dim.getWidth(); int height = (int)dim.getHeight(); frame.setSize(width, height); frame.setLayout(new FlowLayout()); tf1 = new JTextField("Enter the name", 25); tf2 = new JTextField(20); tf3 = new JTextField("Enter a value"); tf1.setFont(new Font("Verdana", Font.BOLD, 18)); tf2.setFont(new Font("Verdana", Font.BOLD, 18)); tf3.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(tf1); frame.add(tf2); frame.add(tf3); //tf1.addActionListener(this); tf2.addActionListener(this); tf3.addActionListener(this); ta = new JTextArea(20, 15); ta.setFont(new Font("Verdana", Font.BOLD, 18)); frame.add(ta); label = new JLabel(); label.setFont(new Font("Verdana", Font.BOLD, 18)); label.setForeground(Color.RED); frame.add(label); label2 = new JLabel(); label2.setFont(new Font("Verdana", Font.BOLD, 18)); label2.setForeground(Color.green); frame.add(label2); b = new JButton("Display"); b.addActionListener(this); frame.add(b); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public void actionPerformed(ActionEvent ae) { String message =""; message += tf1.getText()+": "; message += tf2.getText()+": "; message += tf3.getText()+": "; label.setText(message); label2.setText(ta.getText()); } public static void main(String[] args) { new TextDemo(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter