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

public static void main(String args[]) {
    String actionKey = "theAction";
    JFrame f = new JFrame("Key Tester");
    JButton jb1 = new JButton("<html><center>B<br>Focused/Typed");
    JButton jb2 = new JButton("<html><center>Ctrl-C<br>Window/Pressed");
    JButton jb3 = new JButton("<html><center>Shift-D<br>Ancestor/Released");
    Container pane = f.getContentPane();
    pane.add(jb1, BorderLayout.NORTH);
    pane.add(jb2, BorderLayout.CENTER);
    pane.add(jb3, BorderLayout.SOUTH);

    KeyStroke stroke = KeyStroke.getKeyStroke("typed B");
    Action action = new MyActionListener("Action Happened");
    // Defaults to JComponent.WHEN_FOCUSED map
    InputMap inputMap = jb1.getInputMap();
    inputMap.put(stroke, actionKey);/*from   w w  w . j av a2s .  c o  m*/
    ActionMap actionMap = jb1.getActionMap();
    actionMap.put(actionKey, action);

    stroke = KeyStroke.getKeyStroke("ctrl C");
    action = new MyActionListener("Action Didn't Happen");
    inputMap = jb2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, actionKey);
    actionMap = jb2.getActionMap();
    actionMap.put(actionKey, action);

    stroke = KeyStroke.getKeyStroke("shift released D");
    action = new MyActionListener("What Happened?");
    inputMap = jb3.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(stroke, actionKey);
    actionMap = jb3.getActionMap();
    actionMap.put(actionKey, action);

    f.setSize(200, 200);
    f.show();
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    Container pane = f.getContentPane();
    Icon icon = new RedOvalIcon();
    final Action action = new MyAction("Hello", icon);
    // Add tooltip
    action.putValue(Action.SHORT_DESCRIPTION, "World");
    JToolBar jt1 = new JToolBar();
    JButton b1 = new JButton(action);
    jt1.add(b1);/*  w w  w . ja  va 2s  . c  o  m*/
    pane.add(jt1, BorderLayout.NORTH);
    JToolBar jt2 = new JToolBar();
    JButton b2 = new JButton(action);
    jt2.add(b2);
    pane.add(jt2, BorderLayout.SOUTH);
    JButton jb = new JButton("Toggle Action");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            action.setEnabled(!action.isEnabled());
        }
    });
    pane.add(jb, BorderLayout.CENTER);
    f.setSize(200, 200);
    f.show();
}

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new BorderLayout(2, 2));
    BufferedImage bi = new BufferedImage(600, 200, BufferedImage.TYPE_INT_RGB);
    gui.add(new JLabel(new ImageIcon(bi)));

    JFrame myframe = new JFrame();
    JPanel myPanel = new JPanel();
    gui.add(myPanel, BorderLayout.PAGE_END);
    myPanel.setLayout(new GridLayout(2, 0, 0, 0));

    int x = 0;/*from   ww w  .j a v a2  s  . co  m*/
    int y = 5;

    for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
        myPanel.add(new JButton(alphabet + ""));
        x++;
        if (x > 15) {
            y = 6;
            x = 0;
        }
    }
    myframe.add(gui);
    myframe.pack();
    myframe.setVisible(true);

    myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    JPanel panel = new JPanel(new BorderLayout());
    JLabel label = new JLabel("Name: ");
    label.setDisplayedMnemonic(KeyEvent.VK_N);
    JTextField textField = new JTextField();
    label.setLabelFor(textField);/*from  w w w . j  a v a  2 s  .  c  o m*/
    panel.add(label, BorderLayout.WEST);
    panel.add(textField, BorderLayout.CENTER);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel, BorderLayout.NORTH);
    frame.add(new JButton("Somewhere Else"), BorderLayout.SOUTH);
    frame.setSize(250, 150);
    frame.setVisible(true);

    textField.setText("your text");
    String filename = "test.txt";

    FileWriter writer = new FileWriter(filename);
    textField.write(writer);
    writer.close();

}

From source file:ActionFocusMover.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Focus Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ActionListener actionListener = new ActionFocusMover();
    frame.setLayout(new GridLayout(3, 3));
    for (int i = 1; i < 10; i++) {
        JButton button = new JButton(Integer.toString(i));
        button.addActionListener(actionListener);
        if ((i % 2) != 0) {
            button.setFocusable(false);/*  w  w  w .j  av  a  2s .  c o m*/
        }
        frame.add(button);
    }
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:ABevelBorder.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Bevel Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border raisedBorder = BorderFactory.createRaisedBevelBorder();
    Border loweredBorder = BorderFactory.createLoweredBevelBorder();
    Border myRaisedBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.pink, Color.red);
    Border myLoweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.pink, Color.red);
    JButton raisedButton = new JButton("Raised");
    raisedButton.setBorder(raisedBorder);
    JButton loweredButton = new JButton("Lowered");
    loweredButton.setBorder(loweredBorder);
    JButton myRaisedButton = new JButton("My Raised");
    myRaisedButton.setBorder(myRaisedBorder);
    JButton myLoweredButton = new JButton("My Lowered");
    myLoweredButton.setBorder(myLoweredBorder);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(2, 2, 5, 5));
    contentPane.add(raisedButton);/*from   w ww  . j  av  a2s.  c o  m*/
    contentPane.add(loweredButton);
    contentPane.add(myRaisedButton);
    contentPane.add(myLoweredButton);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);//from   w  w w.jav a2 s. c om

    JDialog dialog = new JDialog(frame, "Dialog", true);

    JPanel mainGui = new JPanel(new BorderLayout());
    mainGui.setBorder(new EmptyBorder(20, 20, 20, 20));
    mainGui.add(new JLabel("Contents go here"), BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel(new FlowLayout());
    mainGui.add(buttonPanel, BorderLayout.SOUTH);

    JButton close = new JButton("Close");
    close.addActionListener(e -> dialog.setVisible(false));

    buttonPanel.add(close);

    frame.setVisible(true);

    dialog.setContentPane(mainGui);
    dialog.pack();
    dialog.setVisible(true);
}

From source file:TryBorderLayout.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Border Layout");
    aWindow.setBounds(30, 30, 300, 300); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    BorderLayout border = new BorderLayout(); // Create a layout manager
    Container content = aWindow.getContentPane(); // Get the content pane
    content.setLayout(border); // Set the container layout mgr
    EtchedBorder edge = new EtchedBorder(EtchedBorder.RAISED); // Button border
    // Now add five JButton components and set their borders
    JButton button;/*w w  w  . jav  a  2  s  .c  om*/
    content.add(button = new JButton("EAST"), BorderLayout.EAST);
    button.setBorder(edge);
    content.add(button = new JButton("WEST"), BorderLayout.WEST);
    button.setBorder(edge);
    content.add(button = new JButton("NORTH"), BorderLayout.NORTH);
    button.setBorder(edge);
    content.add(button = new JButton("SOUTH"), BorderLayout.SOUTH);
    button.setBorder(edge);
    content.add(button = new JButton("CENTER"), BorderLayout.CENTER);
    button.setBorder(edge);
    aWindow.setVisible(true); // Display the window
}

From source file:GridBagWithWeight.java

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

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;/* w w  w. j  a v a  2s .co m*/
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH; // Use both horizontal & vertical
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.fill = GridBagConstraints.HORIZONTAL; // Horizontal only
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE; // Remember to reset to none
    p.add(new JButton("and"), c);
    c.gridx = 2;
    c.fill = GridBagConstraints.VERTICAL; // Vertical only
    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:MainClass.java

public static void main(String[] args) {
    Locale locale = Locale.getDefault();
    ResourceBundle rb = ResourceBundle.getBundle("MyResources", locale);
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("I18N Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(3, 2));
    frame.add(new JLabel(rb.getString("userName")));
    frame.add(new JTextField());
    frame.add(new JLabel(rb.getString("password")));
    frame.add(new JPasswordField());
    frame.add(new JButton(rb.getString("login")));
    frame.pack();//from w w w .j  a  v a  2 s .com
    frame.setVisible(true);
}