import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; /** * playing lightly with modifying alignment * in FlowLayout * * @author Sharon Tuttle * @version 2015-09-29 */ public class FlowLayoutPlay { /** * creates a frame for slight FlowLayout play * * @param args not used here */ public static void main(String[] args) { EventQueue.invokeLater( new Runnable() { public void run() { FlowLayoutPlayFrame aFrame = new FlowLayoutPlayFrame(); aFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); aFrame.setVisible(true); } }); } } /** * a frame with some FlowLayout play */ class FlowLayoutPlayFrame extends JFrame { public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 150; /** * construct a FlowLayoutPlay-panel frame instance */ public FlowLayoutPlayFrame() { this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); this.setTitle("FlowLayout Playing"); // create and add a FlowLayoutPlay panel to my frame FlowLayoutPlayPanel aFlowLayoutPlayPanel = new FlowLayoutPlayPanel(); this.add(aFlowLayoutPlayPanel); } } /** * a panel with FlowLayout experimentation */ class FlowLayoutPlayPanel extends JPanel { // data fields private JTextField visitorGreeting; private JTextField visitorNameField; private JTextField desiredSizeField; /** * constructs a FlowLayoutPlay panel instance */ public FlowLayoutPlayPanel() { // make a FlowLayout instance to use to change // the alignment FlowLayout myFlowLayout = new FlowLayout(); this.setLayout(myFlowLayout); // change which is commented out to see how FlowLayout // behaves differently with different // values of alignment //myFlowLayout.setAlignment(FlowLayout.LEFT); myFlowLayout.setAlignment(FlowLayout.RIGHT); //myFlowLayout.setAlignment(FlowLayout.CENTER); // the default // allow user to enter their name, and see a personalized // greeting when they then click the Submit button JLabel visitorPrompt = new JLabel("Type your name:"); visitorNameField = new JTextField(10); // ...but now, using an uneditable JTextField instead of a // JLabel - so doesn’t resize when contents change, for example visitorGreeting = new JTextField("No visitor has registered yet"); visitorGreeting.setEditable(false); visitorGreeting.setBackground(Color.LIGHT_GRAY); // also giving user a chance to give the greeting’s font size JLabel sizePrompt = new JLabel("Enter font size:"); desiredSizeField = new JTextField("15", 3); JButton submitName = new JButton("Submit"); submitName.addActionListener(new SubmitAction()); this.add(visitorPrompt); this.add(visitorNameField); this.add(visitorGreeting); this.add(submitName); this.add(sizePrompt); this.add(desiredSizeField); } /** * an action listener that changes the greeting * field to reflect the entered user's name, * and its font size to the entered size */ private class SubmitAction implements ActionListener { // default constructor will suffice in this case /** * changes the greeting label to reflect the * entered visitor's name * * @param event a push of the Submit button */ public void actionPerformed(ActionEvent event) { visitorGreeting.setText("Hi, " + visitorNameField.getText() ); // getText returns a String - but if you expect digits // to be entered, can try parsing that String // and converting it to int using Integer.parseInt visitorGreeting.setFont(new Font("Display", Font.PLAIN, Integer.parseInt(desiredSizeField.getText()))); } } }