Chapter 20: Oooey GUI Was a Worm
In this chapter:
The order of the statements is somewhat flexible. Here are two constraints:
- In the
main
method, a variable must be declared before it's used.
- You must add everything that you're going to add to the frame before you call the frame's
pack
method.
So, for example, you can't use the frame
variable before you've declared the frame
variable:
// Bad code:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame frame = new JFrame();
You shouldn't call the frame
object's pack
method before you've added the label to the object:
// Bad code:
frame.pack();
frame.add(label);
Here's the Java code:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CopyFieldToField {
public CopyFieldToField() {
copyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField2.setText(textField1.getText());
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("CopyFieldToField");
frame.setContentPane(new CopyFieldToField().jPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
private JPanel jPanel;
private JTextField textField1;
private JButton copyButton;
private JTextField textField2;
}
Here's the .form
file:
Here's the Java code:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CopyFieldToLabel {
public CopyFieldToLabel() {
copyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jLabel.setText(textField1.getText());
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("CopyFieldToLabel");
frame.setContentPane(new CopyFieldToLabel().jPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
private JPanel jPanel;
private JTextField textField1;
private JButton copyButton;
private JLabel jLabel;
}
Here's the .form
file:
Here's the Java code:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CopyBetweenFields {
private JPanel jpanel;
public CopyBetweenFields() {
a1stTo2ndButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField2.setText(textField1.getText());
}
});
a2ndTo1stButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField1.setText(textField2.getText());
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("CopyBetweenFields");
frame.setContentPane(new CopyBetweenFields().jpanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
private JTextField textField1;
private JButton a1stTo2ndButton;
private JButton a2ndTo1stButton;
private JTextField textField2;
}
Here's the .form
file:
Here's the Java code:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator {
private JPanel jPanel;
public Calculator() {
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int number1 = Integer.parseInt(textField1.getText());
int number2 = Integer.parseInt(textField2.getText());
jLabel.setText(Integer.toString(number1 + number2));
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
frame.setContentPane(new Calculator().jPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setVisible(true);
}
private JTextField textField1;
private JTextField textField2;
private JButton addButton;
private JLabel jLabel;
}
Here's the .form
file: