日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java计算器

發(fā)布時(shí)間:2023/12/10 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java计算器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

計(jì)算器

實(shí)現(xiàn)的功能效果:

1、界面
按鈕、輸出框、背景圖
2、計(jì)算
加、減、乘、除、清零

知識(shí)點(diǎn):

1、繪制窗體,設(shè)置窗體標(biāo)題、大小,固定窗體大小、位置,設(shè)置關(guān)閉方式、窗體可見

2、從電腦本地導(dǎo)入圖片并繪制到窗體

3、 非頂級容器或組件的大小設(shè)置

4、單行文本框與多行文本域的添加

5、用數(shù)組批量添加按鈕等組件

6、給窗體容器、組件加監(jiān)聽

7、在監(jiān)聽類中處理各類動(dòng)作事件

8、把圖片路徑加到Lable上可以直接顯示圖片

9、設(shè)置按鈕透明度、按鈕字體顏色

10、設(shè)置布局

實(shí)現(xiàn)思路:

把計(jì)算器看做一個(gè)對象,創(chuàng)建這樣一個(gè)對象就需要一個(gè)類,那么就有相應(yīng)的屬性和方法

屬性:
窗體JFrame、按鈕、背景圖、文本框

方法:
按鍵監(jiān)聽方法,在監(jiān)聽類中寫出對應(yīng)的事件處理方法

通過方法實(shí)現(xiàn)---->點(diǎn)擊按鈕輸出相應(yīng)的值在文本框,點(diǎn)擊等于號(hào)則輸出結(jié)果

//這是有主方法的類public class Counter extends JPanel {public ImageIcon im;public Image img;public Graphics g;public static void main(String[] args) {Counter counter = new Counter();counter.run();}public void run() {CouterListener couterListener = new CouterListener();// 窗體屬性JFrame frame = new JFrame("Keivn專屬計(jì)算器");FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 10, 10);// 上下左右間距為10frame.setLayout(fl);frame.setSize(270, 550);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(3);frame.setResizable(false);// 固定窗體的大小// 背景im = new ImageIcon("plane\\cat_500_500.jpg");JLabel label = new JLabel(im);label.setPreferredSize(new Dimension(250, 200));// 非頂級容器大小設(shè)置方法frame.add(label);// 文本域JTextArea field = new JTextArea();// 多行文本域field.setPreferredSize(new Dimension(250, 50));field.setRows(3);// 設(shè)置文本域的行數(shù)frame.add(field);couterListener.setJText(field);// 按鈕String btArray[] = { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8","9", "*", ".", "0", "=", "/" };this.setLayout(new GridLayout(4, 4, 10, 10));frame.add(this);for (int i = 0; i < btArray.length; i++) {JButton button = new JButton(BtArray[i]);button.setFont(new Font("宋體", Font.PLAIN, 30));// 設(shè)置按鈕字體、大小、顏色button.setForeground(Color.yellow);button.setPreferredSize(new Dimension(50, 50));// button.setOpaque(false);//設(shè)置按鈕透明度// button.setBackground(new Color(255, 255, 255));button.setContentAreaFilled(false); // 設(shè)置按鈕透明度(方法二), 只須加上此句this.add(button);button.addActionListener(couterListener);// 給按鈕加監(jiān)聽}frame.setVisible(true);g = frame.getGraphics();} }

這是監(jiān)聽類

public class CouterListener implements ActionListener {private JTextArea JF;public int x = 0;public int flag = 1;public Double resulte = 0.0, temp;public String num[] = new String[3];public String count;public void setJText(JTextArea F) {// 接受文本框,用于監(jiān)聽輸出JF = F;}public void actionPerformed(ActionEvent e) {if (e.getActionCommand() == "+" || e.getActionCommand() == "-"|| e.getActionCommand() == "*" || e.getActionCommand() == "/") {JF.setText(JF.getText() + e.getActionCommand() + "\n");if (flag == 1)resulte = temp;count = e.getActionCommand();flag = 0;} else if (e.getActionCommand() == "=") {if (count == "+") {resulte = resulte + temp;JF.setText(resulte + "");} else if (count == "-") {resulte = resulte - temp;JF.setText(resulte + "");} else if (count == "*") {resulte = resulte * temp;JF.setText(resulte + "");} else if (count == "/") {resulte = resulte / temp;JF.setText(resulte + "");}} else {// 如果輸入非數(shù)字輸出到文本框并換行JF.setText(JF.getText() + e.getActionCommand());if (flag == 0) {num = JF.getText().split("\\n");temp = Double.parseDouble(num[1]);} else {temp = Double.parseDouble(JF.getText());}}} }

提示:Ctrl+Shift+O 可以快捷導(dǎo)入所有包

效果展示

總結(jié)

以上是生活随笔為你收集整理的Java计算器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。