import java.awt.*;//陈展鹏
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.*;
public class ChenZhanpeng {
static JTextField edit;
static JButton an1,an2,an3,an4,an5,an6,an7,an8,an9,an10,an11,an12,an13,an14,an15,an16,an17,an18;
public static void main(String[]args){
JFrame jf=new JFrame("猪式计算器");
jf.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent e) {
System.out.println("欢迎您使用猪式计算器");
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
});
jf.setSize(320,300);
Container c=jf.getContentPane();
c.setLayout(new GridLayout(4,5,8,8));
an1=new JButton("<html><h1>1");
an2=new JButton("<html><h1>2");
an3=new JButton("<html><h1>3");
an4=new JButton("<html><h1>4");
an5=new JButton("<html><h1>5");
an6=new JButton("<html><h1>6");
an7=new JButton("<html><h1>7");
an8=new JButton("<html><h1>8");
an9=new JButton("<html><h1>9");
an10=new JButton("<html><h1>.");
an11=new JButton("<html><h1>+");
an12=new JButton("<html><h1>-<h1></html>");
an13=new JButton("<html><h1>*");
an14=new JButton("<html><h1>/");
an15=new JButton("<html><h1>=");
an16=new JButton("<html><h1>0");
an17=new JButton();
an17.setText("<html><h2>清零<h2></html>");
an18=new JButton("<html><h2>退格");
edit=new JTextField();
edit.setSize(300, 50);
c.add(edit);
c.add(an1);
c.add(an2);
c.add(an3);
c.add(an4);
c.add(an5);
c.add(an6);
c.add(an7);
c.add(an8);
c.add(an9);
c.add(an10);
c.add(an11);
c.add(an12);
c.add(an13);
c.add(an14);
c.add(an15);
c.add(an16);
c.add(an17);
c.add(an18);
jf.setVisible(true);
}
}
class jtq implements ActionListener{
public void actionPerformed(ActionEvent e) {
Object a=e.getSource();
JButton t;
t=(JButton)a;
if(t.getText().equals("清零")){
}
}
}
这段代码中,我添加了ActionListener监听器,我想实现,单击清零按钮的时候,edit文本框显示""为空,单击1,2,3。。。。。按钮分别在文本框JTextField edit中显示相应的数字,怎么实现。。。我编不下去了,请高手指点。
----------------解决方案--------------------------------------------------------
在监听器里调用edit.setText()即可。。。
----------------解决方案--------------------------------------------------------
好像都没继承actionListener吧?
----------------解决方案--------------------------------------------------------
jf应该还要注AcitonListener吧~~
----------------解决方案--------------------------------------------------------
给你改了一下,基本功能能实现了,界面设计不太好,没时间给你设计了,有空你自己设计界面吧!
package mypack;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyCountor
{
private JTextField edit;
private JButton an1,an2,an3,an4,an5,an6,an7,an8,an9;
private JButton an10,an11,an12,an13,an14,an15,an16,an17;
private double first = 0.0;
private double second = 0.0;
private double result = 0.0;
private String operator;
private String strInfo="";
private String str;
private JPanel panel;
public static void main(String[]args){
new MyCountor();
}
public MyCountor(){
JFrame jf=new JFrame("猪式计算器");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent e) {
System.out.println("欢迎您使用猪式计算器");
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf.setSize(280,200);
Container c=jf.getContentPane();
panel = new JPanel();
panel.setLayout(new GridLayout(4,4,4,4));
c.setLayout(new BorderLayout());
an1=new JButton("1");
an2=new JButton("2");
an3=new JButton("3");
an4=new JButton("4");
an5=new JButton("5");
an6=new JButton("6");
an7=new JButton("7");
an8=new JButton("8");
an9=new JButton("9");
an10=new JButton(".");
an11=new JButton("+");
an12=new JButton("-");
an13=new JButton("*");
an14=new JButton("/");
an15=new JButton("=");
an16=new JButton("0");
an17=new JButton("清空");
edit=new JTextField(10);
edit.setHorizontalAlignment(JTextField.RIGHT);
edit.setSize(300, 50);
MyActionListener myListener = new MyActionListener();
an1.addActionListener(myListener);
an2.addActionListener(myListener);
an3.addActionListener(myListener);
an4.addActionListener(myListener);
an5.addActionListener(myListener);
an6.addActionListener(myListener);
an7.addActionListener(myListener);
an8.addActionListener(myListener);
an9.addActionListener(myListener);
an10.addActionListener(myListener);
an11.addActionListener(myListener);
an12.addActionListener(myListener);
an13.addActionListener(myListener);
an14.addActionListener(myListener);
an15.addActionListener(myListener);
an16.addActionListener(myListener);
an17.addActionListener(myListener);
panel.add(edit);
panel.add(an1);
panel.add(an2);
panel.add(an3);
panel.add(an4);
panel.add(an5);
panel.add(an6);
panel.add(an7);
panel.add(an8);
panel.add(an9);
panel.add(an16);
panel.add(an10);
panel.add(an11);
panel.add(an12);
panel.add(an13);
panel.add(an14);
panel.add(an15);
JPanel pane = new JPanel();
pane.add(an17);
c.add(edit,BorderLayout.NORTH);
c.add(panel,BorderLayout.CENTER);
c.add(pane,BorderLayout.SOUTH);
jf.setResizable(false);
jf.setVisible(true);
}
private class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource() == an17){
strInfo = "";
edit.setText("");
}
else if(e.getSource() == an1){
strInfo += "1";
edit.setText(strInfo);
}
else if(e.getSource() == an2){
strInfo += "2";
edit.setText(strInfo);
}
else if(e.getSource() == an3){
strInfo += "3";
edit.setText(strInfo);
}
else if(e.getSource() == an4){
strInfo += "4";
edit.setText(strInfo);
}
else if(e.getSource() == an5){
strInfo += "5";
edit.setText(strInfo);
}
else if(e.getSource() == an6){
strInfo += "6";
edit.setText(strInfo);
}
else if(e.getSource() == an7){
strInfo += "7";
edit.setText(strInfo);
}
else if(e.getSource() == an8){
strInfo += "8";
edit.setText(strInfo);
}
else if(e.getSource() == an9){
strInfo += "9";
edit.setText(strInfo);
}
else if(e.getSource() == an16){
strInfo += "0";
edit.setText(strInfo);
}
else if(e.getSource() == an10){
strInfo += ".";
edit.setText(strInfo);
}
else if(e.getSource() == an11){
str = edit.getText();
if(str.equals("")){
first = 0.0;
}
else{
first = Double.parseDouble(str);
}
edit.setText("");
strInfo = "";
operator = "+";
}
else if(e.getSource() == an12){
str = edit.getText();
if(str.equals("")){
first = 0.0;
}
else{
first = Double.parseDouble(str);
}
strInfo = "";
edit.setText("");
operator = "-";
}
else if(e.getSource() == an13){
str = edit.getText();
if(str.equals("")){
first = 0.0;
}
else{
first = Double.parseDouble(str);
}
strInfo = "";
edit.setText("");
operator = "*";
}
else if(e.getSource() == an14){
str = edit.getText();
if(str.equals("")){
first = 0.0;
}
else{
first = Double.parseDouble(str);
}
strInfo = "";
edit.setText("");
operator = "/";
}
else if(e.getSource() == an15){
second = Double.parseDouble(edit.getText());
if(first == 0.0 || second == 0.0){
//弹出出错对话框
}
else{
if(operator.equals("+")){
result = first + second;
}
else if(operator.equals("-")){
result = first - second;
}
else if(operator.equals("*")){
result = first * second;
}
else if(operator.equals("/")){
result = first / second;
}
edit.setText((new Double(result)).toString());
}
}
}
}
}
----------------解决方案--------------------------------------------------------
class JTQ implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object a=e.getSource();
JButton t;
t=(JButton)a;
if(t.getText().equals("清零"))
{
String str1=t.getText();
Reader Reader=new StringReader(str1);
try{
edit.read(Reader,null);
edit.show();
}
catch(IOException ioe)
{
ioe.getStackTrace();
}
}
}
}
请高手修改
----------------解决方案--------------------------------------------------------
edit.setHorizontalAlignment(JTextField.RIGHT);
请问这个是什么方法,RIGHT是什么?
if(e.getSource() == an17){
strInfo = "";
edit.setText("");
}
e.getSource是获取什么?比AN17比较,strInfo=""是什么意思
strInfo += "7";这个又有什么用呢?还有这个first = Double.parseDouble(str);
请高手解释一下,谢谢
----------------解决方案--------------------------------------------------------
edit.setHorizontalAlignment(JTextField.RIGHT);
这个方法是用来设置文本框的显示格式,当取RIGHT时表示:文本框中的内容将从右边开始显示。其默认值是LEFT,从左边开始显示。
if(e.getSource() == an17){
strInfo = "";
edit.setText("");
}
在程序中我定义了字符串strInfo用来存放当前文本框中表示的操作数(用字符串表示,然后转换为double类型),当每按一下数字键时执行 strInfo += "对应数字键的数字" 使strInfo更新为当前的最新值.
知道了strInfo定义的意图后,相信你会明白strInfo = ""的含义了. 意思是说在清空按钮收到激活时间时,会用edit.setText("")把文本框清空,同时也要把strInfo表示的操作数也置成 "";
----------------解决方案--------------------------------------------------------
else if(e.getSource() == an15){
second = Double.parseDouble(edit.getText());
if(first == 0.0 || second == 0.0){
//弹出出错对话框
}
Double.parseDouble这个是什么
----------------解决方案--------------------------------------------------------
Double.parseDouble(String str)方法是将字符串解析为double类型的值。
你去网上下载个jdk帮助文档,有不懂得就拿来查就行了。这样才能很快的提高你的水平!
----------------解决方案--------------------------------------------------------