本文目录一览:
JAVA中AWT本质是什么?
你好,这是一些用于GUI类封装起来的一个包,主要是在AWT包中的,作为一个重量级的GUI编辑,不同于SWING包下的一些轻量级的类。只有你在运用这些类的时候,导入包的时候才可以使用。里面给你封装了很多界面的组件,例如窗体,面板,按钮等。也包括一些布局的方式。但是它由于不是轻量级的组件,所以开发比较耗时,而且平台的局限性也比较大。希望能帮到你。
JAVA编程题(AWT计算器)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.LinkedList; //工具包
import java.text.NumberFormat; //文本包
public class Calculator extends Frame implements ActionListener //计算器类
{
JTextField result;
NumberButton numberButton[];
OperatorButton operatorButton[];
Button radixpoint,positiveminus,backspace,reciprocal,equal,clear; //声明成员变量
//小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮
Panel panel;
String operator[]={"+","-","*","/"};
LinkedList linklist;
boolean pressequal=false;
public Calculator() //构造方法
{
super("计算器");
linklist=new LinkedList();
numberButton=new NumberButton[10];
for(int i=0;i=9;i++)
{
numberButton[i]=new NumberButton(i);
numberButton[i].addActionListener(this);
}
operatorButton=new OperatorButton[4];
for(int i=0;i4;i++)
{
operatorButton[i]=new OperatorButton(operator[i]);
operatorButton[i].addActionListener(this);
}
radixpoint=new Button(".");
positiveminus=new Button("+/-");
backspace=new Button("CE");
reciprocal=new Button("1/x");
equal=new Button("=");
clear=new Button("C");
radixpoint.setForeground(Color.red);
positiveminus.setForeground(Color.red);
backspace.setForeground(Color.red);
reciprocal.setForeground(Color.red);
equal.setForeground(Color.red);
clear.setForeground(Color.red);
radixpoint.addActionListener(this);
positiveminus.addActionListener(this);
backspace.addActionListener(this);
reciprocal.addActionListener(this);
equal.addActionListener(this);
clear.addActionListener(this);
result=new JTextField(10);
result.setHorizontalAlignment(JTextField.RIGHT);
result.setForeground(Color.black);
result.setBackground(Color.white);
result.setFont(new Font("TimesRoman",Font.PLAIN,14));
result.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
result.setEditable(false);
panel=new Panel();
panel.setLayout(new GridLayout(4,5));
panel.add(numberButton[1]);
panel.add(numberButton[2]);
panel.add(numberButton[3]);
panel.add(backspace);
panel.add(clear);
panel.add(numberButton[4]);
panel.add(numberButton[5]);
panel.add(numberButton[6]);
panel.add(operatorButton[0]);
panel.add(operatorButton[2]);
panel.add(numberButton[7]);
panel.add(numberButton[8]);
panel.add(numberButton[9]);
panel.add(operatorButton[1]);
panel.add(operatorButton[3]);
panel.add(numberButton[0]);
panel.add(positiveminus);
panel.add(reciprocal);
panel.add(radixpoint);
panel.add(equal);
add(result,"North");
add(panel,"Center");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(270,200);
setLocation(300,230);
setVisible(true);
}
public void actionPerformed(ActionEvent e) //按钮的单击事件处理方法
{
if(e.getSource() instanceof NumberButton) //数字按钮
{
NumberButton b=(NumberButton)e.getSource();
if(linklist.size()==0)
{
int number=b.getNumber();
linklist.add(""+number);
result.setText(""+number);
pressequal=false;
}
else if(linklist.size()==1pressequal==false)
{
int number=b.getNumber();
String num=(String)linklist.getFirst();
String s=num.concat(""+number);
linklist.set(0,s);
result.setText(s);
}
else if(linklist.size()==1pressequal==true)
{
int number=b.getNumber();
linklist.removeFirst();
linklist.add(""+number);
pressequal=false;
result.setText(""+number);
}
else if(linklist.size()==2)
{
int number=b.getNumber();
linklist.add(""+number);
result.setText(""+number);
}
else if(linklist.size()==3)
{
int number=b.getNumber();
String num=(String)linklist.getLast();
String s=num.concat(""+number);
linklist.set(2,s);
result.setText(s);
}
}
else if(e.getSource() instanceof OperatorButton) //操作按钮
{
OperatorButton b=(OperatorButton)e.getSource();
if(linklist.size()==1)
{
String fuhao=b.getOperator();
linklist.add(fuhao);
}
else if(linklist.size()==2)
{
String fuhao=b.getOperator();
linklist.set(1,fuhao);
}
else if(linklist.size()==3)
{
String fuhao=b.getOperator();
String number1=(String)linklist.getFirst();
String number2=(String)linklist.getLast();
String operator=(String)linklist.get(1);
try
{
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
double n=0;
if(operator.equals("+"))
{
n=n1+n2;
}
else if(operator.equals("-"))
{
n=n1-n2;
}
else if(operator.equals("*"))
{
n=n1*n2;
}
else if(operator.equals("/"))
{
n=n1/n2;
}
linklist.clear();
linklist.add(""+n);
linklist.add(fuhao);
result.setText(""+n);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==equal) //等号按钮
{
pressequal=true;
if(linklist.size()==1||linklist.size()==2)
{
String num=(String)linklist.getFirst();
result.setText(""+num);
}
else if(linklist.size()==3)
{
String number1=(String)linklist.getFirst();
String number2=(String)linklist.getLast();
String operator=(String)linklist.get(1);
try
{
double n1=Double.parseDouble(number1);
double n2=Double.parseDouble(number2);
double n=0;
if(operator.equals("+"))
{
n=n1+n2;
}
else if(operator.equals("-"))
{
n=n1-n2;
}
else if(operator.equals("*"))
{
n=n1*n2;
}
else if(operator.equals("/"))
{
n=n1/n2;
}
result.setText(""+n);
linklist.set(0,""+n);
linklist.removeLast();
linklist.removeLast();
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==radixpoint) //小数点按钮
{
if(linklist.size()==0)
{
pressequal=false;
}
else if(linklist.size()==1)
{
String dot=radixpoint.getLabel();
String num=(String)linklist.getFirst();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
linklist.set(0,s);
}
else
{
s=num;
}
linklist.set(0,s);
result.setText(s);
}
else if(linklist.size()==3)
{
String dot=radixpoint.getLabel();
String num=(String)linklist.getLast();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
linklist.set(2,s);
}
else
{
s=num;
}
result.setText(s);
}
}
else if(e.getSource()==backspace) //退格按钮
{
if(linklist.size()==1)
{
String num=(String)linklist.getFirst();
if(num.length()=1)
{
num=num.substring(0,num.length()-1);
linklist.set(0,num);
result.setText(num);
}
else
{
linklist.removeLast();
result.setText("0");
}
}
else if(linklist.size()==3)
{
String num=(String)linklist.getLast();
if(num.length()=1)
{
num=num.substring(0,num.length()-1);
linklist.set(2,num);
result.setText(num);
}
else
{
linklist.removeLast();
result.setText("0");
}
}
}
else if(e.getSource()==positiveminus) //正负号按钮
{
if(linklist.size()==1)
{
String number1=(String)linklist.getFirst();
try
{
double d=Double.parseDouble(number1);
d=-1*d;
String str=String.valueOf(d);
linklist.set(0,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
else if(linklist.size()==3)
{
String number2=(String)linklist.getLast();
try
{
double d=Double.parseDouble(number2);
d=-1*d;
String str=String.valueOf(d);
linklist.set(2,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==reciprocal) //求倒数按钮
{
if(linklist.size()==1||linklist.size()==2)
{
String number1=(String)linklist.getFirst();
try
{
double d=Double.parseDouble(number1);
d=1.0/d;
String str=String.valueOf(d);
linklist.set(0,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
else if(linklist.size()==3)
{
String number2=(String)linklist.getLast();
try
{
double d=Double.parseDouble(number2);
d=1.0/d;
String str=String.valueOf(d);
linklist.set(0,str);
result.setText(str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==clear) //清零按钮
{
pressequal=false;
result.setText("0");
linklist.clear();
}
}
public static void main(String args[])
{
new Calculator();
}
}
class NumberButton extends Button //数字按钮类
{
int number;
public NumberButton(int number) //构造方法
{
super(""+number);
this.number=number;
setForeground(Color.blue);
}
public int getNumber()
{
return number;
}
}
class OperatorButton extends Button //运算符号按钮类
{
String operator;
public OperatorButton(String operator) //构造方法
{
super(operator);
this.operator=operator;
setForeground(Color.red);
}
public String getOperator()
{
return operator;
}
}
java AWT 是什么?谢谢诶
AWT(Abstract Windowing Toolkit),中文译为抽象窗口工具包,是Java提供的用来建立和设置Java的图形用户界面的基本工具。AWT由Java中的java.awt包提供,里面包含了许多可用来建立与平台无关的图形用户界面(GUI)的类,这些类又被称为组件(components)
Java中AWT事件模型的概念求大神帮助
awt事件模型就是根据你出发的事件来执行相应的业务响应代码,,先设定事件源,然后绑定到事件监听器上,在事件监听器里写上业务代码,当你出发事件源时就会触发这个监听器执行里的代码
用java实现一个简单的计算器。
/*
* @(#)JCalculator.java 1.00 06/17/2015
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* A simple calculator program.
* pI saw this program in a QQ group, and help a friend correct it./p
*
* @author Singyuen Yip
* @version 1.00 12/29/2009
* @see JFrame
* @see ActionListener
*/
public class JCalculator extends JFrame implements ActionListener {
/**
* Serial Version UID
*/
private static final long serialVersionUID = -169068472193786457L;
/**
* This class help close the Window.
* @author Singyuen Yip
*
*/
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
int i;
// Strings for Digit Operator buttons.
private final String[] str = { "7", "8", "9", "/", "4", "5", "6", "*","1",
"2", "3", "-", ".", "0", "=", "+" };
// Build buttons.
JButton[] buttons = new JButton[str.length];
// For cancel or reset.
JButton reset = new JButton("CE");
// Build the text field to show the result.
JTextField display = new JTextField("0");
/**
* Constructor without parameters.
*/
public JCalculator() {
super("Calculator");
// Add a panel.
JPanel panel1 = new JPanel(new GridLayout(4, 4));
// panel1.setLayout(new GridLayout(4,4));
for (i = 0; i str.length; i++) {
buttons[i] = new JButton(str[i]);
panel1.add(buttons[i]);
}
JPanel panel2 = new JPanel(new BorderLayout());
// panel2.setLayout(new BorderLayout());
panel2.add("Center", display);
panel2.add("East", reset);
// JPanel panel3 = new Panel();
getContentPane().setLayout(new BorderLayout());
getContentPane().add("North", panel2);
getContentPane().add("Center", panel1);
// Add action listener for each digit operator button.
for (i = 0; i str.length; i++)
buttons[i].addActionListener(this);
// Add listener for "reset" button.
reset.addActionListener(this);
// Add listener for "display" button.
display.addActionListener(this);
// The "close" button "X".
addWindowListener(new WindowCloser());
// Initialize the window size.
setSize(800, 800);
// Show the window.
// show(); Using show() while JDK version is below 1.5.
setVisible(true);
// Fit the certain size.
pack();
}
public void actionPerformed(ActionEvent e) {
Object target = e.getSource();
String label = e.getActionCommand();
if (target == reset)
handleReset();
else if ("0123456789.".indexOf(label) 0)
handleNumber(label);
else
handleOperator(label);
}
// Is the first digit pressed?
boolean isFirstDigit = true;
/**
* Number handling.
* @param key the key of the button.
*/
public void handleNumber(String key) {
if (isFirstDigit)
display.setText(key);
else if ((key.equals(".")) (display.getText().indexOf(".") 0))
display.setText(display.getText() + ".");
else if (!key.equals("."))
display.setText(display.getText() + key);
isFirstDigit = false;
}
/**
* Reset the calculator.
*/
public void handleReset() {
display.setText("0");
isFirstDigit = true;
operator = "=";
}
double number = 0.0;
String operator = "=";
/**
* Handling the operation.
* @param key pressed operator's key.
*/
public void handleOperator(String key) {
if (operator.equals("+"))
number += Double.valueOf(display.getText());
else if (operator.equals("-"))
number -= Double.valueOf(display.getText());
else if (operator.equals("*"))
number *= Double.valueOf(display.getText());
else if (operator.equals("/"))
number /= Double.valueOf(display.getText());
else if (operator.equals("="))
number = Double.valueOf(display.getText());
display.setText(String.valueOf(number));
operator = key;
isFirstDigit = true;
}
public static void main(String[] args) {
new JCalculator();
}
}
运行界面: