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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java按钮退出_java – 如何在此程序中添加退出按钮?怎么样“清楚”?

發布時間:2024/9/27 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java按钮退出_java – 如何在此程序中添加退出按钮?怎么样“清楚”? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我需要添加一個“清除計算器”的按鈕,以及一個退出butPanel上的程序的按鈕.它也需要是非常基本的

Java代碼,因為我是初學者,并且有一個糟糕的comp.sci.老師.我有一個帶退出按鈕的代碼示例,但我不確定如何將它放入我的程序中.我已經嘗試了這么多.

此外,如果有一個更好的“錯誤檢查”方式,將非常感激.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Calculator2 extends JFrame implements ActionListener

{

JLabel heading = new JLabel ("2. Calculator");

JLabel intro1 = new JLabel ("This program stimulates a four-function calculator.");

JLabel intro2 = new JLabel ("It takes an operator and a number. It can add, subtract,");

JLabel intro3 = new JLabel (" multiply and divide. Enter in the format eg. '+35'. ");

JLabel inLabel = new JLabel (" Operation: ");

JLabel outLabel = new JLabel (" Total: ");

JTextField inOper = new JTextField (7);

JTextField outTota = new JTextField (7); // intro

//panels

JPanel titlePanel = new JPanel ();

JPanel intro1Panel = new JPanel ();

JPanel intro2Panel = new JPanel ();

JPanel intro3Panel = new JPanel ();

JPanel operPanel = new JPanel ();

JPanel totaPanel = new JPanel ();

JPanel butPanel = new JPanel ();

String operTemp;

String totaTemp;

public Calculator2 ()

{

setTitle ("C - 2.");

inOper.addActionListener (this);

outTota.setEditable (false);

getContentPane ().setLayout (new FlowLayout ());

titlePanel.add (heading);

intro1Panel.add (intro1);

intro2Panel.add (intro2);

intro3Panel.add (intro3);

operPanel.add (inLabel);

operPanel.add (inOper);

totaPanel.add (outLabel);

totaPanel.add (outTota); //adds components to panels

getContentPane ().add (titlePanel);

getContentPane ().add (intro1Panel);

getContentPane ().add (intro2Panel);

getContentPane ().add (intro3Panel);

getContentPane ().add (operPanel);

getContentPane ().add (totaPanel); //Adds panels to Frame

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

}

public static int isInteger (String input)

{

try

{

Integer.parseInt (input);

return Integer.parseInt (input);

}

catch (NumberFormatException nfe)

{

return 0;

}

} //isInteger method

// The application

public String calculate (String operation, String newtotal)

{

int total = isInteger (newtotal);

String totalS;

char operator;

int number = 0;

operator = operation.charAt (0);

if (operator == '+')

{

number = isInteger (operation.substring (1));

total = total + number;

totalS = Integer.toString (total);

}

else if (operator == '-')

{

number = isInteger (operation.substring (1));

total = total - number;

totalS = Integer.toString (total);

}

else if (operator == '*')

{

number = isInteger (operation.substring (1));

total = total * number;

totalS = Integer.toString (total);

}

else if (operator == '/')

{

number = isInteger (operation.substring (1));

total = total / number;

totalS = Integer.toString (total);

}

else

{

totalS = ("ERROR");

}

if (number == 0)

{

totalS = ("ERROR");

}

return totalS;

} // calculate method

public void actionPerformed (ActionEvent evt)

{

String userIn = inOper.getText ();

String totalIn = outTota.getText ();

try

{

totaTemp = calculate (userIn, totalIn);

outTota.setText (totaTemp + "");

}

catch (Exception ex)

{

outTota.setText ("ERROR");

}

repaint ();

}

public static void main (String[] args)

{

Calculator2 calc = new Calculator2 ();

calc.setSize (350, 350);

calc.setResizable (false);

calc.setVisible (true);

}

}

總結

以上是生活随笔為你收集整理的java按钮退出_java – 如何在此程序中添加退出按钮?怎么样“清楚”?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。