import javax.swing.JApplet; import java.awt.Dimension; import java.awt.Toolkit; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.Container; import java.awt.Color; import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.BorderFactory; import javax.swing.border.Border; public class TextTest extends JApplet{ public static void main(String[] args) { theApp = new TextTest(); // Create the application object theApp.init(); // ...and initialize it } public void init() { window = new JFrame("Defy Convention"); // Create the app window Toolkit theKit = window.getToolkit(); // Get the window toolkit Dimension wndSize = theKit.getScreenSize(); // Get screen size // Set the position to screen center & size to half screen size window.setBounds(wndSize.width/4, wndSize.height/4, // Position wndSize.width/2, wndSize.height/2); // Size GridBagLayout gridbag = new GridBagLayout(); // Create a layout manager GridBagConstraints constraints = new GridBagConstraints(); Container content = window.getContentPane(); // Get the content pane content.setLayout(gridbag); // Set the container layout mgr // Set color of content pane Color customColor = new Color(25,100,75); content.setBackground(customColor); window.addWindowListener(new WindowHandler()); // Add window listener FieldHandler fh = new FieldHandler(); // one FieldHandler for two sources // Add two text boxes // Set constraints and add first box constraints.weightx = constraints.weighty = 10.0; constraints.fill = constraints.NONE; constraints.ipadx = 30; // Pad 30 in x constraints.ipady = 10; // Pad 10 in y field1 = addBox(" Hi ", constraints, gridbag); // Add the box field1.addActionListener(fh); field1.addFocusListener(new FocusHandler()); // Set constraints and add second button constraints.weightx = 5.0; // Weight half of first constraints.fill = constraints.BOTH; // Expand to fill space constraints.ipadx = constraints.ipady = 0; // No padding constraints.insets = new java.awt.Insets(10, 30, 10, 20); // Left 30 & right 20 constraints.gridwidth = constraints.RELATIVE; // Rest of the row constraints.gridheight = 2; // Height 2x field2 = addBox(" There ", constraints, gridbag); // Add the box field2.addActionListener(fh); field2.addMouseListener(new MouseHandler()); // Set constraints and add third button constraints.gridx = 0; // Begin new row constraints.fill = constraints.NONE; constraints.ipadx = 30; // Pad component in x constraints.ipady = 10; // Pad component in y constraints.insets = new java.awt.Insets(0,0,0,0); // No insets constraints.gridwidth = 1; // Width constraints.gridheight = 1; // Height addBox(" Not Used ", constraints, gridbag); // Add the box window.setVisible(true); } static JTextField addBox(String label, GridBagConstraints constraints, GridBagLayout layout) { // Create a Border object using a BorderFactory method Border edge = BorderFactory.createRaisedBevelBorder(); JTextField box = new JTextField(label); // Create a box box.setBorder(edge); // Add its border box.setBackground(Color.YELLOW); layout.setConstraints(box, constraints); // Set the constraints window.getContentPane().add(box); // Add box to content pane return box; } // FieldHandler extracts text from text field and prints it // Try it by entering text into the box and hitting enter. class FieldHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if(theApp != null) { Object source = e.getSource(); String mess = ((JTextField)source).getText(); System.out.println(mess); } } } // MouseHandler changes color of source when mouse enters its area, // changes color back to WHITE and copies text to first text field // when mouse leaves class MouseHandler extends MouseAdapter { public void mouseEntered(MouseEvent e) { ((JTextField)e.getSource()).setBackground(Color.YELLOW); } public void mouseExited(MouseEvent e) { field1.setText(((JTextField)e.getSource()).getText()); ((JTextField)e.getSource()).setBackground(Color.WHITE); } } // FocusHandler changes color of source when it gains focus class FocusHandler implements FocusListener { public void focusGained(FocusEvent e) { ((JTextField)e.getSource()).setBackground(Color.CYAN); } public void focusLost(FocusEvent e) { ((JTextField)e.getSource()).setBackground(Color.WHITE); } } // Handler class for window events class WindowHandler extends WindowAdapter { // Handler for window closing event public void windowClosing(WindowEvent e) { window.dispose(); // Release the window resources System.exit(0); // End the application } } private static JFrame window; // The application window private static TextTest theApp; // The application object private static JTextField field1, field2; }