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

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

    Border roundedBorder = new LineBorder(Color.BLACK, 12, true);

    JButton roundedButton = new JButton("Rounded 12 Pixel");
    roundedButton.setBorder(roundedBorder);

    Container contentPane = frame.getContentPane();
    contentPane.add(roundedButton, BorderLayout.SOUTH);
    frame.pack();//from  ww  w  .  java2  s  .co m
    frame.setSize(300, frame.getHeight());
    frame.setVisible(true);
}

From source file:Main.java

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

    frame.pack();// w  w w  . java 2s  .c  o  m
    frame.setVisible(true);

    JButton btnGetSelectedText = new JButton("Get selected text");
    btnGetSelectedText.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(jTextPane.getSelectedText());
        }
    });
    frame.getContentPane().add(jTextPane, BorderLayout.NORTH);
    frame.getContentPane().add(btnGetSelectedText, BorderLayout.SOUTH);
}

From source file:RelativeX.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.gridy = 0;/*w w  w  .  j  a  va2  s.c om*/
    pane.add(new JButton("First row"), gbc);
    gbc.gridx = GridBagConstraints.RELATIVE;
    gbc.gridy = 1;
    pane.add(new JButton("Second row, first column"), gbc);
    pane.add(new JButton("Second row, second column"), gbc);
    pane.add(new JButton("Second row, third column"), gbc);
    gbc.gridy = 2;
    pane.add(new JButton("Third row"), gbc);
    f.setSize(600, 300);
    f.setVisible(true);
}

From source file:RelativeY.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 = 0;//w w  w  .  j av a  2 s. c o  m
    pane.add(new JButton("First column"), gbc);
    gbc.gridx = 1;
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("Second column, first row"), gbc);
    pane.add(new JButton("Second column, second row"), gbc);
    pane.add(new JButton("Second column, third row"), gbc);
    gbc.gridx = 2;
    pane.add(new JButton("Third column"), gbc);
    f.setSize(500, 300);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    MyTextArea txt = new MyTextArea();
    f.getContentPane().add(txt);/*ww w.  jav a 2s.c o  m*/
    f.getContentPane().add(new JButton("OK"), BorderLayout.SOUTH);
    f.pack();
    f.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    String[] items = new String[] { "One", "Two", "Three", "Four" };
    JList<String> list = new JList<>(items);
    JFrame f = new JFrame();
    f.add(list, BorderLayout.CENTER);
    JButton btn = new JButton("Get selected");
    btn.addActionListener(new ActionListener() {
        @Override// w  ww.  jav  a 2s  . co m
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showConfirmDialog(f, "You Selected : " + list.getSelectedValue(), "Display",
                    JOptionPane.PLAIN_MESSAGE);
        }
    });

    f.add(btn, BorderLayout.SOUTH);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:MainClass.java

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

    frame.setLayout(new GridLayout(3, 3));

    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + i);
        frame.add(button);/*  w w w .java 2  s.com*/
    }

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

    for (int i = 0; i < 3; i++) {
        JButton button = new JButton("" + (i + 6));
        frame.add(button);
    }

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

From source file:Main.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container content = aWindow.getContentPane();
    content.setLayout(new FlowLayout(FlowLayout.LEFT));
    content.add(new JButton("www.java2s.com"));
    content.add(new JLabel("www.java2s.com"));
    content.add(new JTextField("www.java2s.com"));
    aWindow.setVisible(true);/*from  w  ww. j  a v  a  2s .c  o  m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Action action = new AbstractAction("Button Label") {
        // This method is called when the button is pressed
        public void actionPerformed(ActionEvent evt) {
            // Perform action...
        }//  w w  w. ja  v a  2  s  .  co  m
    };

    // Create the button from the action
    JButton button = new JButton(action);
}

From source file:OptionPaneSample.java

public static void main(String args[]) {
    JFrame f = new JFrame("JOptionPane Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            Object response = JOptionPane.showInputDialog(source, "Where do you want to go tomorrow?",
                    "JOptionPane Sample", JOptionPane.QUESTION_MESSAGE, null,
                    new String[] { "A", "B", "C", "D", "E" }, "E");
            System.out.println("Response: " + response);
        }/*from   ww  w.  j  a v  a2 s. c  om*/
    };
    button.addActionListener(actionListener);
    content.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
}