实验10

 

import java . awt . * ; 
import javax . swing . * ; 

public class JFramedemo extends JFrame {
private Box box 
; 
private Container container 
; 
private JTextField 
text ; 
private JPanel panel1 
, panel2 ; 
private String S1 
[ ] = { "Backspace" , "CE" , "C" } ; //将计算器上显示的内容存在数组里面 
private String S2 
[ ] = { "1" , "2" , "3" , "+" , "sqrt" , "4", "5" , "6" , "-" , "%" , "7" , "8" , "9" , "*" , "1/x" , "0" ,"+/-" , "." , "/" , "=" } ; 
public JFramedemo ( ) {
this 
. setTitle ( "计算器显示界面" ) ; 
this 
. setBounds ( 300 , 300 , 500 , 500 ) ; 
container 
= getContentPane ( ) ; 
container 
. setLayout ( new FlowLayout ( ) ) ; //将内容窗格设置成流式布局 
text = new JTextField ( 15 ) ; 
box 
= Box . createVerticalBox ( ) ; 
panel1 
= new JPanel ( new GridLayout ( 1 , 3 , 20 , 0 )) ; 
//panel1.setLayout(new GridLayout(1,3,10,0)); 
panel2 
= new JPanel ( ) ; 
panel2 
. setLayout ( new GridLayout ( 4 , 5 , 10 , 10 ) ); 
for ( int i = 0 ; i < 3 ; i + + ) //将数组S1的内容加到panel1的Button按钮上 
{
panel1 
. add ( new JButton ( S1 [ i ] ) ) ; 
}
for ( int j = 0 ; j < 20 ; j + + ) //将数组S2的内容加到panel2的Button按钮上 
{
panel2 
. add ( new JButton ( S2 [ j ] ) ) ; 
}
box 
. add ( text ) ; 
box 
. add ( Box . createVerticalStrut ( 15 ) ) ; 
box 
. add ( panel1 ) ; 
box 
. add ( Box . createVerticalStrut ( 25 ) ) ; 
box 
. add ( panel2 ) ; 
container 
. add ( box ) ; 
pack 
( ) ; //调整窗口的大小,使其适应组件的大小和布局 
setVisible 
( true ) ; 
this 
. setDefaultCloseOperation ( EXIT_ON_CLOSE ) ; 
}
public static void main ( String [ ] args ) {
new JFramedemo 
( ) ; 
}
}

心得:

1在编写计算器的过程中要注意包的使用

2注意add方法的使用

posted on 2019-06-02 19:22  越活越酷  阅读(171)  评论(0)    收藏  举报

导航