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();
    JPanel panel = new JPanel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GroupLayout layout = new GroupLayout(panel);
    panel.setLayout(layout);/*from   w w w  .j  a  v a  2  s.c  om*/

    System.out.println(layout.getAutoCreateGaps());

    JButton buttonD = new JButton("D");
    JButton buttonR = new JButton("R");
    JButton buttonY = new JButton("Y");
    JButton buttonO = new JButton("O");
    JButton buttonT = new JButton("T");

    GroupLayout.SequentialGroup leftToRight = layout.createSequentialGroup();

    leftToRight.addComponent(buttonD);
    GroupLayout.ParallelGroup columnMiddle = layout.createParallelGroup();
    columnMiddle.addComponent(buttonR);
    columnMiddle.addComponent(buttonO);
    columnMiddle.addComponent(buttonT);
    leftToRight.addGroup(columnMiddle);
    leftToRight.addComponent(buttonY);

    GroupLayout.SequentialGroup topToBottom = layout.createSequentialGroup();
    GroupLayout.ParallelGroup rowTop = layout.createParallelGroup();
    rowTop.addComponent(buttonD);
    rowTop.addComponent(buttonR);
    rowTop.addComponent(buttonY);
    topToBottom.addGroup(rowTop);
    topToBottom.addComponent(buttonO);
    topToBottom.addComponent(buttonT);

    layout.setHorizontalGroup(leftToRight);
    layout.setVerticalGroup(topToBottom);

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

From source file:FocusCycleSample.java

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

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(3, 3));
    for (int i = 0; i < 8; i++) {
        JButton button = new JButton("" + i);
        contentPane.add(button);//from   w  w  w.  j  a  va2s. c om
    }

    JPanel panel = new FocusCycleConstrainedJPanel();
    panel.setLayout(new GridLayout(1, 3));
    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + (i + 3));
        panel.add(button);
    }
    contentPane.add(panel);

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

From source file:Main.java

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

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setContinuousLayout(true);
    splitPane.setOneTouchExpandable(true);

    JComponent topComponent = new JButton("A");
    splitPane.setTopComponent(topComponent);

    JComponent bottomComponent = new JButton("B");
    splitPane.setBottomComponent(bottomComponent);

    frame.add(splitPane, BorderLayout.CENTER);
    frame.setSize(300, 150);//from  w  w w. j av  a2s . com
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    FlowLayout layout = new FlowLayout(FlowLayout.LEADING, horizontalGap, verticalGap);
    JButton button = new JButton("Discard");
    JLabel[] panels = new JLabel[5];

    JFrame frame = new JFrame("Poker");
    frame.setSize(width, height);/* w w w .  j a va 2 s . c o m*/
    frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

    JPanel deckPanel = new JPanel(layout);
    for (int i = 0; i < 5; i++) {
        panels[i] = new JLabel("" + i);
        deckPanel.add(panels[i]);
    }
    frame.getContentPane().add(deckPanel);
    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttonPanel.add(button);
    frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();

    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GroupLayout layout = new GroupLayout(panel);
    panel.setLayout(layout);//from   w  ww .j a  v a  2  s .c om

    System.out.println(layout.getHonorsVisibility());

    JButton buttonD = new JButton("D");
    JButton buttonR = new JButton("R");
    JButton buttonY = new JButton("Y");
    JButton buttonO = new JButton("O");
    JButton buttonT = new JButton("T");

    GroupLayout.SequentialGroup leftToRight = layout.createSequentialGroup();

    leftToRight.addComponent(buttonD);
    GroupLayout.ParallelGroup columnMiddle = layout.createParallelGroup();
    columnMiddle.addComponent(buttonR);
    columnMiddle.addComponent(buttonO);
    columnMiddle.addComponent(buttonT);
    leftToRight.addGroup(columnMiddle);
    leftToRight.addComponent(buttonY);

    GroupLayout.SequentialGroup topToBottom = layout.createSequentialGroup();
    GroupLayout.ParallelGroup rowTop = layout.createParallelGroup();
    rowTop.addComponent(buttonD);
    rowTop.addComponent(buttonR);
    rowTop.addComponent(buttonY);
    topToBottom.addGroup(rowTop);
    topToBottom.addComponent(buttonO);
    topToBottom.addComponent(buttonT);

    layout.setHorizontalGroup(leftToRight);
    layout.setVerticalGroup(topToBottom);

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

From source file:Main.java

public static void main(String[] args) {
    int TIME_VISIBLE = 3000;
    JFrame frame1 = new JFrame();
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(100, 100);//from   w w  w .ja v  a 2 s.  c o m
    frame1.setLocation(100, 100);

    JButton button = new JButton("My Button");
    frame1.getContentPane().add(button);

    button.addActionListener(e -> {
        JOptionPane pane = new JOptionPane("Message", JOptionPane.INFORMATION_MESSAGE);
        JDialog dialog = pane.createDialog(null, "Title");
        dialog.setModal(false);
        dialog.setVisible(true);

        new Timer(TIME_VISIBLE, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        }).start();
    });

    frame1.setVisible(true);

}

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 w w w  .  jav a2s . c om

    GridBagConstraints gbc = new GridBagConstraints();
    JButton component1 = new JButton("a");
    JButton component2 = new JButton("b");

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbl.setConstraints(component1, gbc);
    container.add(component1);

    gbc.gridx = 0;
    gbc.gridy = 0;
    gbl.setConstraints(component2, gbc);
    container.add(component2);

    container.add(component1);
    container.add(component2);
    frame.pack();
    frame.setVisible(true);

    gbl.layoutContainer(container);

    int[][] dim = gbl.getLayoutDimensions();
    int cols = dim[0].length;
    int rows = dim[1].length;

    System.out.println(cols);
    System.out.println(rows);
}

From source file:BoxSample.java

public static void main(String args[]) {
    JFrame horizontalFrame = new JFrame("Horizontal");
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Box horizontalBox = Box.createHorizontalBox();
    horizontalBox.add(new JLabel("Left"));
    horizontalBox.add(new JTextField("Middle"));
    horizontalBox.add(new JButton("Right"));
    horizontalFrame.add(horizontalBox, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);/*  w w  w . j  av a 2 s  .  c o m*/
    horizontalFrame.setVisible(true);
}

From source file:ExpandableSplit.java

public static void main(String args[]) {
    String title = (args.length == 0 ? "Expandable Split" : args[0]);

    JFrame vFrame = new JFrame(title);
    vFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComponent leftButton = new JButton("Top");
    JComponent rightButton = new JButton("Bottom");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setOneTouchExpandable(true);
    splitPane.setLeftComponent(leftButton);
    splitPane.setRightComponent(rightButton);
    ActionListener oneActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            splitPane.resetToPreferredSizes();
        }//from   w w  w. ja v a 2 s.com
    };
    ((JButton) rightButton).addActionListener(oneActionListener);
    ActionListener anotherActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            splitPane.setDividerLocation(10);
            splitPane.setContinuousLayout(true);
        }
    };
    ((JButton) leftButton).addActionListener(anotherActionListener);
    vFrame.getContentPane().add(splitPane, BorderLayout.CENTER);
    vFrame.setSize(300, 150);
    vFrame.setVisible(true);

}

From source file:MainClass.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Etched Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border raisedBorder = new EtchedBorder(EtchedBorder.RAISED);
    Border loweredBorder = new EtchedBorder(EtchedBorder.LOWERED);
    JButton raisedButton = new JButton("Raised");
    raisedButton.setBorder(raisedBorder);
    JButton loweredButton = new JButton("Lowered");
    loweredButton.setBorder(loweredBorder);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2, 5, 5));
    contentPane.add(raisedButton);// ww w.  jav  a  2  s  .  co m
    contentPane.add(loweredButton);
    frame.setSize(300, 100);
    frame.setVisible(true);
}