📄 38.txt
字号:
/** GUI设计
知识点:
2 基于AWT的GUI设计
2 AWT组件及布局管理器使用
目标/功能要求:
定义一个基于控制台的Java应用程序,实现标准/科学计算器的图形界面,可参考Win2000附件中的计算器界面,例如:
说明:
1. 开发可视化的图形用户界面,使用java.awt包中的下述组件类:Frame, Panel, MenuBar, Menu, MenuItem,
TextField, Button等,也可以根据需要使用其它的组件或自己定制的组件类型。
2. 计算器的图形用户界面至少应包括下述两个组成部分:显示器、按
键。显示器用于显示用户键入的操作数、中间结果和最终结果;按键包括数字键、运算符号键和其它辅助功能键,用于输入数据和运算指令;下图是一个参考界面,也可参照Win98或Win2000附件中的计算机进行外观设计:
*///
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.geom.*;
import java.util.*;
public class GuiTest
{
public static void main(String[] args)
{
UserFrame frame = new UserFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close window
GuiMenuBar menuBar = new GuiMenuBar(); // create menu
frame.setJMenuBar(menuBar);
frame.setLocation(200,250); // show frame
frame.addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.setVisible(true);
}
}
class GuiMenuBar extends JMenuBar // define menu
{
public GuiMenuBar()
{
JMenu File= new JMenu ("File"); //file menu
JMenuItem Exit = new JMenuItem ("Exit");
Exit.addActionListener(new
AbstractAction("Exit")
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
File.add(Exit);
this.add(File);
JMenu Help= new JMenu ("Help"); // help menu
JMenuItem About = new JMenuItem ("About");
Help.add(About);
About.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
JOptionPane.showMessageDialog(null,"科学计算器 1.0版本","关于计算器",JOptionPane.INFORMATION_MESSAGE,null);
}
});
this.add(Help);
}
}
class UserFrame extends JFrame // define Frame
{
Container contentPane;
final JTextField showTextField;
private JButton Space;
public UserFrame() // initialize
{
this.setSize(WIDTH, HEIGHT);
this.setTitle(" 科学计算器 1.0 版");
this.setResizable(false);
this.setIconImage(Toolkit.getDefaultToolkit().getImage("about.gif"));
this.setCursor(new Cursor(Cursor.HAND_CURSOR));
this.sign = false;
this.str = "";
this.str1 = "";
this.n = 0;
this.m = 0;
this.k = 0;
JPanel panel = new JPanel(); // create panel
showTextField = new JTextField("0",27); // set textfield
panel.add(showTextField);
PressAction action = new PressAction(); // create actionListener
// line 1 . set button
Space = new JButton(" ");
panel.add(Space);
JButton backSpace = new JButton(" Backspace ");
backSpace.setForeground(Color.red);
// backSpace.setFont(new Font("宋体",Font.ITALIC,14));
backSpace.addActionListener(action);
panel.add(backSpace);
JButton buttonCE = new JButton(" CE ");
buttonCE.setForeground(Color.red);
buttonCE.addActionListener(action);
panel.add(buttonCE);
JButton buttonC = new JButton(" C ");
buttonC.setForeground(Color.red);
buttonC.addActionListener(action);
panel.add(buttonC);
//line 2 .
JButton buttonMC = new JButton("MC");
buttonMC.setForeground(Color.red);
buttonMC.addActionListener(action);
panel.add(buttonMC);
JButton button7 = new JButton("7");
button7.addActionListener(action);
panel.add(button7);
JButton button8 = new JButton("8");
button8.addActionListener(action);
panel.add(button8);
JButton button9 = new JButton("9");
button9.addActionListener(action);
panel.add(button9);
JButton buttonDev = new JButton("/");
buttonDev.setForeground(Color.red);
buttonDev.addActionListener(action);
panel.add(buttonDev);
JButton buttonSqrt = new JButton("sqrt");
buttonSqrt.addActionListener(action);
panel.add(buttonSqrt);
//line 3 .
JButton buttonMR= new JButton("MR");
buttonMR.setForeground(Color.red);
buttonMR.addActionListener(action);
panel.add(buttonMR);
JButton button4 = new JButton("4");
button4.addActionListener(action);
panel.add(button4);
JButton button5 = new JButton("5");
button5.addActionListener(action);
panel.add(button5);
JButton button6 = new JButton("6");
button6.addActionListener(action);
panel.add(button6);
JButton buttonMul = new JButton("*");
buttonMul.setForeground(Color.red);
buttonMul.addActionListener(action);
panel.add(buttonMul);
JButton buttonPer = new JButton(" % ");
buttonPer.addActionListener(action);
panel.add(buttonPer);
//line 4 .
JButton buttonMS= new JButton("MS");
buttonMS.setForeground(Color.red);
buttonMS.addActionListener(action);
panel.add(buttonMS);
JButton button1 = new JButton("1");
button1.addActionListener(action);
panel.add(button1);
JButton button2 = new JButton("2");
button2.addActionListener(action);
panel.add(button2);
JButton button3 = new JButton("3");
button3.addActionListener(action);
panel.add(button3);
JButton buttonSub = new JButton("-");
buttonSub.setForeground(Color.red);
buttonSub.addActionListener(action);
panel.add(buttonSub);
JButton buttonRec = new JButton("1/x");
buttonRec.addActionListener(action);
panel.add(buttonRec);
//line 5 .
JButton buttonM= new JButton("M+");
buttonM.setForeground(Color.red);
buttonM.addActionListener(action);
panel.add(buttonM);
JButton button0 = new JButton("0");
button0.addActionListener(action);
panel.add(button0);
JButton buttonRev = new JButton("+/-");
buttonRev.addActionListener(action);
panel.add(buttonRev);
JButton buttonPoint = new JButton(".");
buttonPoint.addActionListener(action);
panel.add(buttonPoint);
JButton buttonAdd = new JButton("+");
buttonAdd.setForeground(Color.red);
buttonAdd.addActionListener(action);
panel.add(buttonAdd);
JButton buttonEqual = new JButton("=");
buttonEqual.setForeground(Color.red);
buttonEqual.addActionListener(action);
panel.add(buttonEqual);
contentPane = getContentPane();
contentPane.add(panel,BorderLayout.CENTER); // add panel to frame
}
class PressAction implements ActionListener // define actionListener
{
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand().trim(); // get ActionCommand
if(s.equals("=")) ActionEqual();
if(s.equals("Backspace")) ActionBackSpace();
if(s.equals("CE")) ActionCE();
if(s.equals("C")) ActionC();
if(s=="0"| s=="1"|s=="2"|s=="3"|s=="4"|s=="5"|s=="6"|s=="7"|s=="8"|s=="9")
Action0$9(s);
if(s=="+"|s=="-")
ActionAddSub(s);
if(s=="*"|s=="/"|s=="%")
ActionMulDev(s);
if(s.equals("1/x")) ActionRec();
if(s.equals(".")) ActionPoint();
if(s.equals("sqrt")) ActionSqrt();
if(s.equals("M+")) ActionM();
if(s.equals("MR")) ActionMR();
if(s.equals("MC")) ActionMC();
if(s.equals("MS")) ActionMS();
if(s.equals("+/-")) ActionRev();
}
}
/// Action define
private double n,m,k; //文本框记录或者中间数据
private boolean sign; //标记是否从新输入,true 追加数字,false 记下当前数字,重新输入
private String str,str1; //记下运算符号,
public void Action0$9(String s) // bounton "0"-"9" action
{
if(sign)
showTextField.setText(showTextField.getText()+s);
else
{ showTextField.setText(s);sign = true;}
}
public void ActionCE() // bounton "CE" action
{
showTextField.setText("0");
sign = false;
}
public void ActionC() // bounton "C" action
{
showTextField.setText("0");
n=0;
sign = false;
str="";
}
public void ActionRec() // bounton "1/x" action
{
showTextField.setText(Double.toString(1/Double.parseDouble(showTextField.getText())));
sign = false;
}
public void ActionBackSpace() // bounton "backSpace" action
{
String s = showTextField.getText();
if(s.length()>1)
showTextField.setText(s.substring(0,s.length()-1));
else
{ showTextField.setText("0"); sign=false; }
}
public void ActionSqrt() // bounton "sqrt" action
{
showTextField.setText(Double.toString(Math.sqrt(Double.parseDouble(showTextField.getText()))));
sign = false;
}
public void ActionPoint() // bounton "." action
{
String s = showTextField.getText();
boolean flag = true;
for(int i = 0;i<s.length();i++)
{
if(s.charAt(i)=='.') flag=false;
}
if(flag)
showTextField.setText(showTextField.getText()+".");
if(!sign)
{ showTextField.setText("0."); this.sign=true;
}
}
public void ActionMC() // bounton "MC" action
{
this.Space.setText(" ");
m=0;
}
public void ActionMS() // bounton "MS" action
{
this.Space.setText("M");
m=Double.parseDouble(showTextField.getText());
this.sign = false ;
}
public void ActionMR() // bounton "MR" action
{
this.showTextField.setText(""+m);
this.sign = false;
}
public void ActionM() // bounton "M+" action
{
this.Space.setText("M");
m = m+Double.parseDouble(showTextField.getText());
this.sign = false ;
}
public void ActionRev() // bounton "+/-" action
{
String s = showTextField.getText();
double d = Double.parseDouble(s);
if(d!=0)
d = - d ;
showTextField.setText(Double.toString(d));
this.sign = false ;
}
public void ActionAddSub(String s) // bounton "+" "-" action
{
if(!str.equals(""))
ActionEqual();
if(str1!="")
{
ActionEqual();
n = k;
str =str1;
str1="";
ActionEqual();
}
str = s;
n = Double.parseDouble(showTextField.getText());
sign = false;
}
public void ActionMulDev(String s) // bounton "%" "*" "/" action
{
if(str==s)
{
ActionEqual();
}
else // calculate
{
k=n;
str1=str;
}
str = s;
n = Double.parseDouble(showTextField.getText());
sign = false;
}
public void ActionEqual() // bounton "=" action
{
double t=Double.parseDouble(showTextField.getText());
if(str.equals( "/"))
n = n/t;
if(str.equals("*"))
n = n*t;
if(str.equals( "-"))
n = n-t;
if(str.equals("+"))
n = n+t;
if(str.equals("%"))
n = n%t;
if(str1!="")
{
str=str1;
str1="";
showTextField.setText(Double.toString(n));
n = k ;
ActionEqual();
}
str="";
showTextField.setText(Double.toString(n));
sign = false;
}
public static final int WIDTH = 310;
public static final int HEIGHT = 250;
}
// end //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -