import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * A GUI application that uses several sub-panels * using different layout managers * * @author Sharon Tuttle * @version 2015-10-07 */ public class LayoutTrio { /** * creates a LayoutTrioFrame * * @param args not used here */ public static void main(String args[]) { EventQueue.invokeLater( new Runnable() { public void run() { LayoutTrioFrame mainFrame = new LayoutTrioFrame(); mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); mainFrame.setVisible(true); } }); } } /** * A frame with a panel and subpanels, with several layout * managers in use */ class LayoutTrioFrame extends JFrame { // data fields private final static int DEFAULT_WIDTH = 400; private final static int DEFAULT_HEIGHT = 250; /** * constructs a LayoutTrioFrame instance */ public LayoutTrioFrame() { this.setTitle("LayoutTrio"); this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); // add LayoutTrio panel to frame LayoutTrioPanel panel = new LayoutTrioPanel(); this.add(panel); } } /** * A panel containing subpanels, using several different * layout managers * */ class LayoutTrioPanel extends JPanel { // data fields for LayoutTrioPanel private final static Font DISPLAY_FONT = new Font("Dialog", Font.PLAIN, 20); private int runningTotal; private JTextField resultsField; /** * constructs a LayoutTrio panel instance */ public LayoutTrioPanel() { runningTotal = 0; // make this "master" panel use BorderLayout this.setLayout(new BorderLayout()); // we'll have a welcome label on a sub-panel // in the north // stub to start: //JLabel northStub = new JLabel("NORTH STUB"); //this.add(northStub, BorderLayout.NORTH); JPanel welcomePanel = makeMsgPanel( "Welcome to 3-layouts-at-once DEMO!"); this.add(welcomePanel, BorderLayout.NORTH); // then I'd like number-buttons-and-results // sub-panel in the center //JLabel centerStub = new JLabel("CENTER STUB"); //this.add(centerStub, BorderLayout.CENTER); JPanel actionPanel = makeActionPanel(); this.add(actionPanel, BorderLayout.CENTER); // and, we'll have a clear button in a subpanel // in the south //JLabel southStub = new JLabel("SOUTH STUB"); //this.add(southStub, BorderLayout.SOUTH); JPanel clearPanel = makeClearPanel(); this.add(clearPanel, BorderLayout.SOUTH); } /** * create a small sub-panel with a centered message-label * * @param msg the desired text for the label * @return a panel with a centered label with the specified text */ private JPanel makeMsgPanel(String msg) { JPanel msgPanel = new JPanel(); JLabel msgLabel = new JLabel(msg); msgLabel.setFont(DISPLAY_FONT); msgLabel.setForeground(Color.BLUE); msgPanel.add(msgLabel); return msgPanel; } /** * make a sub-panel with 12 numbered buttons * in its center, and a results label and textfield * in its south * * @return a number-buttons-plus-results panel */ private JPanel makeActionPanel() { JPanel newActionPanel = new JPanel(); newActionPanel.setLayout(new BorderLayout()); // set up a sub-panel to hold the number-buttons JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(4, 3)); // set up an array of active number-buttons JButton[] buttonArray = new JButton[12]; for (int i=0; i < buttonArray.length; i++) { buttonArray[i] = new JButton("" + (i+1)); buttonArray[i].setFont(DISPLAY_FONT); buttonArray[i].addActionListener(new NumButtonAction()); buttonPanel.add(buttonArray[i]); } newActionPanel.add(buttonPanel, BorderLayout.CENTER); // make a little results panel to hold the current // running sum of number-button-clicks-so-far JPanel resultsPanel = new JPanel(); JLabel resultsLabel = new JLabel("Sum so far: "); resultsLabel.setFont(DISPLAY_FONT); resultsPanel.add(resultsLabel); resultsField = new JTextField("0", 5); resultsField.setFont(DISPLAY_FONT); resultsField.setEditable(false); resultsField.setHorizontalAlignment(JTextField.RIGHT); resultsPanel.add(resultsField); newActionPanel.add(resultsPanel, BorderLayout.SOUTH); return newActionPanel; } /** * an action listener that updates the sum so far * based on a number-button push */ private class NumButtonAction implements ActionListener { // default constructor will suffice, in this case /** * adds the source's label value to the running total * * @param event a click of a number button */ public void actionPerformed(ActionEvent event) { // initial stub //System.out.println("CLICKED A BUTTON!"); // hey, the event object has a method // getActionCommand -- which is the // corresponding "command" for this // action event; // when the event is a button push, // the action command is the text // of the clicked button int currButtonVal = Integer.parseInt(event.getActionCommand()); runningTotal += currButtonVal; resultsField.setText("" + runningTotal); } } /** * make a sub-panel with a clear button in its center * * @return a panel with a clear button centered in it */ private JPanel makeClearPanel() { JPanel newClearPanel = new JPanel(); JButton clearButton = new JButton("Clear"); clearButton.setFont(DISPLAY_FONT); clearButton.setForeground(Color.RED); clearButton.addActionListener(new ClearAction()); newClearPanel.add(clearButton); return newClearPanel; } /** * action to clear the results field when the clear * button is pushed */ private class ClearAction implements ActionListener { // default constructor will suffice, here /** * clear out the running total * * @param event a click of the clear button */ public void actionPerformed(ActionEvent event) { runningTotal = 0; resultsField.setText("0"); } } }