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:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Label Focus Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name: ");
    label.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField textField = new JTextField();
    label.setLabelFor(textField);/*from ww w  . j a v a2s .  c  o  m*/
    panel.add(label, BorderLayout.WEST);
    panel.add(textField, BorderLayout.CENTER);
    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:IconMatteBorder.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Icon Matted Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon diamondIcon = new DiamondIcon(Color.red, true, 10, 10);
    Border diamondBorder = new MatteBorder(10, 10, 10, 10, diamondIcon);
    JButton diamondButton = new JButton("diamonds");
    diamondButton.setBorder(diamondBorder);
    Icon diamondIcon2 = new ImageIcon("diamond.gif");
    Border diamondBorder2 = new MatteBorder(36, 40, 36, 40, diamondIcon2);
    JButton diamondButton2 = new JButton("diamonds2");
    diamondButton2.setBorder(diamondBorder2);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(2, 1));
    contentPane.add(diamondButton);/*from  w  w  w  .  j  a  v a2  s .  c om*/
    contentPane.add(diamondButton2);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:StrutSample.java

public static void main(String args[]) {
    Box horizontalBox;//w ww .jav a  2 s.co  m
    JPanel panel;
    JFrame frame = new JFrame("Horizontal Strut");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(0, 1));

    horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(Box.createHorizontalStrut(10));
    horizontalBox.add(new JButton("Left"));
    horizontalBox.add(new JButton("Middle"));
    horizontalBox.add(new JButton("Right"));
    panel = new JPanel(new BorderLayout());
    panel.add(horizontalBox);
    panel.setBorder(BorderFactory.createTitledBorder("Beginning Strut"));
    contentPane.add(panel);

    horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(new JButton("Left"));
    horizontalBox.add(Box.createHorizontalStrut(10));
    horizontalBox.add(new JButton("Middle"));
    horizontalBox.add(Box.createHorizontalStrut(25));
    horizontalBox.add(new JButton("Right"));
    panel = new JPanel(new BorderLayout());
    panel.add(horizontalBox);
    panel.setBorder(BorderFactory.createTitledBorder("2 Middle Struts"));
    contentPane.add(panel);

    horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(Box.createHorizontalStrut(25));
    horizontalBox.add(new JButton("Left"));
    horizontalBox.add(new JButton("Middle"));
    horizontalBox.add(new JButton("Right"));
    horizontalBox.add(Box.createHorizontalStrut(10));
    panel = new JPanel(new BorderLayout());
    panel.add(horizontalBox);
    panel.setBorder(BorderFactory.createTitledBorder("Beginning/End Struts"));
    contentPane.add(panel);

    horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(new JButton("Left"));
    horizontalBox.add(new JButton("Middle"));
    horizontalBox.add(new JButton("Right"));
    panel = new JPanel(new BorderLayout());
    horizontalBox.add(Box.createHorizontalStrut(10));
    panel.add(horizontalBox);
    panel.setBorder(BorderFactory.createTitledBorder("End Strut"));
    contentPane.add(panel);

    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:MainClass.java

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

    ActionListener actionListener = new ActionFocusMover();
    MouseListener mouseListener = new MouseEnterFocusMover();

    frame.setLayout(new GridLayout(3, 3));
    for (int i = 1; i < 10; i++) {
        JButton button = new JButton(Integer.toString(i));
        button.addActionListener(actionListener);
        button.addMouseListener(mouseListener);
        if ((i % 2) != 0) {
            button.setFocusable(false);/*from  ww w .ja v  a 2s. c  om*/
        }
        frame.add(button);
    }

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

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();

    JTextArea textArea = new JTextArea();
    f.add(new JScrollPane(textArea), BorderLayout.CENTER);

    Timer timer = new Timer(1000, new ActionListener() {
        @Override//from  w  w w  . ja v a 2s  . c  o m
        public void actionPerformed(ActionEvent e) {
            textArea.append("bla");
        }
    });
    timer.setRepeats(true);
    timer.start();
    JButton button = new JButton("Click me");
    button.addActionListener(e -> {
        System.out.println("Before option pane");
        JOptionPane.showMessageDialog(f, "A message dialog");
        System.out.println("After option pane");
    });
    f.add(button, BorderLayout.SOUTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:MyActionListener.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("ActionListener Test 1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Register");
    button.addActionListener(new MyActionListener());
    frame.getContentPane().add(button);//from w  ww.jav  a  2  s.com
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame() {
        @Override/*from  w  w  w  .jav  a 2s .  c  om*/
        public synchronized void setExtendedState(int state) {
            if (isUndecorated() && (state & MAXIMIZED_BOTH) == MAXIMIZED_BOTH) {
                super.setMaximizedBounds(new Rectangle(300, 300));
            }
            super.setExtendedState(state);
        }
    };
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800, 600);
    frame.setUndecorated(true);
    frame.getContentPane().add(new JButton(new AbstractAction("Toggle maximize") {
        @Override
        public void actionPerformed(ActionEvent e) {
            int state = frame.getExtendedState();
            if ((state & JFrame.MAXIMIZED_BOTH) == JFrame.MAXIMIZED_BOTH) {
                frame.setExtendedState(JFrame.NORMAL);
            } else {
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            }
        }
    }), BorderLayout.PAGE_END);
    frame.setVisible(true);
}

From source file:LabelFor.java

public static void main(String args[]) {
    JFrame f = new JFrame("LabelFor Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    JLabel label = new JLabel("Username");
    JTextField textField = new JTextField();
    label.setDisplayedMnemonic(KeyEvent.VK_U);
    label.setLabelFor(textField);/*from ww  w .j ava  2 s.  c  o  m*/
    Container box = Box.createHorizontalBox();
    box.add(label);
    box.add(textField);
    content.add(box, BorderLayout.NORTH);
    content.add(new JButton("Submit"), BorderLayout.SOUTH);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:GridBagWithGridWidthHeight.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of gridwidth, gridheight constraints");
    JPanel p = new JPanel(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;//from   w ww  . j a v a  2 s.c o  m
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2; // span across 2 rows
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1; // set back to 1 row
    c.gridwidth = 2; // span across 2 columns
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1; // set back to 1 column
    p.add(new JButton("and"), c);
    c.gridx = 2;
    p.add(new JButton("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:BorderLayoutExample.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JMenuBar menubar = new JMenuBar();
    JMenu file = new JMenu("File");

    menubar.add(file);/* ww  w . j  av a2  s .  co  m*/
    f.setJMenuBar(menubar);

    JToolBar toolbar = new JToolBar();
    toolbar.setFloatable(false);

    JButton bexit = new JButton(new ImageIcon("exit.png"));
    bexit.setBorder(new EmptyBorder(0, 0, 0, 0));
    toolbar.add(bexit);

    f.add(toolbar, BorderLayout.NORTH);

    JToolBar vertical = new JToolBar(JToolBar.VERTICAL);
    vertical.setFloatable(false);
    vertical.setMargin(new Insets(10, 5, 5, 5));

    JButton selectb = new JButton(new ImageIcon("a.png"));
    selectb.setBorder(new EmptyBorder(3, 0, 3, 0));

    JButton freehandb = new JButton(new ImageIcon("b.png"));
    freehandb.setBorder(new EmptyBorder(3, 0, 3, 0));
    JButton shapeedb = new JButton(new ImageIcon("c.png"));
    shapeedb.setBorder(new EmptyBorder(3, 0, 3, 0));

    vertical.add(selectb);
    vertical.add(freehandb);
    vertical.add(shapeedb);

    f.add(vertical, BorderLayout.WEST);
    f.add(new JTextArea(), BorderLayout.CENTER);

    JLabel statusbar = new JLabel(" Statusbar");
    statusbar.setPreferredSize(new Dimension(-1, 22));
    statusbar.setBorder(LineBorder.createGrayLineBorder());
    f.add(statusbar, BorderLayout.SOUTH);
    f.setSize(350, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}