exercise15_2.java

来自「一款用java编写的小型数据库管理系统」· Java 代码 · 共 52 行

JAVA
52
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exercise15_2 extends JFrame{
     private JLabel jlIndex=new JLabel("Array Index");
     private JLabel jlElement=new JLabel("Array Element");
     private JTextField jtIndex=new JTextField(15);
     private JTextField jtElement=new JTextField(15);
     private JButton jb=new JButton("Show Element");
     private JPanel jp1=new JPanel();
     private JPanel jp2=new JPanel();
     private int [] num=new int[100];
     private int Index;
     public Exercise15_2(){
    	 jp1.setLayout(new GridLayout(1,1));
    	 jp1.add(jb);
    	 
    	 jp2.setLayout(new GridLayout(2,2));
    	 jp2.add(jlIndex);
    	 jp2.add(jtIndex);
    	 jp2.add(jlElement);
        jtElement.setEditable(false);
	 jp2.add(jtElement);
    	 
    	 add(jp2,BorderLayout.NORTH);
    	 add(jp1,BorderLayout.SOUTH);
    	 
    	 jb.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			Index=Integer.parseInt(jtIndex.getText());
    			try{
    			for(int i=0;i<100;i++){
    				num[i]=(int)(Math.random()*100);
    			}
    			jtElement.setText(""+num[Index]);
    			}
    			catch (IndexOutOfBoundsException ex){
    				jtElement.setText("Out of Bound");
    			}
    		}
    	 });
     }
     public static void main(String[]ar){
    	 Exercise15_2 frame=new Exercise15_2();
    	 frame.setTitle("Exercise15_2");
    	 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	 frame.setLocationRelativeTo(null);
    	 frame.pack();
    	 frame.setVisible(true);
     }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?