MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.text.Keymap;

public class MainClass {

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

        JTextField textField = new JTextField();
        frame.add(textField, BorderLayout.NORTH);

        JPanel panel = new JPanel();
        JButton defaultButton = new JButton("Default Button");
        defaultButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(actionEvent.getActionCommand() + " selected");
            }
        });
        panel.add(defaultButton);

        JButton otherButton = new JButton("Other Button");
        otherButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(actionEvent.getActionCommand() + " selected");
            }
        });
        panel.add(otherButton);

        frame.add(panel, BorderLayout.SOUTH);

        Keymap keymap = textField.getKeymap();
        KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
        keymap.removeKeyStrokeBinding(keystroke);

        frame.getRootPane().setDefaultButton(defaultButton);

        frame.setSize(250, 150);
        frame.setVisible(true);

    }
}