Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计 » Swing  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 高手请进------这个程序有点小错误!
guliusuo





发贴: 28
积分: 0
于 2007-05-24 10:13 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.awt.event.*;
public class WageCalculator extends JFrame
{
  private JLabel hourlyWageJLabel;
  private JTextField hourlyWageJTextField;
  
  private JLabel hoursWorkedJLabel;
  private JTextField hoursWorkedJTextField;
  
  private JLabel grossWagesJLabel;
  private JTextField grossWagesJTextField;
  
  private JButton calculateJButton;
  
  double hourlyWage;
  double hoursWorked;
  double wages;
  final double HOUR_LIMIT=40.0;
  DecimalFormat dollars;
  
  public WageCalculator()
  {
    createUserInterface();
  }
  public void createUserInterface()
  {
    Container contentPane=this.getContentPane();
    contentPane.setLayout(null);
    
    hourlyWageJLabel=new JLabel();
    hourlyWageJLabel.setBounds(16,16,90,21);
    hourlyWageJLabel.setText("Hourly wage");
    contentPane.add(hourlyWageJLabel);
    
    hourlyWageJTextField=new JTextField();
    hourlyWageJTextField.setBounds(115,16,90,21);
    hourlyWageJTextField.setHorizontalAlignment(JTextField.RIGHT);
    contentPane.add(hourlyWageJTextField);
    hourlyWageJTextField.addKeyListener(
      new KeyAdapter()
      {
        public void keyPressed(KeyEvent Event)
        {
          hourlyWageJTextFieldKeyPressed(Event);
        }
      });
    
    hoursWorkedJLabel=new JLabel();
    hoursWorkedJLabel.setBounds(16,57,90,21);
    hoursWorkedJLabel.setText("Hours worked");
    contentPane.add(hoursWorkedJLabel);
    
    hoursWorkedJTextField=new JTextField();
    hoursWorkedJTextField.setBounds(115,57,90,21);
    hoursWorkedJTextField.setHorizontalAlignment(JTextField.RIGHT);
    contentPane.add(hoursWorkedJTextField);
    hoursWorkedJTextField.addKeyListener(
      new KeyAdapter()
      {
        public void KeyPressed(KeyEvent Event)
        {
          hoursWorkedJTextFieldKeyPressed(Event);
        }
      });
    
    grossWagesJLabel=new JLabel();
    grossWagesJLabel.setBounds(16,97,90,21);
    grossWagesJLabel.setText("Gross wages");
    contentPane.add(grossWagesJLabel);
    
    grossWagesJTextField=new JTextField();
    grossWagesJTextField.setBounds(115,97,90,21);
    grossWagesJTextField.setHorizontalAlignment(JTextField.RIGHT);
    grossWagesJTextField.setEditable(false);
    contentPane.add(grossWagesJTextField);
    
    calculateJButton=new JButton();
    calculateJButton.setBounds(115,135,90,21);
    calculateJButton.setText("Calculate");
    contentPane.add(calculateJButton);
    calculateJButton.addActionListener(
      new ActionListener()
      {
        public void actionPerformed(ActionEvent Event)
        {
          calculateJButtonActionPerformed(Event);
        }
      });
    
    setTitle("Wage Calculator");
    setSize(230,200);
    setLocation(150,200);
    setVisible(true);
  }
  private void calculateJButtonActionPerformed(ActionEvent e)
  {
    hourlyWage=Double.parseDouble(hourlyWageJTextField.getText());
    hoursWorked=Double.parseDouble(hoursWorkedJTextField.getText());
    if(hoursWorked<=HOUR_LIMIT)
    {
      wages=hourlyWage*hoursWorked;
    }
    else
    {
      wages=hourlyWage*HOUR_LIMIT;
      wages+=(hoursWorked-HOUR_LIMIT)*hourlyWage*1.5;
    }
    dollars=new DecimalFormat("$0.00");
    grossWagesJTextField.setText(dollars.format(wages));
  }
  
  private void hourlyWageJTextFieldKeyPressed(KeyEvent e)
  {
    grossWagesJTextField.setText("");
  }
  
  private void hoursWorkedJTextFieldKeyPressed(KeyEvent e)
  {
    grossWagesJTextField.setText("");
  }
  
  public static void main(String args[])
  {
    WageCalculator application=new WageCalculator();
    application.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
}

程序运行界面:

问题:
为何向第一个TextField中输入内容后,能将第三个TextField的内容清空;而向第二个TextField中输入内容时,第三个TextField的内容不能清空,从我写代码来看,应该都是对的啊?

请求帮助!!


guliusuo edited on 2007-05-24 10:43

作者 Re:高手请进------这个程序有点小错误! [Re:guliusuo]
guliusuo





发贴: 28
积分: 0
于 2007-05-24 16:16 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
在线等……


作者 Re:高手请进------这个程序有点小错误! [Re:guliusuo]
snowbug



CJSDN高级会员


发贴: 418
积分: 130
于 2007-05-31 04:03 user profilesend a private message to usersend email to snowbugsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

hoursWorkedJTextField.addKeyListener(new KeyAdapter() {
public void KeyPressed(KeyEvent Event) {
hoursWorkedJTextFieldKeyPressed(Event);
}
});


应该是小写

如果你用的是 eclipse IDE 的话,就应该看到这行上有个警告。




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923