我在Java上的年龄计算程序遇到了一些麻烦。当我预设出生年份,出生月份和出生日期值时它工作正常,但当我尝试让用户将自己的生日输入到文本字段中然后尝试使用这些值时,我只是达到了死胡同。我尝试在Yahoo Answers上询问,我得到的提示是“getActionCommand()的返回值是一个字符串,而结果是JLabel。你能比较它们吗?”我不确定如何处理这个提示。
这就是我所拥有的以及我尝试实现整个“用户输入”理念的方式。我很确定我的编码是凌乱和低效的,所以请耐心等待。我很感激任何帮助!
//Date: April 11, 2012
//Description: Calculates the age in terms of days depending on your birthdate.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AgeCalculator extends Frame implements ActionListener {
JButton equal;
JTextField year, month, day;
JLabel result, first, second, third;
JFrame frame;
JPanel panel;
static int totaldaysalive;
static int daysaliveyr;
static int daysalivem;
static int birthyr;
static int birthm;
static int birthd;
static int currentyr = 2012;
public AgeCalculator(){
gui();
}
public void gui(){
frame = new JFrame ("Age Calculator");
panel = new JPanel(new GridBagLayout());
panel.setBackground(Color.LIGHT_GRAY);
GridBagConstraints x = new GridBagConstraints();
equal = new JButton ("Get Result");
x.insets = new Insets(3,0,3,0);
first = new JLabel("Year ");
x.gridx = 0;
x.gridx = 0;
panel.add(first, x);
year = new JTextField(10);
x.gridx = 5;
x.gridy = 0;
x.gridwidth = 3;
panel.add(year, x);
second = new JLabel ("Month ");
x.gridx = 0;
x.gridy = 1;
panel.add(second,x);
month = new JTextField(10);
x.gridx = 5;
x.gridy = 1;
x.gridwidth = 3;
panel.add(month,x);
third = new JLabel ("Day ");
x.gridx = 0;
x.gridy = 2;
panel.add(third,x);
day = new JTextField(10);
x.gridx = 5;
x.gridy = 2;
x.gridwidth = 3;
panel.add(day,x);
x.gridx = 6;
x.gridy = 3;
panel.add(equal,x);
result = new JLabel ("");
x.gridx = 5;
x.gridy = 5;
panel.add(result,x);
frame.add(panel);
frame.setVisible(true);
frame.setSize(350, 350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Calc e = new Calc();
equal.addActionListener(e);
year.addActionListener(e);
month.addActionListener(e);
day.addActionListener(e);
}
class Calc implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
birthyr = Integer.parseInt(year.getText());
} catch (NumberFormatException a) {
result.setText("Illegal data for first field.");
result.setForeground(Color.red);
return;
}
try {
birthm = Integer.parseInt(month.getText());
} catch (NumberFormatException a) {
result.setText("Illegal data for second field.");
result.setForeground(Color.red);
return;
}
try {
birthd = Integer.parseInt(day.getText());
} catch (NumberFormatException a) {
result.setText("Illegal data for third field.");
result.setForeground(Color.red);
return;
}
if (e.getActionCommand().equals (equal)){
totaldaysalive = ageCalcYr() + ageCalcM() + birthd;
result.setText(Integer.toString(totaldaysalive));
}
}
public int ageCalcYr(){
for (int i = birthyr; i <= currentyr; i++){
if ((i % 4 == 0) && (!(i % 100 == 0) || (i % 400 == 0))){
daysaliveyr = daysaliveyr + 366;
}
else {
daysaliveyr = daysaliveyr + 365;
}
}
return daysaliveyr;
}
public int ageCalcM(){
if (birthm == 1){
daysalivem = daysalivem + 0;
}
else if (birthm == 2){
daysalivem = daysalivem + 30;
}
else if (birthm == 3){
daysalivem = daysalivem + 60;
}
else if (birthm == 4){
daysalivem = daysalivem + 90;
}
else if (birthm == 5){
daysalivem = daysalivem + 120;
}
else if (birthm == 6){
daysalivem = daysalivem + 150;
}
else if (birthm == 7){
daysalivem = daysalivem + 180;
}
else if (birthm == 8){
daysalivem = daysalivem + 210;
}
else if (birthm == 9){
daysalivem = daysalivem + 240;
}
else if (birthm == 10){
daysalivem = daysalivem + 270;
}
else if (birthm == 11){
daysalivem = daysalivem + 300;
}
else if (birthm == 12){
daysalivem = daysalivem + 330;
}
return daysalivem;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
AgeCalculator gui = new AgeCalculator();
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:3)
快速治愈:
if (e.getActionCommand ().equals ("Get Result")) { // equal)) {
totaldaysalive = ageCalcYr () + ageCalcM () + birthd;
result.setText (Integer.toString (totaldaysalive));
}
如果你有时间,我可以给你发20条改进。
public class AgeCalculator // extends Frame implements ActionListener
接下来是一组可视组件和其他属性,后者是静态的 - 它禁止在同一个JVM上使用2个AgeCalculator。这肯定不是意图的限制。
JButton equal; JTextField year, month, day; JLabel result, ... ... static int birthd; static int currentyr = 2012;
在适当情况下使用简化添加:
daysaliveyr += 366;
要计算月份的天数,请将出生日和出生日作为参数: int totaldaysalive = ageCalcYr(birthyr)+ ageCalcM(birthm)+ birthd; result.setText(Integer.toString(totaldaysalive));
变量totaldaysalive的实时时间可以减少到2行 - 一个非常小的搜索错误的范围,如果有的话。
public int ageCalcM (int birthm) {
int daysalivem = 0;
if (birthm == 2) {
daysalivem += 30;
}
else if (birthm == 3) {
daysalivem += 60;
}
在当前状态下,ageCalcM是一个附带条件。否则你可以说daysalivem = (birthm - 1) * 30;
更短的代码:
public int ageCalcM (int birthm) {
if (birthm == 2) {
return 30;
}
else if (birthm == 3) {
return 60;
}
然而,愚蠢重复中的这种大规模操作可以用一个简单的数组来解决:
public int ageCalcM (int birthm) {
int[] mdays = {0, 30, 60, 90, ...};
return mdays [birthm];
}
在main-Method中创建一个从未使用的实例'gui'。这就是你所需要的:
public static void main(String [] args){ new AgeCalculator(); }
桂,顺便说一句。如果你已经有了这个名字的方法,那就是一个坏名字。
还剩下什么?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AgeCalculator
{
JTextField year, month, day;
JLabel result;
public AgeCalculator () {
JFrame frame = new JFrame ("Age Calculator");
JPanel panel = new JPanel (new GridBagLayout ());
panel.setBackground (Color.LIGHT_GRAY);
GridBagConstraints x = new GridBagConstraints ();
JButton equal = new JButton ("Get Result");
x.insets = new Insets (3, 0, 3, 0);
JLabel first = new JLabel ("Year ");
// two times gridx = 0 here?
x.gridx = 0;
x.gridx = 0;
panel.add (first, x);
year = new JTextField (10);
x.gridx = 5;
x.gridy = 0;
x.gridwidth = 3;
panel.add (year, x);
JLabel second = new JLabel ("Month ");
x.gridx = 0;
x.gridy = 1;
panel.add (second, x);
month = new JTextField (10);
x.gridx = 5;
x.gridy = 1;
x.gridwidth = 3;
panel.add (month, x);
JLabel third = new JLabel ("Day ");
x.gridx = 0;
x.gridy = 2;
panel.add (third, x);
day = new JTextField (10);
x.gridx = 5;
x.gridy = 2;
x.gridwidth = 3;
panel.add (day, x);
x.gridx = 6;
x.gridy = 3;
panel.add (equal, x);
result = new JLabel ("");
x.gridx = 5;
x.gridy = 5;
panel.add (result, x);
frame.add (panel);
frame.setVisible (true);
frame.setSize (350, 350);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Calc e = new Calc ();
equal.addActionListener (e);
}
class Calc implements ActionListener {
public void actionPerformed (ActionEvent e) {
int birthyr;
int birthm;
int birthd;
try {
birthyr = Integer.parseInt (year.getText ());
} catch (NumberFormatException a) {
result.setText ("Illegal data for first field.");
result.setForeground (Color.red);
return;
}
try {
birthm = Integer.parseInt (month.getText ());
} catch (NumberFormatException a) {
result.setText ("Illegal data for second field.");
result.setForeground (Color.red);
return;
}
try {
birthd = Integer.parseInt (day.getText ());
} catch (NumberFormatException a) {
result.setText ("Illegal data for third field.");
result.setForeground (Color.red);
return;
}
if (e.getActionCommand ().equals ("Get Result")) { // equal)) {
int totaldaysalive = ageCalcYr (birthyr) + ageCalcM (birthm) + birthd;
result.setText (Integer.toString (totaldaysalive));
}
}
public int ageCalcYr (int birthyr) {
int currentyr = 2012;
int daysaliveyr = 0;
for (int i = birthyr; i <= currentyr; i++) {
if ((i % 4 == 0) && (! (i % 100 == 0) || (i % 400 == 0))) {
daysaliveyr += 366;
}
else {
daysaliveyr += 365;
}
}
return daysaliveyr;
}
public int ageCalcM (int birthm) {
int[] mdays = {0, 30, 60, 90, 120};
return mdays [birthm];
}
}
public static void main (String [] args) {
new AgeCalculator ();
}
}
答案 1 :(得分:0)
我想你想做equal.addMouseListener(e)
。当然,您必须更改Calc
才能实施MouseListener
。您可能只需要实际编写mouseClicked(MouseEvent)
方法。所有其他的都是比你所追求的更具体的东西。
这将响应按钮上的点击事件。我认为你不想要任何其他听众。如果是,则应为KeyListeners
或ActionListeners
以外的其他内容。
另一方面,我很难看到因为你的缩进已经关闭,但我无法确切说明为什么int
字段为static
。我认为这可能是不必要的。
答案 2 :(得分:0)
由于您使用按钮开始计算,因此只在该按钮上注册一个动作侦听器。在动作执行方法内部读取,解析并计算以天为单位的年龄。