Java Reference
In-Depth Information
Display 17.17
A Text Field (part 2 of 2)
38
JPanel buttonPanel = new JPanel();
39
buttonPanel.setLayout( new FlowLayout());
40
buttonPanel.setBackground(Color.GREEN);
41
JButton actionButton = new JButton("Click me");
42
actionButton.addActionListener( this );
43
buttonPanel.add(actionButton);
44
JButton clearButton = new JButton("Clear");
45
clearButton.addActionListener( this );
46
buttonPanel.add(clearButton);
47
add(buttonPanel);
48
}
49
public void actionPerformed(ActionEvent e)
50
{
51
String actionCommand = e.getActionCommand();
52
if (actionCommand.equals("Click me"))
53
name.setText("Hello " + name.getText());
54
else if (actionCommand.equals("Clear"))
55
name.setText("");
56
else
57
name.setText("Unexpected error.");
58
}
59
}
This sets the text field equal to the empty string,
which makes it blank.
R ESULTING GUI (When program is started and a name entered)
R ESULTING GUI (After clicking the "Click me" button)
Search WWH ::




Custom Search