java GUI初试

学面向对象,以java为设计语言,虽然老师不是讲java但是还是把java的基础知识大概讲了一下,速度当然很快,很少动手实践,这是自己抽时间照着课本做的图像化界面计算圆的周长面积。

/*
 *功能:测试GUI的使用,用户输入圆的半径,然后计算
 *时间:2012.11.3
 *作者:KDF5000
 *文件名:TestGui.java
 */
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import java.text.*;
public class TestGui {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new CalculateDomo();
    }

}

class CalculateDomo extends JFrame implements ActionListener
{
    Circle theCircle;
    JTextField messageText;
    JTextArea resultArea;
    JButton calButton;
    JButton closeButton;
    JButton clearButton;
    
    public CalculateDomo()
    {
        super("计算圆的面积和周长");
        
        Container c=this.getContentPane();
        c.setLayout(new GridLayout(2,1));
        JPanel centerPanel=new JPanel(new FlowLayout());
        
        messageText =new JTextField(5);
        JLabel messageLabel=new JLabel("请输入圆的半径: ");
        calButton =new JButton("计算");
        closeButton =new JButton("关闭窗口");
        resultArea =new JTextArea("计算结果:",4,20);
        
        centerPanel.add(messageLabel);
        centerPanel.add(messageText);
        centerPanel.add(calButton);
        centerPanel.add(closeButton);
        c.add(centerPanel);
        c.add(resultArea);
        
        //注册事件到容器
        calButton.addActionListener(this);
        closeButton.addActionListener(this);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(360,200);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==calButton)
            calculate();
        if(e.getSource()==closeButton)
            shutDown();
    }
    
    private void calculate()
    {
        String message=messageText.getText();
        if(message.length()==0)
        {
            JOptionPane.showMessageDialog(null,"请输入半径!","提示",1);    
        }
        else
        {
            int radius=Integer.parseInt(message);
            theCircle=new Circle(radius);
            int theRadius=theCircle.getRadius();
            double girth=theCircle.calGirth();
            double area=theCircle.calArea();
            
            resultArea.setText("计算结果如下:");
            resultArea.append("\n圆的半径是:"+theRadius);
            resultArea.append("\n圆的周长是:"+new DecimalFormat("#.00").format(girth));
            resultArea.append("\n圆的面积是:"+new DecimalFormat("#.00").format(area));
        }
    }
    
    public void shutDown()
    {
        System.exit(0);
    }
    
}


//圆类
class Circle
{
    private int radius;
    
    public Circle(int radius)
    {
        this.radius=radius;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }
    
    public double calArea()
    {
        return 3.14159*(this.radius^2);
    }
    
    public double calGirth()
    {
        return 2*3.14159*this.radius;
    }
}

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0