java-窗口程序-circle

import com.sun.awt.AWTUtilities;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Circle {

    static int DIV_NUM = 0;

    public static void main(String[] args) {
        int diam = 800;
        if (args != null && args.length >= 1) {
            try {
                diam = Integer.parseInt(args[0]);
            } catch (Exception e) {

            }
        }
        int divNum = 16;
        if (args != null && args.length >= 2) {
            try {
                divNum = Integer.parseInt(args[1]);
            } catch (Exception e) {
            }
        }
        if (divNum % 2 != 0) {
            return;
        }
        DIV_NUM = divNum;
        JFrame jf = new JFrame();
        RoundRectPanel jp = new RoundRectPanel(jf);
        jp.setLayout(null);
        jp.setOpaque(false);//Panel设置为透明
        jp.setBorder(new RoundRectBorder(Color.WHITE));

        jf.add(jp);
        jf.setUndecorated(true);//去掉标题栏
        jf.setBounds(300, 200, diam, diam);
        AWTUtilities.setWindowOpaque(jf, false);//JFrame设置为透明
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
        jf.setLocationRelativeTo(null);
        ReSizeEvent dg = new ReSizeEvent(jf);
        /**添加两个监听器**/
        jf.addMouseListener(dg);
        jf.addMouseMotionListener(dg);
        jf.setAlwaysOnTop(true);
    }
}

class RoundRectPanel extends JPanel {

    private JFrame jf;

    public RoundRectPanel(JFrame jf) {
        super();
        this.jf = jf;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.WHITE);
        BasicStroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 10.0f, new float[] {10, 10}, 0);
        g2d.setStroke(stroke);
        int diam = jf.getWidth();
        int div = Circle.DIV_NUM / 2;
        int length = diam / Circle.DIV_NUM;
        int radius = diam / 2;

        //横线
        for (int i = 1; i < div; i++) {
            int divLength = cal(radius, (div - i) * length);

            int[] topLeft = new int[] {radius - divLength, length * i};
            int[] topRight = new int[] {radius + divLength, length * i};
            int[] 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里提供一个简单的 Java 窗口程序来计算圆的面积和周长: ```java import javax.swing.*; import java.awt.event.*; public class CircleCalculator extends JFrame implements ActionListener { private JTextField radiusField; private JLabel areaLabel, circumferenceLabel; public CircleCalculator() { setTitle("Circle Calculator"); setSize(300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置 UI 布局为 FlowLayout setLayout(new FlowLayout()); // 添加半径输入框和标签 JLabel radiusLabel = new JLabel("Radius:"); add(radiusLabel); radiusField = new JTextField(10); add(radiusField); // 添加计算按钮 JButton calculateButton = new JButton("Calculate"); add(calculateButton); calculateButton.addActionListener(this); // 添加面积和周长标签 areaLabel = new JLabel("Area: "); add(areaLabel); circumferenceLabel = new JLabel("Circumference: "); add(circumferenceLabel); } public void actionPerformed(ActionEvent e) { // 获取输入的半径 double radius = Double.parseDouble(radiusField.getText()); // 计算面积和周长 double area = Math.PI * radius * radius; double circumference = 2 * Math.PI * radius; // 更新面积和周长标签 areaLabel.setText("Area: " + area); circumferenceLabel.setText("Circumference: " + circumference); } public static void main(String[] args) { CircleCalculator calculator = new CircleCalculator(); calculator.setVisible(true); } } ``` 上述代码使用了 Java 的 Swing 组件来构建窗口 UI,其中包含一个半径输入框、一个计算按钮和两个标签来显示圆的面积和周长。在点击计算按钮后,程序会获取输入框中的半径并计算出对应的面积和周长,最后更新标签的显示内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值