Import Java . AWT . * ; Import the javax . Swing . * ; public class JFramedemo the extends the JFrame { Private Box Box ; Private Container Container ; Private the JTextField text ; Private the JPanel Panel1 , Panel2 ; Private String Sl [ ] = { "the Backspace" , " the CE " , " C " } ; // there will be an array of content displayed on the calculator inside Private S2 String [ ]
= { "1" , "2" , "3" , "+" , "sqrt" , "4", "5" , "6" , "-" , "%" , "7" , "8" , "9" , "*" , "1/x" , "0" ,"+/-" , "." , "/" , "=" } ;
public JFramedemo ( ) {
this . setTitle ( "Calculator display interface" ) ;
the this . The setBounds ( 300 , 300 , 500 , 500 ) ;
Container = the getContentPane ( ) ;
Container . SetLayout ( new new the FlowLayout ( ) ) ; // set streaming content pane layout text = the JTextField new new ( 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 + + ) on the contents of the array S1 // added to the Button button panel1
{
panel1 . the Add ( new new the JButton ( S1 [ I ] ) ) ;
}
for ( int J = 0 ; J < 20 is ; J + + ) / / S2 the content added to the array of button button panel2
{
panel2 . the Add ( new new the 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 ( ) ; // resize the window, to adapt the size and layout of the component
setVisible ( to true ) ;
the this . SetDefaultCloseOperation ( EXIT_ON_CLOSE ) ;
}
public static void main ( String [ ] args ) {
new new JFramedemo ( ) ;
}
}
Experience:
1 in the preparation of the calculator to pay attention to the use of the package
Note that a method using two add