Example usage for java.awt Container add

List of usage examples for java.awt Container add

Introduction

In this page you can find the example usage for java.awt Container add.

Prototype

public void add(Component comp, Object constraints) 

Source Link

Document

Adds the specified component to the end of this container.

Usage

From source file:RelativeXY.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;// w ww  .  j  a v a  2s  .c o m
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("First row, first column"), gbc);
    pane.add(new JButton("Second row"), gbc);
    pane.add(new JButton("Third row"), gbc);
    gbc.gridx = GridBagConstraints.RELATIVE;
    pane.add(new JButton("First row, second column"), gbc);
    f.setSize(500, 300);
    f.setVisible(true);
}

From source file:Command.java

public static void main(String[] args) throws IOException {
    javax.swing.JFrame f = new javax.swing.JFrame("Command Test");
    javax.swing.JButton b1 = new javax.swing.JButton("Tick");
    javax.swing.JButton b2 = new javax.swing.JButton("Tock");
    javax.swing.JLabel label = new javax.swing.JLabel("Hello world");
    java.awt.Container pane = f.getContentPane();

    pane.add(b1, java.awt.BorderLayout.WEST);
    pane.add(b2, java.awt.BorderLayout.EAST);
    pane.add(label, java.awt.BorderLayout.NORTH);

    b1.addActionListener(Command.parse(label, "setText(\"tick\");"));
    b2.addActionListener(Command.parse(label, "setText(\"tock\");"));

    f.pack();/* w  w w .ja v  a2 s .  c om*/
    f.show();
}

From source file:UndoableDrawingPanel2.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Drawing Sample2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    UndoableDrawingPanel2 drawingPanel = new UndoableDrawingPanel2();

    UndoManager manager = new UndoManager();
    drawingPanel.addUndoableEditListener(manager);

    JToolBar toolbar = new JToolBar();
    toolbar.add(UndoManagerHelper.getUndoAction(manager));
    toolbar.add(UndoManagerHelper.getRedoAction(manager));

    Container content = frame.getContentPane();
    content.add(toolbar, BorderLayout.NORTH);
    content.add(drawingPanel, BorderLayout.CENTER);
    frame.setSize(300, 150);//from w  ww .j a  va2  s  .  c  o  m
    frame.setVisible(true);
}

From source file:SelectingJListSample.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    JFrame frame = new JFrame("Selecting JList");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JList jlist = new JList(labels);
    JScrollPane scrollPane1 = new JScrollPane(jlist);
    contentPane.add(scrollPane1, BorderLayout.WEST);

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent listSelectionEvent) {
            System.out.print("First index: " + listSelectionEvent.getFirstIndex());
            System.out.print(", Last index: " + listSelectionEvent.getLastIndex());
            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
                JList list = (JList) listSelectionEvent.getSource();
                int selections[] = list.getSelectedIndices();
                Object selectionValues[] = list.getSelectedValues();
                for (int i = 0, n = selections.length; i < n; i++) {
                    if (i == 0) {
                        System.out.print("  Selections: ");
                    }/*from ww w .j av a 2  s  . co  m*/
                    System.out.print(selections[i] + "/" + selectionValues[i] + " ");
                }
                System.out.println();
            }
        }
    };
    jlist.addListSelectionListener(listSelectionListener);

    MouseListener mouseListener = new MouseAdapter() {
        public void mouseClicked(MouseEvent mouseEvent) {
            JList theList = (JList) mouseEvent.getSource();
            if (mouseEvent.getClickCount() == 2) {
                int index = theList.locationToIndex(mouseEvent.getPoint());
                if (index >= 0) {
                    Object o = theList.getModel().getElementAt(index);
                    System.out.println("Double-clicked on: " + o.toString());
                }
            }
        }
    };
    jlist.addMouseListener(mouseListener);

    frame.setSize(350, 200);
    frame.setVisible(true);
}

From source file:GridBagLayoutColumnSpanHORIZONTAL.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;/* w  w w  .j a v  a  2 s.c  om*/
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("First row, first column"), gbc);
    pane.add(new JButton("Second row"), gbc);
    gbc.gridwidth = 2;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    pane.add(new JButton("Third row, spans two columns"), gbc);
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.NONE;
    gbc.gridx = GridBagConstraints.RELATIVE;
    pane.add(new JButton("First row, second column"), gbc);
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:MoveViewSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JViewport Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Icon icon = new ImageIcon("dog.jpg");
    JLabel dogLabel = new JLabel(icon);
    JViewport viewport = new JViewport();
    viewport.setView(dogLabel);//from ww w. ja v  a2s.  com

    InputMap inputMap = viewport.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = viewport.getActionMap();

    // Up key moves view up unit
    Action upKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, UNIT);
    KeyStroke upKey = KeyStroke.getKeyStroke("UP");
    inputMap.put(upKey, "up");
    actionMap.put("up", upKeyAction);

    // Down key moves view down unit
    Action downKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, UNIT);
    KeyStroke downKey = KeyStroke.getKeyStroke("DOWN");
    inputMap.put(downKey, "down");
    actionMap.put("down", downKeyAction);

    // Left key moves view left unit
    Action leftKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, UNIT);
    KeyStroke leftKey = KeyStroke.getKeyStroke("LEFT");
    inputMap.put(leftKey, "left");
    actionMap.put("left", leftKeyAction);

    // Right key moves view right unit
    Action rightKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, UNIT);
    KeyStroke rightKey = KeyStroke.getKeyStroke("RIGHT");
    inputMap.put(rightKey, "right");
    actionMap.put("right", rightKeyAction);

    // PgUp key moves view up block
    Action pgUpKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, BLOCK);
    KeyStroke pgUpKey = KeyStroke.getKeyStroke("PAGE_UP");
    inputMap.put(pgUpKey, "pgUp");
    actionMap.put("pgUp", pgUpKeyAction);

    // PgDn key moves view down block
    Action pgDnKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, BLOCK);
    KeyStroke pgDnKey = KeyStroke.getKeyStroke("PAGE_DOWN");
    inputMap.put(pgDnKey, "pgDn");
    actionMap.put("pgDn", pgDnKeyAction);

    // Shift-PgUp key moves view left block
    Action shiftPgUpKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, BLOCK);
    KeyStroke shiftPgUpKey = KeyStroke.getKeyStroke("shift PAGE_UP");
    inputMap.put(shiftPgUpKey, "shiftPgUp");
    actionMap.put("shiftPgUp", shiftPgUpKeyAction);

    // Shift-PgDn key moves view right block
    Action shiftPgDnKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, BLOCK);
    KeyStroke shiftPgDnKey = KeyStroke.getKeyStroke("shift PAGE_DOWN");
    inputMap.put(shiftPgDnKey, "shiftPgDn");
    actionMap.put("shiftPgDn", shiftPgDnKeyAction);

    Container contentPane = frame.getContentPane();
    contentPane.add(viewport, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:UndoDrawing.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Drawing Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    UndoableDrawingPanel drawingPanel = new UndoableDrawingPanel();

    UndoManager manager = new UndoManager();
    drawingPanel.addUndoableEditListener(manager);

    JToolBar toolbar = new JToolBar();
    JButton undoButton = new JButton(UndoManagerHelper.getUndoAction(manager));
    toolbar.add(undoButton);//from w w  w. j a v  a2  s .co m
    JButton redoButton = new JButton(UndoManagerHelper.getRedoAction(manager));
    toolbar.add(redoButton);

    Container content = frame.getContentPane();
    content.add(toolbar, BorderLayout.NORTH);
    content.add(drawingPanel, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:UndoDrawing.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Drawing Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    UndoableDrawingPanel drawingPanel = new UndoableDrawingPanel();

    UndoManager manager = new UndoManager();
    drawingPanel.addUndoableEditListener(manager);

    JToolBar toolbar = new JToolBar();
    toolbar.add(UndoManagerHelper.getUndoAction(manager));
    toolbar.add(UndoManagerHelper.getRedoAction(manager));

    Container content = frame.getContentPane();
    content.add(toolbar, BorderLayout.NORTH);
    content.add(drawingPanel, BorderLayout.CENTER);
    frame.setSize(300, 150);/*  w w w .ja v a  2  s .  com*/
    frame.setVisible(true);
}

From source file:ButtonBorderTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Fourth Button");
    Container contentPane = frame.getContentPane();
    Icon icon = new ImageIcon("java2s.gif");
    JButton b = new JButton("Button!");
    Border bored = BorderFactory.createMatteBorder(10, 5, 10, 5, icon);
    b.setBorder(bored);//from  w w  w.j  a v  a 2s . co m
    contentPane.add(b, BorderLayout.CENTER);
    frame.setSize(350, 200);
    frame.show();
}

From source file:JMFPlayer.java

public static void main(String[] argv) {
    JFrame f = new JFrame("JMF Player Demo");
    Container frameCP = f.getContentPane();
    JMFPlayer p = new JMFPlayer(f, argv.length == 0 ? "file:///C:/music/midi/beet5th.mid" : argv[0]);
    frameCP.add(BorderLayout.CENTER, p);
    f.setSize(200, 200);//  ww w .jav  a2 s . c  o  m
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}