Solutions to the Try It Out Exercises in Beginning Programming with Java For Dummies, 6th Edition
by Barry Burd

Chapter 20: Oooey GUI Was a Worm

In this chapter:

Mix it up

The order of the statements is somewhat flexible. Here are two constraints: 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);

Copy text to a text field

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: <?xml version="1.0" encoding="UTF-8"?> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="CopyFieldToField"> <grid id="27dc6" binding="jPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1"> <constraints> <xy x="20" y="20" width="500" height="400"/> </constraints> <properties/> <border type="none"/> <children> <component id="9b1b3" class="javax.swing.JTextField" binding="textField1" default-binding="true"> <constraints/> <properties> <columns value="15"/> </properties> </component> <component id="7d7b0" class="javax.swing.JButton" binding="copyButton" default-binding="true"> <constraints/> <properties> <text value="Copy"/> </properties> </component> <component id="523cc" class="javax.swing.JTextField" binding="textField2" default-binding="true"> <constraints/> <properties> <columns value="15"/> </properties> </component> </children> </grid> </form>

Copy text to a label

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: <?xml version="1.0" encoding="UTF-8"?> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="CopyFieldToLabel"> <grid id="27dc6" binding="jPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1"> <constraints> <xy x="20" y="20" width="500" height="400"/> </constraints> <properties/> <border type="none"/> <children> <component id="a7e99" class="javax.swing.JTextField" binding="textField1" default-binding="true"> <constraints/> <properties> <columns value="15"/> </properties> </component> <component id="5eefc" class="javax.swing.JButton" binding="copyButton" default-binding="true"> <constraints/> <properties> <text value="Copy"/> </properties> </component> <component id="42c03" class="javax.swing.JLabel" binding="jLabel"> <constraints/> <properties> <text value=""/> </properties> </component> </children> </grid> </form>

Copy between text fields

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: <?xml version="1.0" encoding="UTF-8"?> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="CopyBetweenFields"> <grid id="27dc6" binding="jpanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1"> <constraints> <xy x="20" y="20" width="500" height="400"/> </constraints> <properties/> <border type="none"/> <children> <component id="cf0ad" class="javax.swing.JTextField" binding="textField1" default-binding="true"> <constraints/> <properties> <columns value="15"/> </properties> </component> <component id="5e38b" class="javax.swing.JButton" binding="a1stTo2ndButton" default-binding="true"> <constraints/> <properties> <text value="1st to 2nd"/> </properties> </component> <component id="9bdff" class="javax.swing.JButton" binding="a2ndTo1stButton" default-binding="true"> <constraints/> <properties> <text value="2nd to 1st"/> </properties> </component> <component id="90306" class="javax.swing.JTextField" binding="textField2" default-binding="true"> <constraints/> <properties> <columns value="15"/> </properties> </component> </children> </grid> </form>

Create a small calculator

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: <?xml version="1.0" encoding="UTF-8"?> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="Calculator"> <grid id="27dc6" binding="jPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1"> <constraints> <xy x="20" y="20" width="500" height="400"/> </constraints> <properties/> <border type="none"/> <children> <component id="c8f4f" class="javax.swing.JTextField" binding="textField1" default-binding="true"> <constraints/> <properties> <columns value="5"/> </properties> </component> <component id="de936" class="javax.swing.JLabel"> <constraints/> <properties> <text value="+"/> </properties> </component> <component id="f48fd" class="javax.swing.JTextField" binding="textField2" default-binding="true"> <constraints/> <properties> <columns value="5"/> </properties> </component> <component id="a258c" class="javax.swing.JLabel"> <constraints/> <properties> <text value="="/> </properties> </component> <component id="7dba0" class="javax.swing.JLabel" binding="jLabel"> <constraints/> <properties> <text value=""/> </properties> </component> <component id="1dc1e" class="javax.swing.JButton" binding="addButton" default-binding="true"> <constraints/> <properties> <text value="Add"/> </properties> </component> </children> </grid> </form>