?? infojpanel.java
字號:
package my_calculator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;
public class InfoJPanel extends JFrame implements ActionListener{
private boolean flag=true;
private int i=0;
//定義文本框用來顯示數據
private JTextField JTF_result=new JTextField(24);
//下拉表列表用來定義運算符
String functions[]={"加","減","乘","除"};
JComboBox JCBfun=new JComboBox(functions);
//控制按鈕
private JButton on_off=new JButton("");
private JButton Backspace=new JButton("退格");
private JButton clean=new JButton("清屏");
private JButton reset=new JButton("復位");
//數字按鈕
private JButton one=new JButton("1");
private JButton two=new JButton("2");
private JButton three=new JButton("3");
private JButton four=new JButton("4");
private JButton five=new JButton("5");
private JButton six=new JButton("6");
private JButton seven=new JButton("7");
private JButton eight=new JButton("8");
private JButton night=new JButton("9");
private JButton zero=new JButton("0");
private JButton point=new JButton(".");
//運算按鈕
private JButton add=new JButton("+");
private JButton sub=new JButton("-");
private JButton mul=new JButton("*");
private JButton div=new JButton("/");
private JButton equ1=new JButton("=");
private JButton equ2=new JButton("等于");
//符號按鈕
private JButton pos_or_minus=new JButton("+/-");
private JButton sqrt=new JButton("sqrt");
private JButton cos=new JButton("cos");
private JButton sin=new JButton("sin");
//聲明面板
private JPanel JPTextArea=new JPanel();
private JPanel JPButton_control=new JPanel();
private JPanel JPButton_data=new JPanel();
//構造方法
public InfoJPanel(JFrame mainframe){
Container contentPane=mainframe.getContentPane();
//建立放置文本框的面板
JPTextArea.setLayout(new FlowLayout());
JPTextArea.add(JTF_result);
JTF_result.setBackground(Color.lightGray.brighter().brighter());
JTF_result.setForeground(Color.blue); //字體顏色
JTF_result.setFont(new Font("楷體",Font.LAYOUT_LEFT_TO_RIGHT,16)); //字體設置
JTF_result.setVisible(true);
//建立放置功能按鈕的面板
JPButton_control.setLayout(new GridLayout(1,4,8,8));
JPButton_control.add(on_off);
JPButton_control.add(Backspace);
JPButton_control.add(clean);
JPButton_control.add(reset);
//為按鈕添加圖標
on_off.setIcon(new ImageIcon("images\\top_exit.gif"));
//建立放置數字和符號按鈕的面板
JPButton_data.setLayout(new GridLayout(4,5,8,4));
JPButton_data.add(seven);
JPButton_data.add(eight);
JPButton_data.add(night);
JPButton_data.add(div);
JPButton_data.add(sqrt);
JPButton_data.add(four);
JPButton_data.add(five);
JPButton_data.add(six);
JPButton_data.add(mul);
JPButton_data.add(cos);
JPButton_data.add(one);
JPButton_data.add(two);
JPButton_data.add(three);
JPButton_data.add(sub);
JPButton_data.add(sin);
JPButton_data.add(zero);
JPButton_data.add(pos_or_minus);
JPButton_data.add(point);
JPButton_data.add(add);
JPButton_data.add(equ1);
//把面板放置到框架中
contentPane.setLayout(new FlowLayout());
contentPane.add(JPTextArea);
contentPane.add(JPButton_control);
contentPane.add(JPButton_data);
//設置字體顏色
on_off.setForeground(Color.red);
Backspace.setForeground(Color.red);
clean.setForeground(Color.red);
reset.setForeground(Color.red);
add.setForeground(Color.red);
sub.setForeground(Color.red);
mul.setForeground(Color.red);
div.setForeground(Color.red);
equ1.setForeground(Color.red);
one.setForeground(Color.blue);
two.setForeground(Color.blue);
three.setForeground(Color.blue);
four.setForeground(Color.blue);
five.setForeground(Color.blue);
six.setForeground(Color.blue);
seven.setForeground(Color.blue);
eight.setForeground(Color.blue);
night.setForeground(Color.blue);
zero.setForeground(Color.blue);
sqrt.setForeground(Color.blue);
cos.setForeground(Color.blue);
sin.setForeground(Color.blue);
point.setForeground(Color.blue);
pos_or_minus.setForeground(Color.blue);
mainframe.setVisible(true);
//設置監聽器
on_off.addActionListener(this);
Backspace.addActionListener(this);
clean.addActionListener(this);
reset.addActionListener(this);
one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
night.addActionListener(this);
zero.addActionListener(this);
point.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
equ1.addActionListener(this);
equ2.addActionListener(this);
pos_or_minus.addActionListener(this);
sqrt.addActionListener(this);
cos.addActionListener(this);
sin.addActionListener(this);
}
//按鈕事件處理方法
public void actionPerformed(ActionEvent e){
String Command=e.getActionCommand();
//關閉按鈕
if(e.getSource()==on_off){
System.exit(0);
}
if(e.getSource()==one){
clean_screen(); //判別是否要清屏
Append("1");
}
if(e.getSource()==two){
clean_screen(); //判別是否要清屏
Append("2");
}
if(e.getSource()==three){
clean_screen(); //判別是否要清屏
Append("3");
}
if(e.getSource()==four){
clean_screen(); //判別是否要清屏
Append("4");
}
if(e.getSource()==five){
clean_screen(); //判別是否要清屏
Append("5");
}
if(e.getSource()==six){
clean_screen(); //判別是否要清屏
Append("6");
}
if(e.getSource()==seven){
clean_screen(); //判別是否要清屏
Append("7");
}
if(e.getSource()==eight){
clean_screen(); //判別是否要清屏
Append("8");
}
if(e.getSource()==night){
clean_screen(); //判別是否要清屏
Append("9");
}
if(e.getSource()==zero){
clean_screen(); //判別是否要清屏
Append("0");
}
if(Command.equals(".")){
clean_screen(); //判別是否要清屏
Append(".");
}
if(Command.equals("+")){
clean_screen(); //判別是否要清屏
Append("+");
String s1=JTF_result.getText();
}
if(Command.equals("-")){
clean_screen(); //判別是否要清屏
Append("-");
}
if(Command.equals("*")){
clean_screen(); //判別是否要清屏
Append("*");
}
if(Command.equals("/")){
clean_screen(); //判別是否要清屏
Append("/");
}
if(Command.equals("=")){
clean_screen(); //判別是否要清屏
Append("=");
String s=JTF_result.getText();
Cal(s);
flag=true;
}
if(Command.equals("+/-")){
clean_screen();
String s1=JTF_result.getText();
String s2=null;
String s3=null;
if(i%2==0){
s2="-".concat(s1);JTF_result.setText(s2);
}
else{
s3=s1.substring(1,JTF_result.getText().length());
JTF_result.setText(s3);
}
++i;
}
if(Command.equals("sqrt")){
clean_screen(); //判別是否要清屏
Append("sqrt");
}
if(Command.equals("cos")){
clean_screen(); //判別是否要清屏
Append("cos");
}
if(Command.equals("sin")){
clean_screen(); //判別是否要清屏
Append("sin");
}
//退格按鈕
if(Command=="退格"){
int len=JTF_result.getText().length();
if(len>=1){
String s=JTF_result.getText().substring(0,len-1);
JTF_result.setText(s);
}
}
//清空按鈕
if(Command=="清屏"){
JTF_result.setText(null);
}
//復位按鈕
if(Command=="復位"){
JTF_result.setText(null);
}
}
//判別是否要清屏
void clean_screen(){
if(flag){
System.out.println("要清屏了!!");
JTF_result.setText(null);
}
}
//對表達式的處理
void Cal(String s){
int m1=0;
int m2=0;
m2=s.indexOf("=");
double resu=0;
char ch=HaveFun(s);
String a=String.valueOf(ch);
Is_First_Fun(s); //對運算符出現在首位的錯誤處理
if(s.startsWith("sqrt",0)){
int len=JTF_result.getText().length();
s=JTF_result.getText().substring(4,len-1);
Double data=Double.parseDouble(s);
System.out.println(s);
resu=Math.sqrt(data);
String result=Double.toString(resu);
}
if(s.startsWith("cos",0)){
int len=JTF_result.getText().length();
s=JTF_result.getText().substring(3,len-1);
Double data=Double.parseDouble(s);
resu=Math.cos(data*Math.PI/180);
String result=Double.toString(resu);
}
if(s.startsWith("sin",0)){
int len=JTF_result.getText().length();
s=JTF_result.getText().substring(3,len-1);
Double data=Double.parseDouble(s);
resu=Math.sin(data*Math.PI/180);
String result=Double.toString(resu);
}
if(a.equals("+"))
{
System.out.println("我是加號2");
m1=s.indexOf("+");
String s1=s.substring(0,m1);
String s2=s.substring(m1+1,m2);
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
resu=d1+d2;
}
if(a.equals("-"))
{
System.out.println("我是減號2");
m1=s.indexOf("-");
String s1=s.substring(0,m1);
String s2=s.substring(m1+1,m2);
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
resu=d1-d2;
}
if(a.equals("*"))
{
System.out.println("我是乘號2");
m1=s.indexOf("*");
String s1=s.substring(0,m1);
String s2=s.substring(m1+1,m2);
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
resu=d1*d2;
}
if(a.equals("/"))
{
System.out.println("我是除號2");
m1=s.indexOf("/");
String s1=s.substring(0,m1);
String s2=s.substring(m1+1,m2);
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
resu=d1/d2;
}
String r=Double.toString(resu);
Append(r);
}
//對運算符出現在首位的錯誤處理
void Is_First_Fun(String s){
if((s.startsWith("/",0)||s.startsWith("*",0)||s.startsWith("=",0))){
JOptionPane.showMessageDialog(null,"表達式首位符號出錯","非法輸入",JOptionPane.ERROR_MESSAGE);
System.out.println("表達式首位不能是運算符");
flag=false;
}
}
//在文本框添加數據
void Append(String str){
String str1=JTF_result.getText();
String str2=str1+str;
JTF_result.setText(str2);
flag=false;
}
//判斷含有哪個運算符號
char HaveFun(String s){
System.out.println(s);
char temp=0;
String ad="+";
for(int i=0;i<s.length();i++){
if(s.startsWith("+",i)) {
if(i==0) Append("Sorry,這個功能尚未實現!");
flag=false;
System.out.println("我是加號1");
return '+';
}
else if(s.startsWith("-",i)) {
if(i==0) Append("Sorry,這個功能尚未實現!");
flag=false;
System.out.println("我是減號1");
return '-';
}
else if(s.startsWith("*",i)) {
System.out.println("我是乘號1");
return '*';
}
else if(s.startsWith("/",i)) {
System.out.println("我是除號1");
return '/';
}
else if(s.startsWith(".",i)) {
if(i==0) Append("Sorry,這個功能尚未實現!");
flag=false;
System.out.println("我是點");
return '-';
}
}
return '#';
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -