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) {
    JButton button = new JButton("Test");
    ButtonModel model = button.getModel();
    model.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println("Armed: " + model.isArmed() + " Enabled: " + model.isEnabled() + " Pressed: "
                    + model.isPressed());
        }/*  w w  w. j av  a 2s  .  co  m*/
    });

    JOptionPane.showMessageDialog(null, button);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTabbedPane pane = new JTabbedPane();
    JButton component = new JButton("button");

    Icon icon = new ImageIcon("icon.gif");
    pane.addTab("label", icon, component);

}

From source file:Main.java

public static void main(String... args) {
    JFrame f = new JFrame();
    JButton button;/*from   w  w w.ja  va  2 s . c  o  m*/

    JPanel p = new JPanel();
    button = new JButton("Button");
    p.setLayout(null);
    button.setBounds(40, 100, 100, 60);
    p.add(button);

    f.add(p);
    // setLayout(null);
    f.setDefaultCloseOperation(3);
    f.setSize(400, 400);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");

    jb.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent ev) {
            System.out.println("ChangeEvent!");
        }// w  ww .j a  va  2  s  . c  om
    });
    ChangeListener[] lis = jb.getChangeListeners();
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:ButtonFocus.java

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

    JButton focusButton = new JButton("Focused");
    JButton notFocusButton = new JButton("Not Focused");

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());
    contentPane.add(focusButton);// www  . j  a  v  a2 s  .c o  m
    contentPane.add(notFocusButton);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

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

    JFrame jfrm = new JFrame();
    jfrm.setSize(300, 300);//w w w . ja  va2 s  . com

    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrm.getRootPane().setDefaultButton(jbtnA);

    jbtnA.setMnemonic(KeyEvent.VK_A);

    jbtnA.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            System.out.println("Alpha pressed. Beta is enabled.");
            jbtnA.setEnabled(!jbtnA.isEnabled());
        }
    });
    jfrm.add(jbtnA);
    jfrm.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");

    jb.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent ev) {
            System.out.println("ChangeEvent!");
        }//from w ww .  j  a  v a 2 s.c o  m
    });
    jb.setActionCommand("Bob");

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    final JFrame frame = new JFrame();
    frame.setUndecorated(true);/*w w  w. j  a  va 2 s.c o  m*/
    JButton button = new JButton("Minimize");
    button.addActionListener(e -> frame.setState(Frame.ICONIFIED));
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JComponent button = new JButton("Cut");
    String tooltiptext = "<html>" + "This is the " + "<img src=\"file:cut.gif\">" + " tool tip text."
            + "</html>";
    button.setToolTipText(tooltiptext);/* w w  w  .  ja v  a  2  s  .  com*/
    JPanel panel = new JPanel();
    panel.add(button);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(400, 400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object object = new JButton("push me");

    ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
    out.writeObject(object);// www.  j  a va2  s . c o m
    out.close();

    // Serialize to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    out = new ObjectOutputStream(bos);
    out.writeObject(object);
    out.close();

    // Get the bytes of the serialized object
    byte[] buf = bos.toByteArray();
    System.out.println(new String(buf));
}