开发者

how to apply "Enter" key for all buttons in the below frame

开发者 https://www.devze.com 2022-12-07 19:48 出处:网络
* import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder;

*

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.InputMap;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Array extends JFrame {

    private JPanel contentPane;
    private JTextField element;
    private JTextField length;
    private JTextField deleteposition;
    private JTextField insertposition;
    private JTextField displaybox;
    private int arr[];

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Array frame = new Array();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Array() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 566, 386);
        contentPane = new JPanel();
        contentPane.setBackground(new Color(245, 121, 0));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JLabel lblArrayDatastructure = new JLabel("ARRAY DATASTRUCTURE");
        lblArrayDatastructure.setForeground(new Color(0, 0, 0));
        lblArrayDatastructure.setFont(new Font("Tlwg Typist", Font.BOLD, 15));
        lblArrayDatastructure.setBounds(199, 12, 177, 17);
        contentPane.add(lblArrayDatastructure);
        
        JLabel lblEnterArrayLength = new JLabel("Enter Array Length");
        lblEnterArrayLength.setForeground(new Color(204, 0, 0));
        lblEnterArrayLength.setBounds(37, 48, 134, 15);
        contentPane.add(lblEnterArrayLength);
        
            
        JButton create = new JButton("CREATE ARRAY");
        //getRootPane().setDefaultButton(create);
        create.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //code for creating array
                //String len = length.getText();
                //int l = Integer.valueOf(len);
                int len = Integer.valueOf(length.getText());
                arr = new int [len];
                String message = "Array of length "+len+" Created";
                JOptionPane.showMessageDialog(contentPane, message);
            }
        });
        
        length = new JTextField();
        length.setBounds(199, 46, 134, 19);
        contentPane.add(length);
        length.setColumns(10);
        create.setFont(new Font("Courier 10 Pitch", Font.BOLD, 12));
        create.setBounds(388, 44, 134, 25);
        contentPane.add(create);
        
        JLabel lblInsertAnInteger = new JLabel("Insert An Integer Element");
        lblInsertAnInteger.setForeground(new Color(204, 0, 0));
        lblInsertAnInteger.setBounds(7, 110, 181, 15);
        contentPane.add(lblInsertAnInteger);
        
        element = new JTextField();
        element.setBounds(211, 108, 77, 19);
        contentPane.add(element);
        element.setColumns(10);
        
        JButton insert = new JButton("INSERT");
        insert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //code for insertion
                int elem = Integer.valueOf(element.getText());
                int pos = Integer.valueOf(insertposition.getText());
                arr[pos] = elem;
                String message = "Element "+elem+" Inserted at position "+pos;
                JOptionPane.showMessageDialog(contentPane, message);
                element.setText("");
                insertposition.setText("");
            }
        });
        insert.setFont(new Font("Courier 10 Pitch", Font.BOLD, 12));
        insert.setBounds(462, 106, 90, 25);
        contentPane.add(insert);
        
        JLabel lblDeletePosition = new JLabel("Delete Position");
        lblDeletePosition.setForeground(new Color(204, 0, 0));
        lblDeletePosition.setBounds(72, 177, 109, 15);
        contentPane.add(lblDeletePosition);
        
        deleteposition = new JTextField();
        deleteposition.setBounds(216, 174, 114, 21);
        contentPane.add(deleteposition);
        deleteposition.setColumns(10);
        
        insertposition = new JTextField();
        insertposition.setBounds(375, 108, 76, 19);
        contentPane.add(insertposition);
        insertposition.setColumns(10);
        
        JLabel lblPosition = new JLabel("Position");
        lblPosition.setForeground(new Color(204, 0, 0));
        lblPosition.setBounds(306, 110, 70, 15);
        contentPane.add(lblPosition);
        
        JButton delete = new JButton("DELETE");
        delete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //code for deletion
                int pos = Integer.valueOf(deleteposition.getText());
                arr[pos]=0;
                String message = "Element Deleted at position "+pos;
                JOptionPane.showMessageDialog(contentPane, message);
                deleteposition.setText("");
                
            }
        });
        delete.setFont(new Font("Courier 10 Pitch", Font.BOLD, 12));
        delete.setBounds(366, 173, 109, 25);
        contentPane.add(delete);
        
        JButton display = new JButton("DISPLAY");
        display.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //code for display
                String msg = "";
                for(int i=0; i<=arr.length-1; i++)
                {
                    msg = msg+" "+arr[i];
                }
                displaybox.setText(msg);
            }
        });
        display.setFont(new Font("Courier 10 Pitch", Font.BOLD, 12));
        display.setBounds(216, 245, 130, 25);
        contentPane.add(display);
        
        displaybox = new JTextField();
        displaybox.setForeground(new Color(0, 0, 0));
        displaybox.setBackground(new Color(238, 238, 236));
        displaybox.setBounds(175, 282, 218, 19);
        contentPane.add(displaybox);
        displaybox.setColumns(10);
        
        JButton home = new JButton("HOME");
        home.setFont(new Font("Courier 10 Pitch", Font.BOLD, 12));
        home.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(开发者_Go百科false);
                DataStructures dataStructures = new DataStructures();
                dataStructures.setVisible(true);
                //this.toBack();
                //setVisible(false);
                //new DataStructures().toFront();
                //new DataStructures().setState(java.awt.Frame.NORMAL);
            }
    
        });
        home.setBounds(242, 313, 77, 17);
        contentPane.add(home);
        }

    private Object pane() {
        return null;
    }
}

*

0

精彩评论

暂无评论...
验证码 换一张
取 消