Example usage for java.awt FlowLayout CENTER

List of usage examples for java.awt FlowLayout CENTER

Introduction

In this page you can find the example usage for java.awt FlowLayout CENTER.

Prototype

int CENTER

To view the source code for java.awt FlowLayout CENTER.

Click Source Link

Document

This value indicates that each row of components should be centered.

Usage

From source file:ImageLabelExample.java

public static void main(String[] args) {
    JLabel[] labels = new JLabel[9];

    labels[0] = makeLabel(JLabel.TOP, JLabel.LEFT);
    labels[1] = makeLabel(JLabel.TOP, JLabel.CENTER);
    labels[2] = makeLabel(JLabel.TOP, JLabel.RIGHT);
    labels[3] = makeLabel(JLabel.CENTER, JLabel.LEFT);
    labels[4] = makeLabel(JLabel.CENTER, JLabel.CENTER);
    labels[5] = makeLabel(JLabel.CENTER, JLabel.RIGHT);
    labels[6] = makeLabel(JLabel.BOTTOM, JLabel.LEFT);
    labels[7] = makeLabel(JLabel.BOTTOM, JLabel.CENTER);
    labels[8] = makeLabel(JLabel.BOTTOM, JLabel.RIGHT);

    labels[0].setEnabled(false);// w  w w  .j av a  2  s . c  om

    labels[1].setDisabledIcon(new ImageIcon("2.gif"));
    labels[1].setEnabled(false);

    labels[2].setIconTextGap(15);
    labels[3].setIconTextGap(0);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = frame.getContentPane();
    c.setLayout(new FlowLayout(FlowLayout.CENTER, 3, 3));
    for (int i = 0; i < 9; i++)
        c.add(labels[i]);
    frame.setSize(350, 150);
    frame.setVisible(true);
}

From source file:ModifyModelSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Modifying Model");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    // Fill model
    final DefaultListModel model = new DefaultListModel();
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);//from w  w  w  .j a  v  a2 s . c  o  m
    }
    JList jlist = new JList(model);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    contentPane.add(scrollPane1, BorderLayout.WEST);

    final JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    JScrollPane scrollPane2 = new JScrollPane(textArea);
    contentPane.add(scrollPane2, BorderLayout.CENTER);

    ListDataListener listDataListener = new ListDataListener() {
        public void contentsChanged(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalAdded(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        public void intervalRemoved(ListDataEvent listDataEvent) {
            appendEvent(listDataEvent);
        }

        private void appendEvent(ListDataEvent listDataEvent) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            switch (listDataEvent.getType()) {
            case ListDataEvent.CONTENTS_CHANGED:
                pw.print("Type: Contents Changed");
                break;
            case ListDataEvent.INTERVAL_ADDED:
                pw.print("Type: Interval Added");
                break;
            case ListDataEvent.INTERVAL_REMOVED:
                pw.print("Type: Interval Removed");
                break;
            }
            pw.print(", Index0: " + listDataEvent.getIndex0());
            pw.print(", Index1: " + listDataEvent.getIndex1());
            DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource();
            Enumeration elements = theModel.elements();
            pw.print(", Elements: ");
            while (elements.hasMoreElements()) {
                pw.print(elements.nextElement());
                pw.print(",");
            }
            pw.println();
            textArea.append(sw.toString());
        }
    };

    model.addListDataListener(listDataListener);

    // Setup buttons
    JPanel jp = new JPanel(new GridLayout(2, 1));
    JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
    JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1));
    jp.add(jp1);
    jp.add(jp2);
    JButton jb = new JButton("add F");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.add(0, "First");
        }
    });
    jb = new JButton("addElement L");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.addElement("Last");
        }
    });
    jb = new JButton("insertElementAt M");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            model.insertElementAt("Middle", size / 2);
        }
    });
    jb = new JButton("set F");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.set(0, "New First");
        }
    });
    jb = new JButton("setElementAt L");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.setElementAt("New Last", size - 1);
        }
    });
    jb = new JButton("load 10");
    jp1.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            for (int i = 0, n = labels.length; i < n; i++) {
                model.addElement(labels[i]);
            }
        }
    });
    jb = new JButton("clear");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.clear();
        }
    });
    jb = new JButton("remove F");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.remove(0);
        }
    });
    jb = new JButton("removeAllElements");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.removeAllElements();
        }
    });
    jb = new JButton("removeElement 'Last'");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            model.removeElement("Last");
        }
    });
    jb = new JButton("removeElementAt M");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.removeElementAt(size / 2);
        }
    });
    jb = new JButton("removeRange FM");
    jp2.add(jb);
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            int size = model.getSize();
            if (size != 0)
                model.removeRange(0, size / 2);
        }
    });
    contentPane.add(jp, BorderLayout.SOUTH);
    frame.setSize(640, 300);
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    super(new FlowLayout(FlowLayout.CENTER, 10, 3));
    add(new JButton("w w w.j a v a 2 s . c o m"));
    add(new JButton("w w w.j a v a 2 s . com"));
    add(new JButton("w w w.java2s.com"));
    add(new JButton("www.j ava 2 s . c o m"));
}

From source file:Main.java

public Main() {
    this.setSize(400, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Change Frame Color");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = (Component) e.getSource();
            JFrame frame = (JFrame) SwingUtilities.getRoot(component);
            frame.setBackground(Color.RED);
        }/*from   w ww  . j a  v  a2 s  .com*/
    });
    this.getContentPane().add(button);
}

From source file:Main.java

public Main() {
    this.setSize(400, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    JButton button = new JButton("Change Frame Color");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = (Component) e.getSource();
            JFrame frame = (JFrame) SwingUtilities.getRoot(component);
            frame.getContentPane().setBackground(Color.RED);
        }/* www  .  j  a  v  a 2 s.c o m*/
    });
    this.getContentPane().add(button);
}

From source file:Main.java

public Main() throws HeadlessException {
    setSize(300, 300);//from w ww  .  ja v a  2 s  . c  om
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));

    JButton disable = new JButton("DISABLE");
    disable.setToolTipText("disabled.");
    disable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ToolTipManager.sharedInstance().setEnabled(false);
        }
    });
    JButton enable = new JButton("ENABLE");
    enable.setToolTipText("enabled.");
    enable.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            ToolTipManager.sharedInstance().setEnabled(true);
        }
    });
    getContentPane().add(enable);
    getContentPane().add(disable);
}

From source file:Main.java

public Main() {

    FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT, 10, 3);
    setLayout(flowLayout);//from   w w  w . j  a v  a  2  s  . c  om

    add(new JButton("w w w.j a v a 2 s . c o m"));
    add(new JButton("w w w.j a v a 2 s . com"));
    add(new JButton("w w w.java2s.com"));
    add(new JButton("www.j ava 2 s . c o m"));

    flowLayout.setAlignment(FlowLayout.CENTER);
}

From source file:Main.java

public Main() throws HeadlessException {
    this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    JButton button = new JButton("Close");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);/*  www.  j  a v  a 2 s  .  com*/
        }
    });

    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.setSize(new Dimension(100, 100));
    this.getContentPane().add(button);
}

From source file:Main.java

public Main() {

    super();//from w w  w. j a  va  2  s  . com
    java.util.List<String> list = new ArrayList<String>();

    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");

    Object[] arrayObject = list.toArray();
    String[] data = Arrays.copyOf(arrayObject, arrayObject.length, String[].class); // java 1.6+
    JComboBox<String> combo = new JComboBox<>(data);

    setLayout(new FlowLayout(FlowLayout.CENTER));
    add(combo, BorderLayout.CENTER);
}

From source file:Main.java

public Main() {
    super();/*  w ww .ja  v  a2 s  .co  m*/
    states = new JComboBox<String>(new String[] { "Select a State", "AL", "AK", "AZ", "AR", "CA", "CO", "CT",
            "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN",
            "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI",
            "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" });

    setLayout(new FlowLayout(FlowLayout.CENTER));
    add(states, BorderLayout.CENTER);

    states.addItemListener(e -> {
        if (states.getSelectedIndex() > 0) {
            System.out.println("YOU CLICK INDEX- " + states.getSelectedIndex());
        }
    });
}