大二java課設要求通過選單可以切換簡易計算器和科學計算器。我之前按按鈕能顯示數字,后來寫著寫著,現在數字顯示不了了。我的切換做的也有問題,我會彈出兩個計算器視窗

求助
,謝謝package packagecalculater;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main implements ActionListener //處理按鈕implements ActionListener
{
JTextField tf;
JButton bt;
String btText[]= {"7","8","9","/",
"4","5","6","*",
"1","2","3","-",
"0","mode1","mode2","+",
"sqrt","pow","%","=",
};
public JMenu set1;
char mode1 ;
char mode2;
String strA=""; //代表輸入的第一個數字
String strB=""; //代表輸入的第二個數字
String strC=""; //代表A+B的結果
char operator='~';//代表運算子,~代表當前沒有運算子
String operator1="~";
//
public static void main(String args[])
{
Main app=new Main();
app.go(); //定義go方法
app.go1();
}
void go()
{
//
JFrame frame =new JFrame("Calculator");//import 進來
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
tf=new JTextField();
tf.setPreferredSize(new Dimension(600,90));//美化界面,設定輸入框大小
bt=new JButton();
frame.getContentPane().add(tf,BorderLayout.NORTH);
frame.getContentPane().add(bt,BorderLayout.CENTER);//內容面板
//
JPanel p=new JPanel();//內容面板
p.setLayout(new GridLayout(4,4));//四行四列
//添加16個按鈕
for(int i=0;i<16;i++) {
JButton bt = new JButton(btText[i]);
p.add(bt);
bt.addActionListener(this);//讓按鈕有反應,給按鈕添加回應,“this”對應事件
}
frame.getContentPane().add(p,BorderLayout.CENTER);//添加按鈕
frame.setSize(600, 700); //界面的長寬高
frame.setVisible(true);
}
void go1()
{
//
JFrame frame =new JFrame("Calculator");//import 進來
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
tf=new JTextField();
tf.setPreferredSize(new Dimension(600,90));//美化界面,設定輸入框大小
bt=new JButton();
frame.getContentPane().add(tf,BorderLayout.NORTH);
frame.getContentPane().add(bt,BorderLayout.CENTER);//內容面板
//
JPanel p=new JPanel();//內容面板
p.setLayout(new GridLayout(5,4));//四行四列
//添加16個按鈕
for(int i=0;i<20;i++) {
JButton bt = new JButton(btText[i]);
p.add(bt);
bt.addActionListener(this);//讓按鈕有反應,給按鈕添加回應,“this”對應事件
}
frame.getContentPane().add(p,BorderLayout.CENTER);//添加按鈕
frame.setSize(600, 700); //界面的長寬高
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e, boolean operateValidFlag) {
// TODO 自動生成的方法存根
//顯示不同的值
String action =e.getActionCommand();
//字串處理
char act=action.charAt(0);
String s1=action.toString();
if(act>='0'&&act<='9') {
//判斷輸入的是否為第二個數值
if(operator=='~') { //~代表當前沒有運算子,沒有運算子就代表輸入的是數字A
if(act=='0') {
if(strA.length()>0) {
strA+=action;
tf.setText(strA);//將資料放入白框中
}
}
else {
strA+=action;
tf.setText(strA);//將資料放入白框中
}
}else
{
if(act=='0')
{
if(strB.length()>0)
{
strB+=action;
tf.setText(strB);
}
}
else {
strB+=action;
tf.setText(strB);
}
}
}
else if(act=='+'||act=='-'||act=='*'||act=='/'||s1=="sqrt"||s1=="pow"||act=='%') {
operator=act;//按了加減乘除之后operator又恢復成數字
operator1=s1;
tf.setText("");//將上一次的運算清空
}
else if(act=='=')
{
if(operator=='+') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a+b;
tf.setText(c+"");//顯示結果
}
else if(operator=='-') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a-b;
tf.setText(c+"");//顯示結果
}
else if(operator=='*') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a*b;
tf.setText(c+"");//顯示結果
}
else if(operator=='/') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a/b;
tf.setText(c+"");//顯示結果
}
else if(operator1=="sqrt") {
int a=Integer.parseInt(strA);//轉換數字型別
int c= (int) Math.sqrt(a);
tf.setText(c+"");//顯示結果
}
else if(operator1=="pow") {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=(int) Math.pow(a,b);
tf.setText(c+"");//顯示結果
}
else if(operator=='%') {
int a=Integer.parseInt(strA);//轉換數字型別
int c=(int) (a/100);
tf.setText(c+"");//顯示結果
}
}
operator ='~';//清空
operator1 ="~";
strA="";//清空A
strB="";//清空B
}
public void createmenu() {
set1=new JMenu("切換");
JMenuItem[] itemsfunction = new JMenuItem[2];
itemsfunction[0]=new JMenuItem("標準計算器");
itemsfunction[1]=new JMenuItem("科學計算器");
itemsfunction[0].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
if (operator1=="mode1")
{
go();
}
}
});
itemsfunction[1].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
if (operator1=="mode2")
{
go1();
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自動生成的方法存根
}
}
uj5u.com熱心網友回復:
你這前端是啥啊,gui么?uj5u.com熱心網友回復:
嗯是的,圖形用戶界面
uj5u.com熱心網友回復:
完了,gui不懂.
uj5u.com熱心網友回復:
你這前端是啥啊,gui么?
嗯是的,圖形用戶界面
完了,gui不懂.
我現在已經解決了第一個問題,就剩第二個切換的問題了
uj5u.com熱心網友回復:
首先可用代碼標簽貼出來,這樣看得腦瓜疼。uj5u.com熱心網友回復:
第一個:簡易計算器
package packagecalculater1;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Jianyi implements ActionListener //處理按鈕implements ActionListener
{
JTextField tf;
JButton bt;
String btText[]= {"7","9","10","/",
"4","5","6","*",
"1","2","3","-",
"0","go","=","+"};
String strA=""; //代表輸入的第一個數字
String strB=""; //代表輸入的第二個數字
String strC=""; //代表A+B的結果
char operator='~';//代表運算子,~代表當前沒有運算子
//
public static void main(String args[])
{
Jianyi app=new Jianyi();
app.go(); //定義go方法
}
void go()
{
//
JFrame frame =new JFrame("Calculator");//import 進來
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
tf=new JTextField();
tf.setPreferredSize(new Dimension(600,90));//美化界面,設定輸入框大小
bt=new JButton();
frame.getContentPane().add(tf,BorderLayout.NORTH);
frame.getContentPane().add(bt,BorderLayout.CENTER);//內容面板
//
JPanel p=new JPanel();//內容面板
p.setLayout(new GridLayout(4,4));//四行四列
//添加16個按鈕
for(int i=0;i<16;i++) {
JButton bt = new JButton(btText[i]);
p.add(bt);
bt.addActionListener(this);//讓按鈕有反應,給按鈕添加回應,“this”對應事件
}
frame.getContentPane().add(p,BorderLayout.CENTER);//添加按鈕
frame.setSize(600, 700); //界面的長寬高
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自動生成的方法存根
//顯示不同的值
String action =e.getActionCommand();
//字串處理
char act=action.charAt(0);
if(act>='0'&&act<='9') {
//判斷輸入的是否為第二個數值
if(operator=='~') { //~代表當前沒有運算子,沒有運算子就代表輸入的是數字A
if(act=='0') {
if(strA.length()>0) {
strA+=action;
tf.setText(strA);//將資料放入白框中
}
}
else {
strA+=action;
tf.setText(strA);//將資料放入白框中
}
}else
{
if(act=='0')
{
if(strB.length()>0)
{
strB+=action;
tf.setText(strB);
}
}
else {
strB+=action;
tf.setText(strB);
}
}
}
else if(act=='+'||act=='-'||act=='*'||act=='/') {
operator=act;//按了加減乘除之后operator又恢復成數字
tf.setText("");//將上一次的運算清空
}
else if(act=='=')
{
if(operator=='+') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a+b;
tf.setText(c+"");//顯示結果
}
else if(operator=='-') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a-b;
tf.setText(c+"");//顯示結果
}
else if(operator=='*') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a*b;
tf.setText(c+"");//顯示結果
}
else if(operator=='/') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a/b;
tf.setText(c+"");//顯示結果
}
operator ='~';//清空
strA="";//清空A
strB="";//清空B
}
}
}
第二個:科學計算器
package packagecalculater1;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Kexue implements ActionListener //處理按鈕implements ActionListener
{
JTextField tf;
JButton bt;
String btText[]= {"7","9","10","/",
"4","5","6","*",
"1","2","3","-",
"0","back","=","+",
"sqrt","pow","hp","x^2"
};
String strA=""; //代表輸入的第一個數字
String strB=""; //代表輸入的第二個數字
String strC=""; //代表A+B的結果
char operator='~';//代表運算子,~代表當前沒有運算子
String operator1="~";
//
public static void main(String args[])
{
Kexue app=new Kexue();
app.go(); //定義go方法
}
void go()
{
//
JFrame frame =new JFrame("Calculator");//import 進來
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//
tf=new JTextField();
tf.setPreferredSize(new Dimension(600,90));//美化界面,設定輸入框大小
bt=new JButton();
frame.getContentPane().add(tf,BorderLayout.NORTH);
frame.getContentPane().add(bt,BorderLayout.CENTER);//內容面板
//
JPanel p=new JPanel();//內容面板
p.setLayout(new GridLayout(5,4));//5行四列
//添加20個按鈕
for(int i=0;i<20;i++) {
JButton bt = new JButton(btText[i]);
p.add(bt);
bt.addActionListener(this);//讓按鈕有反應,給按鈕添加回應,“this”對應事件
}
frame.getContentPane().add(p,BorderLayout.CENTER);//添加按鈕
frame.setSize(600, 700); //界面的長寬高
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自動生成的方法存根
//顯示不同的值
String action =e.getActionCommand();
//字串處理
char act=action.charAt(0);
String s1=action.toString();
if(act>='0'&&act<='9') {
//判斷輸入的是否為第二個數值
if(operator=='~') { //~代表當前沒有運算子,沒有運算子就代表輸入的是數字A
if(act=='0') {
if(strA.length()>0) {
strA+=action;
tf.setText(strA);//將資料放入白框中
}
}
else {
strA+=action;
tf.setText(strA);//將資料放入白框中
}
}else
{
if(act=='0')
{
if(strB.length()>0)
{
strB+=action;
tf.setText(strB);
}
}
else {
strB+=action;
tf.setText(strB);
}
}
}
else if(act=='+'||act=='-'||act=='*'||act=='/'||s1=="sqrt"||s1=="pow"||s1=="hp"||s1=="x^2") {
operator=act;//按了加減乘除之后operator又恢復成數字
operator1=s1;
tf.setText("");//將上一次的運算清空
}
else if(act=='=')
{
if(operator=='+') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a+b;
tf.setText(c+"");//顯示結果
}
else if(operator=='-') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a-b;
tf.setText(c+"");//顯示結果
}
else if(operator=='*') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a*b;
tf.setText(c+"");//顯示結果
}
else if(operator=='/') {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=a/b;
tf.setText(c+"");//顯示結果
}
else if(operator1=="sqrt") {
int a=Integer.parseInt(strA);//轉換數字型別
int c= (int) Math.sqrt(a);
tf.setText(c+"");//顯示結果
}
else if(operator1=="pow") {
int a=Integer.parseInt(strA);//轉換數字型別
int b=Integer.parseInt(strB);
int c=(int) Math.pow(a,b);
tf.setText(c+"");//顯示結果
}
else if(operator1=="hp") {
int a=Integer.parseInt(strA);//轉換數字型別
double c=(double)1/a;
tf.setText(c+"");//顯示結果
}
else if(operator1=="x^2") {
int a=Integer.parseInt(strA);//轉換數字型別
int c=a*a;
tf.setText(c+"");//顯示結果
}
operator ='~';//清空
strA="";//清空A
strB="";//清空B
}
}
}
uj5u.com熱心網友回復:
首先可用代碼標簽貼出來,這樣看得腦瓜疼。
我現在就是不太清楚怎么把這兩個進行切換,我和我室友搗鼓了3小時了,關于切換
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/223081.html
標籤:Eclipse
上一篇:SpringBoot自動配置原理
下一篇:求助,不懂用結構體。苦惱啊
