📄 exercise15_2.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -