仲恺信计程序设计实践课程设计代码参考

此代码示例展示了如何使用Java编程来完成两个任务:一是显示21世纪(2001-2100年)的所有闰年;二是创建一个用户界面,用户可以输入年份和起始星期,程序会根据输入显示该年的日历。程序包括了闰年判断和日历布局的逻辑,以及图形界面的交互设计。
摘要由CSDN通过智能技术生成

1、显示闰年

编写程序,显示21世纪(2001年到2100年)所有的闰年,每行10个。

/**
 * @author Carrot
 */
public class LeapYearTest {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 2001; i < 2100; i++) {
            if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
                count++;
                if (count % 10 == 0)
                    System.out.println(i);
                 else
                    System.out.print(i + "  ");
            }
        }
    }
}

 

2、显示日历

编写程序,提示用户输入年份和该年第一天的星期,在控制台上显示该年的日历表。例如,用户输入2010年,第一天是星期5。

/**
 * @author Carrot
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//设置窗口
public class CalendarTest extends JFrame{
    public CalendarTest() {
        //添加面板
        add(new CalendarPanel());

        //设置窗口属性
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);//设置窗口大小不可改变
        setLocationRelativeTo(null);
        setSize(200,150);
    }

    public static void main(String[] args) {
        new CalendarTest();
    }
}

//设置面板
class CalendarPanel extends JPanel {

    JTextField T1;
    JTextField T2;
    JButton bt1;

    public CalendarPanel() {
        init();
        event();
    }

    //设计面板
    void init() {

        //设置输入年份面板
        JLabel L1 = new JLabel("  请输入年份");
        T1 = new JTextField(20);
        JPanel p1 = new JPanel(new GridLayout(2, 1));
        p1.add(L1);
        p1.add(T1);

        //设置输入第一天的面板
        JLabel L2 = new JLabel("  该年第一天的星期为");
        T2 = new JTextField(20);
        JPanel p2 = new JPanel(new GridLayout(2, 1));
        p2.add(L2);
        p2.add(T2);

        //设置数据确认的面板
        bt1 = new JButton("确定并查看");
        JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        p3.add(bt1);

        //添加组件
        setLayout(new GridLayout(3, 1));
        add(p1);
        add(p2);
        add(p3);
    }

    //添加事件
    void event() {
        //按确定键时发生的事件
        bt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new DataProcessing(T1.getText(), T2.getText());
                } catch (NumberFormatException numberFormatException) {
                    new EDialog();
                }
            }
        });
    }
}

//数据处理
class DataProcessing {
    int year;//表示输入的年份
    int CorrespondingWeek;//标表示每月的星期几

    public static boolean isZero = false;//判断面板出入的数值是否出错
    public DataProcessing(String yearText, String firstDayText) throws NumberFormatException{
        year = Integer.valueOf(yearText);
        CorrespondingWeek = Integer.valueOf(firstDayText);
        if (year != 0 && CorrespondingWeek > 0 && CorrespondingWeek < 8)
            calculate();
        else {
            isZero = true;
            new EDialog();
        }
    }

    void calculate() {
        //设置所有的月份名称和星期名称
        String[] months = {"January","February","March","April","May","June",
                "July","August","September","October","November","December"};
        String[] Week = {"MON","TUE","WED","THU","FRI","SAT","SUN"};

        for (int month = 0; month < months.length; month++) {
            int day = 1;//表示月份的第几天

            //打印月份、年份以及分割线
            System.out.println("\t\t" + months[month] + " " + year);
            System.out.println("---------------------------");

            //打印星期
            System.out.print(Week[6] + " ");
            for (int week = 1; week < Week.length; week++)
                System.out.print(Week[week] + " ");
            System.out.println();

            int count = 0;//表示该月份的天数
            //判断月内日期结束数字
            if (month == 0 || month == 2 || month == 4 || month == 6
                    || month == 7 || month == 9 || month == 11) {
                count = 32;
            } else if (month == 1) {
                //判断是否为闰年
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    count = 29;
                else
                    count = 30;
            } else {
                count = 31;
            }

            int place = 0;//表示月份第一天的位置

            //打印日期
            while (day != count) {
                if (day == 1) {
                    place = 4 * (CorrespondingWeek - 1);
                    String str = "";
                    for (int k = 0; k < place; k++)
                        str += " ";
                    System.out.print(str + day);
                    day++;
                    if (CorrespondingWeek == 7)
                        System.out.println();
                    CorrespondingWeek = (1 + CorrespondingWeek) % 8;
                    if (CorrespondingWeek == 0)
                        CorrespondingWeek = 1;
                } else if (day <= 10 && day > 1) {
                    if (CorrespondingWeek != 1)
                        System.out.print("   " + day);
                    else
                        System.out.print(day);
                    day++;
                    if (CorrespondingWeek == 7)
                        System.out.println();
                    CorrespondingWeek = (1 + CorrespondingWeek) % 8;
                    if (CorrespondingWeek == 0)
                        CorrespondingWeek = 1;
                } else {
                    if (CorrespondingWeek != 1)
                        System.out.print("  " + day);
                    else
                        System.out.print(day);
                    day++;
                    if (CorrespondingWeek == 7)
                        System.out.println();
                    CorrespondingWeek = (CorrespondingWeek +1) % 8;
                    if (CorrespondingWeek == 0)
                        CorrespondingWeek = 1;
                }
            }
            System.out.println("\n");
        }
    }
}

//设置弹窗(异常处理)
class EDialog extends JDialog {
    public EDialog() {
        setVisible(true);
        setLocationRelativeTo(null);
        setSize(250,100);

        Container container = this.getContentPane();
        container.setLayout(new FlowLayout(FlowLayout.CENTER));
        if (DataProcessing.isZero)
            container.add(new JLabel("数据存在数值0,请重新输入"));
        else
            container.add(new JLabel("非有效数据,请重新输入"));
    }
}

3、显示一个饼图

编写程序,使用饼图显示作业、平时测验、期中考试和期末考试占总成绩的百分比,如下图所示。假设作业占20%用红色显示,平时测验占10%用蓝色显示,期中考试占30%用绿色显示,期末考试占40%用白色显示。

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

/**
 * @author Carrot
 */
public class PiePanel extends JPanel{

    PiePanel() {
        setLayout(null);

        //设置各区域的标签
        JLabel lr = new JLabel("Projects--20%");
        lr.setBounds(300,100,100,100);

        JLabel lb = new JLabel("Quizzes--10%");
        lb.setBounds(190,25,100,100);

        JLabel lg = new JLabel("Midterms--30%");
        lg.setBounds(75,150,100,100);

        JLabel lw = new JLabel("Final--40%");
        lw.setBounds(250,300,100,100);

        add(lr);
        add(lb);
        add(lg);
        add(lw);
    }

    //画饼图
    @Override
    protected void paintComponent(Graphics g) {
        //画出红色区域
        g.setColor(Color.RED);
        g.fillArc(5,5,450,450,0,72);
        //画出蓝色区域
        g.setColor(Color.BLUE);
        g.fillArc(5,5,450,450,72,36);
        //画出绿色区域
        g.setColor(Color.GREEN);
        g.fillArc(5, 5, 450, 450, 108, 108);
        //画出白色区域
        g.setColor(Color.WHITE);
        g.fillArc(5,5,450,450,216,144);
    }
}

5、转动的风扇

编写程序,模拟一个转动的风扇图形。可以使用定时器实现风扇图形的转动。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

/**
 * @author Carrot
 */
public class FanTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new FanPanel());
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setBounds(750,250,410,600);
    }
}

class FanPanel extends JPanel implements ActionListener {
    Timer timer = new Timer(100,this);//设置定时器

    int angle;//设置扇叶的度数

    //设置具体颜色的数值
    int redCount = 0;
    int greenCount = 0;
    int blueCount = 0;
    //设置颜色
    Color color;

    //设置扇叶颜色的组件
    JLabel labelColor;
    JButton red;
    JButton green;
    JButton blue;
    JScrollBar scrollBarRed;
    JScrollBar scrollBarGreen;
    JScrollBar scrollBarBlue;

    //设置控制速度的组件
    JLabel labelSpeed;
    JButton low;
    JButton medium;
    JButton high;
    JScrollBar scrollBarSpeed;//自定义速度


    FanPanel() {
        setLayout(null);

        //设置颜色组件的属性
        labelColor = new JLabel("颜色设置");
        labelColor.setBounds(50,240,100,100);
        red = new JButton("红色");
        red.setBounds(50,300,100,50);
        green = new JButton("绿色");
        green.setBounds(150,300,100,50);
        blue = new JButton("蓝色");
        blue.setBounds(250,300,100,50);
        scrollBarRed = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 255);
        scrollBarRed.setBounds(50,350,100,25);
        scrollBarGreen = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 255);
        scrollBarGreen.setBounds(150,350,100,25);
        scrollBarBlue = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 255);
        scrollBarBlue.setBounds(250,350,100,25);

        //设置速度组件的属性
        labelSpeed = new JLabel("速度设置");
        labelSpeed.setBounds(50,390,100,100);
        low = new JButton("低速");
        low.setBounds(50,450,100,50);
        medium = new JButton("中速");
        medium.setBounds(150,450,100,50);
        high = new JButton("高速");
        high.setBounds(250,450,100,50);
        scrollBarSpeed = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 100);
        scrollBarSpeed.setBounds(50,500,300,25);

        add(labelColor);add(red);add(green);add(blue);
        add(scrollBarRed);add(scrollBarGreen);add(scrollBarBlue);
        add(labelSpeed);add(low);add(medium);add(high);add(scrollBarSpeed);

        timer.start();
        event();
    }

    void event() {
        /**
         *  改变扇叶的颜色
         */
        red.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                redCount = 255;greenCount = 0;blueCount = 0;
                scrollBarRed.setValue(255);
                scrollBarGreen.setValue(0);
                scrollBarBlue.setValue(0);
            }
        });
        green.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                redCount = 0;greenCount = 255;blueCount = 0;
                scrollBarRed.setValue(0);
                scrollBarGreen.setValue(255);
                scrollBarBlue.setValue(0);
            }
        });
        blue.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                redCount = 0;greenCount = 0;blueCount = 255;
                scrollBarRed.setValue(0);
                scrollBarGreen.setValue(0);
                scrollBarBlue.setValue(255);
            }
        });
        scrollBarRed.addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                redCount = e.getValue();
            }
        });
        scrollBarGreen.addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                greenCount = e.getValue();
            }
        });
        scrollBarBlue.addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                blueCount = e.getValue();
            }
        });


        /**
         *  给速度组件添加事件,达到变速效果
         */
        low.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TimeCtrl(100);
                scrollBarSpeed.setValue(0);
            }
        });
        medium.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TimeCtrl(50);
                scrollBarSpeed.setValue(50);
            }
        });
        high.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TimeCtrl(0);
                scrollBarSpeed.setValue(100);
            }
        });
        scrollBarSpeed.addAdjustmentListener(new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                TimeCtrl(100 - e.getValue());
            }
        });

    }

    //控制定时器
    void TimeCtrl(int delay) {
        timer.stop();
        timer = new Timer(delay,this);
        timer.start();
    }

    /**
     *  画风扇
     * @param g
     */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        color = new Color(redCount,greenCount,blueCount);

        g.drawArc(95,45,210,210,0,360);

        g.setColor(color);
        g.fillArc(100,50,200,200,angle,30);
        g.fillArc(100,50,200,200,angle + 90,30);
        g.fillArc(100,50,200,200,angle + 180,30);
        g.fillArc(100,50,200,200,angle + 270,30);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        angle += 100;
        timer.start();
        repaint();
    }
}

6、汽车运动演示(20分)

编写程序,模拟汽车运动。汽车从左向右移动。当它到达右终点,就从左边重新开始,然后继续同样的过程。可以使用定时器控制动画。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Carrot
 */

public class CarTest {
    public static void main(String[] args) {
        //设置窗口
        JFrame frame = new JFrame();
        frame.add(new CarPanel());
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);//设置窗口不可改变大小
        frame.setBounds(100,100,1000,550);
        frame.setVisible(true);
    }
}

class CarPanel extends JPanel implements ActionListener {
    //小车的位置坐标
    int x1 = 0;
    int y1 = 100;
    int x2 = -400;
    int y2 = 300;

    //小车的窗口设计坐标
    int[] windowsX = new int[4];
    int[] windowsY = new int[4];

    Timer timer;

    int color1 = 60;
    int color2 = 40;



    CarPanel() {
        setBackground(Color.BLACK);
        timer = new Timer(1, this);
        timer.start();


    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        /**
         *  设计马路
         */
        g.setColor(Color.white);
        for (int i = 0; i < 1000; i += 50)
            g.drawLine(i,240,i + 25,240);
        g.drawLine(0,50,1000,50);
        g.drawLine(0,40,1000,40);
        g.drawLine(0,450,1000,450);
        g.drawLine(0,440,1000,440);



        /**
         *  设计汽车的模型
         */
        //第一车道
        g.setColor(new Color(140, color1, 60));
        g.fillRoundRect(x1, y1,150,75,50,50);
        g.setColor(new Color(180, color1 + 25, 85));
        g.fillRoundRect(x1 + 110, y1 + 12,33,50,75,75);
        g.fillRect(x1 + 110, y1 + 12,20,50);
        g.setColor(new Color(190, color1 + 40, 100));
        g.fillRect(x1 + 30, y1 + 25,50,25);
         windowsX[0] = x1 + 20;windowsY[0] = y1 + 12;
         windowsX[1] = x1 + 100;windowsY[1] = y1 + 12;
         windowsX[2] = x1 + 80;windowsY[2] = y1 + 22;
         windowsX[3] = x1 + 30;windowsY[3] = y1 + 22;
         g.fillPolygon(windowsX,windowsY,4);
        windowsX[0] = x1 + 20;windowsY[0] = y1 + 12 + 50;
        windowsX[1] = x1 + 100;windowsY[1] = y1 + 12 + 50;
        windowsX[2] = x1 + 80;windowsY[2] = y1 + 22 + 30;
        windowsX[3] = x1 + 30;windowsY[3] = y1 + 22 + 30;
        g.fillPolygon(windowsX,windowsY,4);
        windowsX[0] = x1 + 15;windowsY[0] = y1 + 13;
        windowsX[1] = x1 + 15;windowsY[1] = y1 + 61;
        windowsX[2] = x1 + 25;windowsY[2] = y1 + 51;
        windowsX[3] = x1 + 25;windowsY[3] = y1 + 23;
        g.fillPolygon(windowsX,windowsY,4);
        windowsX[0] = x1 + 15 + 90;windowsY[0] = y1 + 13;
        windowsX[1] = x1 + 15 + 90;windowsY[1] = y1 + 61;
        windowsX[2] = x1 + 30 + 60;windowsY[2] = y1 + 51;
        windowsX[3] = x1 + 30 + 60;windowsY[3] = y1 + 23;
        g.fillPolygon(windowsX,windowsY,4);

        //第二车道
        g.setColor(new Color(140, color2, 60));
        g.fillRoundRect(x2, y2,150,75,50,50);
        g.setColor(new Color(180, color2 + 25, 85));
        g.fillRoundRect(x2 + 110, y2 + 12,33,50,75,75);
        g.fillRect(x2 + 110, y2 + 12,20,50);
        g.setColor(new Color(190, color2 + 40, 100));
        g.fillRect(x2 + 30, y2 + 25,50,25);
        windowsX[0] = x2 + 20;windowsY[0] = y2 + 12;
        windowsX[1] = x2 + 100;windowsY[1] = y2 + 12;
        windowsX[2] = x2 + 80;windowsY[2] = y2 + 22;
        windowsX[3] = x2 + 30;windowsY[3] = y2 + 22;
        g.fillPolygon(windowsX,windowsY,4);
        windowsX[0] = x2 + 20;windowsY[0] = y2 + 12 + 50;
        windowsX[1] = x2 + 100;windowsY[1] = y2 + 12 + 50;
        windowsX[2] = x2 + 80;windowsY[2] = y2 + 22 + 30;
        windowsX[3] = x2 + 30;windowsY[3] = y2 + 22 + 30;
        g.fillPolygon(windowsX,windowsY,4);
        windowsX[0] = x2 + 15;windowsY[0] = y2 + 13;
        windowsX[1] = x2 + 15;windowsY[1] = y2 + 61;
        windowsX[2] = x2 + 25;windowsY[2] = y2 + 51;
        windowsX[3] = x2 + 25;windowsY[3] = y2 + 23;
        g.fillPolygon(windowsX,windowsY,4);
        windowsX[0] = x2 + 15 + 90;windowsY[0] = y2 + 13;
        windowsX[1] = x2 + 15 + 90;windowsY[1] = y2 + 61;
        windowsX[2] = x2 + 30 + 60;windowsY[2] = y2 + 51;
        windowsX[3] = x2 + 30 + 60;windowsY[3] = y2 + 23;
        g.fillPolygon(windowsX,windowsY,4);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        //两个车道上的车的正常运动
        if (x1 >= 1000) {
            x1 = -150;
            color1 = (int)(180 * Math.random() + 20);
        } else {
            x1 += 5;
        }
        if (x2 >= 1000) {
            x2 = -150;
            color2 = (int)(180 * Math.random() + 20);
        }
        else {
            x2+=8;
        }

        repaint();
    }
}

7、交通信号灯(20分)

编写程序,模拟交通信号灯。程序让用户从红、黄、绿三色灯中选择一种。当选择一个单选按钮后,相应的灯被打开,并且一次只能亮一种灯。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Carrot
 */

public class TrafficLightsTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new TrafficLightsPanel());
        frame.setBounds(500,250,500,600);
        frame.setVisible(true);
    }
}



class TrafficLightsPanel extends JPanel implements ActionListener {
    //设置三色单选按钮
    JRadioButton radioButton_red;
    JRadioButton radioButton_yellow;
    JRadioButton radioButton_green;

    TrafficLightsPanel() {
        setLayout(null);

        //设计单选按钮属性
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        radioButton_red = new JRadioButton("Red",false);
        radioButton_yellow = new JRadioButton("Yellow",false);
        radioButton_green = new JRadioButton("Green",false);

        //将单选按钮组合在一起
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton_red);
        buttonGroup.add(radioButton_yellow);
        buttonGroup.add(radioButton_green);

        //添加事件监听
        radioButton_red.addActionListener(this);
        radioButton_yellow.addActionListener(this);
        radioButton_green.addActionListener(this);

        //添加面板
        panel.add(radioButton_red);
        panel.add(radioButton_yellow);
        panel.add(radioButton_green);
        panel.setBounds(0,500,500,50);
        add(panel);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        //设计红绿灯未亮的模型
        g.setColor(Color.BLACK);
        g.fillRoundRect(180,40,140,400,100,100);
        g.drawRoundRect(175,35,150,410,100,100);
        g.setColor(new Color(80, 0, 0));
        g.fillOval(190,50,120,120);
        g.setColor(new Color(135, 135, 14));
        g.fillOval(190,180,120,120);
        g.setColor(new Color(0, 80, 0));
        g.fillOval(190,310,120,120);

        //设置让红绿灯亮起来
        if (radioButton_red.isSelected() == true) {
            g.setColor(Color.RED);
            g.fillOval(190,50,120,120);
            g.setColor(new Color(135, 135, 14));
            g.fillOval(190,180,120,120);
            g.setColor(new Color(0, 80, 0));
            g.fillOval(190,310,120,120);
        } else if (radioButton_yellow.isSelected() == true) {
            g.setColor(new Color(80, 0, 0));
            g.fillOval(190,50,120,120);
            g.setColor(Color.yellow);
            g.fillOval(190,180,120,120);
            g.setColor(new Color(0, 80, 0));
            g.fillOval(190,310,120,120);
        } else if (radioButton_green.isSelected() == true) {
            g.setColor(new Color(80, 0, 0));
            g.fillOval(190,50,120,120);
            g.setColor(new Color(135, 135, 14));
            g.fillOval(190,180,120,120);
            g.setColor(Color.GREEN);
            g.fillOval(190,310,120,120);
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (radioButton_red.isSelected()|| radioButton_yellow.isSelected()
                || radioButton_green.isSelected())
            repaint();
    }
}

以上的代码存在部分对原题目的创新,仅供学习和参考!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值