import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GettingText implements ActionListener {
JTextField hi = new JTextField(12);
JTextField lo = new JTextField(12);
JPanel panel;
public void actionPerformed(ActionEvent e) {
String hiText = hi.getText();
String loText = lo.getText();
boolean equal = hiText.equals(loText);
int compare = hiText.compareTo(loText); // see String api
System.out.printf("hiText = %s loText = %s " +
"equal = %b compare = %d%n",
hiText, loText, equal, compare);
CardLayout cards = (CardLayout)panel.getLayout();
cards.next(panel);
// or
// cards.show(panel, "blue");
}
private JPanel getContent() {
CardLayout cards = new CardLayout();
panel = new JPanel(cards);
panel.add("data", getComponentPanel());
panel.add("blue", getBluePanel());
return panel;
}
private JPanel getComponentPanel() {
JButton button = new JButton("jump");
button.addActionListener(this);
// Optionally, you can also add the ActionListener
// to the two textFields which will generate an
// ActionEvent when you press the enter/return key.
hi.addActionListener(this);
lo.addActionListener(this);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,0,5,0);
gbc.weighty = 1.0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(hi, gbc);
panel.add(lo, gbc);
panel.add(button, gbc);
return panel;
}
private JPanel getBluePanel() {
JPanel panel = new JPanel();
panel.setBackground(Color.blue);
return panel;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new GettingText().getContent());
f.setSize(400,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}