java课程设计题目及代码是什么?

Python022

java课程设计题目及代码是什么?,第1张

java课程设计题目及代码分别是:

1、题目:计算器。设计内容是设计一个图形界面(GUI)的计算器应用程序,完成简单的算术运算。

设计要求是设计的计算器应用程序可以完成家法、减法、乘法、除法和取余运算。且有小数点、正负号、求倒数、退格和清零功能。

2、代码:

数字按钮NumberButton类如下:

import java.awt.

import java.awt.event.

import javax.swing.

public class NumberButton extends Button.

{

int number.

public NumberButton(int number).

 {

super(""+number).

this.number=number.

setForeground(Color.blue).

}

public int getNumber().

{

return number

}

}

其它java课程设计题目及代码是:

题目:华容道编写一个按钮的子类,使用该子类创建的对象代表华容道中的人物。通过焦点事件控制人物颜色,当人物获得焦点时颜色为蓝色,当失去焦点时颜色为灰色。

通过键盘事件和鼠标事件来实现曹操、关羽等人物的移动。当人物上发生鼠标事件或键盘事件时,如果鼠标指针的位置是在人物的下方(也就是组件的下半部分)或按下键盘的“↓“键,该人物向下移动。向左、向右和向上的移动原理类似。

代码是:

String name[]={"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"}.

for(int i=0i<name.lengthi++).

{

person[i]=new Person(i,name[i]).

person[i].addKeyListener(this).

person[i].addMouseListener(this).

//     person[i].addFocusListener(new Person).

add(person[i]).

}

person[0].setBounds(104,54,100,100).

person[1].setBounds(104,154,100,50).

person[2].setBounds(54,154,50,100).

person[3].setBounds(204,154,50,100).

person[4].setBounds(54,54,50,100).

person[5].setBounds(204,54,50,100)

person[6].setBounds(54,254,50,50)

person[7].setBounds(204,254,50,50)

person[8].setBounds(104,204,50,50)

person[9].setBounds(154,204,50,50)

无聊写了个,修复了下Bug:

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JOptionPane

import javax.swing.JTextField

public class Calculate extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L

    private JButton plus, reduce, multiply, divice, reset

    private JTextField one, two, result

    private boolean device_falg = false

    private final int width = 400, height = 300

    public Calculate() {

        super("修改密码")

        this.setLayout(null)

        this.setSize(width, height)

        init()

        Layout()

    }

    public void init() {

        plus = new JButton("加   ")

        reduce = new JButton("减    ")

        multiply = new JButton("乘   ")

        divice = new JButton("除    ")

        reset = new JButton("清空")

        one = new JTextField()

        two = new JTextField()

        result = new JTextField()

    }

    public void Layout() {

        this.add(new JLabel("第一个数")).setBounds(20, 10, 60, 80)

        this.add(one).setBounds(100, 38, 100, 25)

        this.add(new JLabel("第二个数")).setBounds(20, 40, 60, 80)

        this.add(two).setBounds(100, 70, 100, 25)

        this.add(new JLabel("结果")).setBounds(20, 85, 60, 80)

        this.add(result).setBounds(100, 110, 100, 25)

        this.add(plus).setBounds(70, 170, 80, 25)

        this.add(reduce).setBounds(200, 170, 80, 25)

        this.add(multiply).setBounds(70, 200, 80, 25)

        this.add(divice).setBounds(200, 200, 80, 25)

        this.add(reset).setBounds(300, 220, 80, 25)

        plus.addActionListener(this)

        reduce.addActionListener(this)

        multiply.addActionListener(this)

        divice.addActionListener(this)

        reset.addActionListener(this)

        this.setVisible(true)

        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE)

    }

    public boolean Format() {

        boolean FLAG = false

        boolean flag = false

        String one = this.one.getText().toString().trim()

        String two = this.two.getText().toString().trim()

        if (one == null || one.equals("") || two == null || two.equals("")) {

            JOptionPane.showMessageDialog(getParent(), "请输入完整信息!")

            FLAG = false

            flag = true

        }

        boolean boll_1 = one.matches("[\\d]{1,100}")

        boolean boll_2 = two.matches("[\\d]{1,100}")

        boolean boll_3 = one.matches("[\\d]{1,100}+[.]+[\\d]{1,100}")

        boolean boll_4 = two.matches("[\\d]{1,100}+[.]+[\\d]{1,100}")

        if (flag) {

            return false

        }

        if ((boll_1 && boll_2) || (boll_3 && boll_4) || (boll_1 && boll_4)

                || (boll_3 && boll_2)) {

            FLAG = true

        } else {

            JOptionPane.showMessageDialog(getParent(), "请输入数字!")

            FLAG = false

        }

        if (FLAG && device_falg) {

            if (Double.parseDouble(two) == 0) {

                JOptionPane.showMessageDialog(getParent(), "被除数不能为0!")

                FLAG = false

                device_falg=false

            }

        }

        return FLAG

    }

    public double Plus(double one, double two) {

        return one + two

    }

    public double Multiply(double one, double two) {

        return one * two

    }

    public double Divice(double one, double two) {

        return one / two

    }

    public double Reduce(double one, double two) {

        return one - two

    }

    public void Clear() {

        one.setText("")

        two.setText("")

        result.setText("")

    }

    @Override

    public void actionPerformed(ActionEvent e) {

        Object o = e.getSource()

        if (o == reset) {

            Clear()

            return

        }

        if (o == divice) {

            device_falg = true

        }

        if (!Format()) {

            return

        }

        double one = Double.parseDouble(this.one.getText())

        double two = Double.parseDouble(this.two.getText())

        double result = 0

        if (o == plus) {

            result = Plus(one, two)

        } else if (o == reduce) {

            result = Reduce(one, two)

        } else if (o == multiply) {

            result = Multiply(one, two)

        } else if (o == divice) {

            result = Divice(one, two)

        }

        this.result.setText("" + result)

    }

    public static void main(String[] args) {

        new Calculate()

    }

}

采用java编写一个简单计算器,使用awt和swing

代码如下:

import java.awt.Color

import java.awt.Font

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JLabel

import javax.swing.JPanel

/*用java做一个计算器

* 实现整数和小数的加减乘除

* 具有删除和复位功能

*/

public class CaculatorDemo extends JFrame {

private static final long serialVersionUID = 1L

private StringBuilder sBuilder = new StringBuilder()

private Double a//中间变量用于存储输入的第一个数

private Double b//中间变量,用于存储输入的第二个数

private Double double1//用于接收计算结果

private Integer i// i用于表示加减乘除

public CaculatorDemo() {

this.setTitle("计算器")

this.setSize(318, 457)

this.setLocationRelativeTo(null)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

JPanel panel = new JPanel()

this.getContentPane().add(panel)

panel.setLayout(null)

// 定义一个label用于显示输入数据和计算结果,这里不用textfield,可以避免用户输入造成bug,简化程序开发

final JLabel label = new JLabel()

label.setBounds(0, 0, 300, 50)

label.setFont(new Font("dialog", 1, 30))

label.setOpaque(true)// 由于jlabel默认透明,直接设置背景色无效,需要先将不透明设置为true

label.setBackground(Color.white)

panel.add(label)

// 定义按钮组件

JButton button1 = new JButton("1")

JButton button2 = new JButton("2")

JButton button3 = new JButton("3")

JButton button4 = new JButton("4")

JButton button5 = new JButton("5")

JButton button6 = new JButton("6")

JButton button7 = new JButton("7")

JButton button8 = new JButton("8")

JButton button9 = new JButton("9")

JButton button0 = new JButton("0")

JButton buttonadd = new JButton("+")// 加

JButton buttonminus = new JButton("-")// 减

JButton buttontime = new JButton("×")// 乘

JButton buttondivid = new JButton("÷")// 除

JButton buttonequal = new JButton("=")// 等于

JButton buttondecimal = new JButton(".")// 小数点

JButton buttondelet = new JButton("←")// 删除

JButton buttonclear = new JButton("C")// 清除

// 定义按钮组件位置

button0.setBounds(0, 50, 100, 60)

button0.setFont(new Font("dialog", 1, 30))

panel.add(button0)

button1.setBounds(100, 50, 100, 60)

button1.setFont(new Font("dialog", 1, 30))

panel.add(button1)

button2.setBounds(200, 50, 100, 60)

button2.setFont(new Font("dialog", 1, 30))

panel.add(button2)

button3.setBounds(0, 110, 100, 60)

button3.setFont(new Font("dialog", 1, 30))

panel.add(button3)

button4.setBounds(100, 110, 100, 60)

button4.setFont(new Font("dialog", 1, 30))

panel.add(button4)

button5.setBounds(200, 110, 100, 60)

button5.setFont(new Font("dialog", 1, 30))

panel.add(button5)

button6.setBounds(0, 170, 100, 60)

button6.setFont(new Font("dialog", 1, 30))

panel.add(button6)

button7.setBounds(100, 170, 100, 60)

button7.setFont(new Font("dialog", 1, 30))

panel.add(button7)

button8.setBounds(200, 170, 100, 60)

button8.setFont(new Font("dialog", 1, 30))

panel.add(button8)

button9.setBounds(0, 230, 100, 60)

button9.setFont(new Font("dialog", 1, 30))

panel.add(button9)

buttonadd.setBounds(100, 230, 100, 60)

buttonadd.setFont(new Font("dialog", 1, 30))

panel.add(buttonadd)//加

buttonminus.setBounds(200, 230, 100, 60)

buttonminus.setFont(new Font("dialog", 1, 30))

panel.add(buttonminus)//减

buttontime.setBounds(0, 290, 100, 60)

buttontime.setFont(new Font("dialog", 1, 30))

panel.add(buttontime)//乘

buttondivid.setBounds(100, 290, 100, 60)

buttondivid.setFont(new Font("dialog", 1, 30))

panel.add(buttondivid)//除

buttonequal.setBounds(200, 290, 100, 60)

buttonequal.setFont(new Font("dialog", 1, 30))

panel.add(buttonequal)//等于

buttondecimal.setBounds(0, 350, 100, 60)

buttondecimal.setFont(new Font("dialog", 1, 30))

panel.add(buttondecimal)//小数点

buttonclear.setBounds(100, 350, 100, 60)

buttonclear.setFont(new Font("dialog", 1, 30))

panel.add(buttonclear)//复位

buttondelet.setBounds(200, 350, 100, 60)

buttondelet.setFont(new Font("dialog", 1, 30))

panel.add(buttondelet)//删除

// 给各个按钮设置动作监听器

// 输入数值操作0~9

button0.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append("0")

label.setText(sBuilder.toString())

}

})

button1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append("1")

label.setText(sBuilder.toString())

}

})

button2.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

sBuilder.append("2")

label.setText(sBuilder.toString())

}

})

button3.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append("3")

label.setText(sBuilder.toString())

}

})

button4.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append("4")

label.setText(sBuilder.toString())

}

})

button5.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append("5")

label.setText(sBuilder.toString())

}

})

button6.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append("6")

label.setText(sBuilder.toString())

}

})

button7.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append("7")

label.setText(sBuilder.toString())

}

})

button8.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append("8")

label.setText(sBuilder.toString())

}

})

button9.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append("9")

label.setText(sBuilder.toString())

}

})

// 输入运算符操作,需要先判断a是否为0.0

buttonadd.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

a = Double.parseDouble(sBuilder.toString())

sBuilder = new StringBuilder()

label.setText("+")

i = 0

}

})

buttonminus.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

a = Double.parseDouble(sBuilder.toString())

sBuilder = new StringBuilder()

label.setText("-")

i = 1

}

})

buttontime.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

a = Double.parseDouble(sBuilder.toString())

sBuilder = new StringBuilder()

label.setText("×")

i = 2

}

})

buttondivid.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

a = Double.parseDouble(sBuilder.toString())

sBuilder = new StringBuilder()

label.setText("÷")

i = 3

}

})

buttonequal.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// 该判断中间变量是否为空

if (!"".equals(sBuilder.toString()) &&(!(a == 0.0))) {

b = Double.parseDouble(sBuilder.toString())

if (i == 0) {

double1 = a + b

label.setText(double1.toString())

sBuilder = new StringBuilder()

sBuilder.append(double1)

} else if (i == 1) {

double1 = a - b

label.setText(double1.toString())

sBuilder = new StringBuilder()

sBuilder.append(double1)

} else if (i == 2) {

double1 = a * b

label.setText(double1.toString())

sBuilder = new StringBuilder()

sBuilder.append(double1)

} else if (i == 3) {

double1 = a / b

label.setText(double1.toString())

sBuilder = new StringBuilder()

sBuilder.append(double1)

} else {

label.setText(sBuilder.toString())

}

}

}

})

buttondecimal.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder.append(".")

label.setText(sBuilder.toString())

}

})

buttonclear.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

sBuilder = new StringBuilder()

label.setText("")

}

})

buttondelet.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (!"".equals(sBuilder.toString())) {

sBuilder.deleteCharAt(sBuilder.length() - 1)

label.setText(sBuilder.toString())

}

}

})

this.setVisible(true)

}

public static void main(String[] args) {

new CaculatorDemo()

}

}

————————————————

版权声明:本文为CSDN博主「散人陈某」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/jameschen9051/article/details/78883938