Example usage for javax.swing JButton JButton

List of usage examples for javax.swing JButton JButton

Introduction

In this page you can find the example usage for javax.swing JButton JButton.

Prototype

public JButton(Action a) 

Source Link

Document

Creates a button where properties are taken from the Action supplied.

Usage

From source file:AdjustmentTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    Icon icon = new ImageIcon("java2s.gif");
    JButton b = new JButton(icon);
    JScrollPane pane = new JScrollPane(b);
    AdjustmentListener hListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Horizontal: ");
            dumpInfo(e);//from   w  w  w .ja v  a  2 s. co m
        }
    };
    JScrollBar hBar = pane.getHorizontalScrollBar();
    hBar.addAdjustmentListener(hListener);
    AdjustmentListener vListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Vertical: ");
            dumpInfo(e);
        }
    };
    JScrollBar vBar = pane.getVerticalScrollBar();
    vBar.addAdjustmentListener(vListener);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.show();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame frame = new JFrame("Cornering Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("+");

    JLabel bigLabel = new JLabel("A");
    bigLabel.setPreferredSize(new Dimension(400, 400));
    bigLabel.setBorder(new LineBorder(Color.red));

    JScrollPane scrollPane = new JScrollPane(bigLabel);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, button);
    scrollPane.setColumnHeaderView(new JLabel("B"));
    scrollPane.setRowHeaderView(new JLabel("C"));

    ActionListener actionListener = new JScrollPaneToTopAction(scrollPane);
    button.addActionListener(actionListener);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 200);// www.j av a2  s .c o m
    frame.setVisible(true);
}

From source file:Toolbars.java

public static void main(String[] args) {
    JToolBar toolbar1 = new JToolBar();
    JToolBar toolbar2 = new JToolBar();

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    JButton newb = new JButton(new ImageIcon("new.png"));
    JButton openb = new JButton(new ImageIcon("open.png"));
    JButton saveb = new JButton(new ImageIcon("save.png"));

    toolbar1.add(newb);/*from  ww  w. jav  a 2 s  . c o  m*/
    toolbar1.add(openb);
    toolbar1.add(saveb);
    toolbar1.setAlignmentX(0);

    JButton exitb = new JButton(new ImageIcon("exit.png"));
    toolbar2.add(exitb);
    toolbar2.setAlignmentX(0);

    exitb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    panel.add(toolbar1);
    panel.add(toolbar2);

    JFrame f = new JFrame();
    f.add(panel, BorderLayout.NORTH);

    f.setSize(300, 200);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new BorderLayout());
    gui.setBorder(new TitledBorder("Border Layout"));

    JPanel labels = new JPanel();
    labels.setBorder(new TitledBorder("Flow Layout"));
    labels.add(new JLabel("Label 1"));
    labels.add(new JLabel("Label 2"));

    gui.add(labels, BorderLayout.NORTH);
    gui.add(new JButton("Button"), BorderLayout.SOUTH);

    JOptionPane.showMessageDialog(null, gui);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();

    GridBagLayout gbl = new GridBagLayout();

    container.setLayout(gbl);//from  www.ja  va2 s .c  om

    // Place a component at cell location (1,1)
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;

    JButton component = new JButton("a");

    // Associate the gridbag constraints with the component
    gbl.setConstraints(component, gbc);

    container.add(component);

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JLabel label = new JLabel("java2s.com");

    JFrame frame = new JFrame("Testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(label);/*  ww w.j  a  v a2 s. com*/

    JPanel buttons = new JPanel(new GridLayout(0, 1));
    for (int index = 0; index < 10; index++) {
        buttons.add(new JButton(String.valueOf(index)));
    }

    JPanel right = new JPanel(new BorderLayout());
    right.add(buttons, BorderLayout.NORTH);
    frame.add(right, BorderLayout.EAST);

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Select Me");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("I was selected.");
        }/*from ww w .j a  va2  s . c  o m*/
    };

    MouseListener mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            int modifiers = mouseEvent.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                System.out.println("Left button pressed.");
            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
                System.out.println("Middle button pressed.");
            }
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                System.out.println("Right button pressed.");
            }
        }

        public void mouseReleased(MouseEvent mouseEvent) {
            if (SwingUtilities.isLeftMouseButton(mouseEvent)) {
                System.out.println("Left button released.");
            }
            if (SwingUtilities.isMiddleMouseButton(mouseEvent)) {
                System.out.println("Middle button released.");
            }
            if (SwingUtilities.isRightMouseButton(mouseEvent)) {
                System.out.println("Right button released.");
            }
            System.out.println();
        }
    };

    button.addActionListener(actionListener);
    button.addMouseListener(mouseListener);

    frame.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 100);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Reverse Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < 9; i++) {
        JButton button = new JButton(Integer.toString(i));
        frame.add(button);/* w w w  . jav  a  2s . c om*/
    }
    final Container contentPane = frame.getContentPane();
    Comparator<Component> comp = new Comparator<Component>() {
        public int compare(Component c1, Component c2) {
            Component comps[] = contentPane.getComponents();
            List list = Arrays.asList(comps);
            int first = list.indexOf(c1);
            int second = list.indexOf(c2);
            return second - first;
        }
    };
    FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp);
    frame.setFocusTraversalPolicy(policy);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:CloseAction.java

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

    JButton closeButton = new JButton(new CloseAction());
    contentPane.add(closeButton);//from   w  w  w.j  a  v  a  2  s .c  om

    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JFrame frame = new JFrame();

    final JButton activate = new JButton("Show");
    frame.add(activate);/*w  ww .ja v  a2  s.  c  om*/

    frame.pack();
    frame.setVisible(true);

    final Main glass = new Main(frame);
    frame.setGlassPane(glass);

    activate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            glass.setVisible(true);
        }
    });
}