当前位置:编程学习 > JAVA >>

java小实验

[java]
package Second; 
import java.awt.*; 
import javax.swing.*; 
import java.math.*; 
import java.awt.event.*; 
public class test1 implements ActionListener{ 
 
    /**
     * @param args
     */       www.zzzyk.com
      public JTextField s=new JTextField(13); 
      public JTextField s1=new JTextField(13); 
      public JLabel l1=new JLabel("平方"); 
      public JLabel l2=new JLabel("立方"); 
      public JLabel l3=new JLabel(" 四次方"); 
      public JButton b1=new JButton("平方结果"); 
      public JButton b2=new JButton("立方结果"); 
      public JButton b3=new JButton("四次方根"); 
    void display() 
    { 
       
      JPanel panel=new JPanel(new GridLayout(4,2)); 
      JFrame jf=new JFrame("测试程序"); 
      jf.setSize(300,200); 
      jf.setContentPane(panel); 
      jf.setVisible(true); 
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      jf.setResizable(false); 
      panel.add(s); 
      panel.add(s1); 
      panel.add(l1); 
      panel.add(b1); 
      panel.add(l2); 
      panel.add(b2); 
      panel.add(l3); 
      panel.add(b3); 
      b1.addActionListener(this); 
      b2.addActionListener(this); 
      b3.addActionListener(this); 
    } 
    public void actionPerformed(ActionEvent e){ 
         
        if(e.getSource()==b1) 
        { 
         double a=Double.parseDouble(s.getText()); 
         a=a*a; 
         s1.setText(""+a); 
        } 
        else if(e.getSource()==b2) 
        { 
             double a=Double.parseDouble(s.getText()); 
             a=a*a*a; 
             s1.setText(""+a); 
             
        } 
        else if(e.getSource()==b3) 
        { 
            double a=Double.parseDouble(s.getText()); 
            a=a*a*a*a; 
            s1.setText(""+a); 
        } 
    } 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
          test1 m=new test1(); 
          m.display(); 
    } 
 

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,