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

您没有登录

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

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 问一个JAVA编程题
wwwhahchn





发贴: 5
积分: 0
于 2006-05-06 12:28 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
编写程序,计算销售总额和提成,如图所示。用户在Sales Amount文本域中键入销售总额并按下回车键后,Commissions文本域中显示提成。类似的,用户键入提成后,显示相应的销售总额

我写的程序:
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;

public class Exercise_9_10 extends JFrame implements ActionListener
{
private JTextField jtfSalesAmount, jtfCommissions;

public static void main(String[] args)
{
Exercise_9_10 frame = new Exercise_9_10();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public Exercise_9_10()
{
setTitle("Exercise 9.10");

JLabel jlblInputSalesAmount = new JLabel("Sales Amount");
JLabel jlblInputCommissions = new JLabel("Commissions");
JLabel jlblSalesAmount = new JLabel("Sales Amount");
JLabel jlblCommissionRate = new JLabel("Commission Rate");
JLabel jlblSA1 = new JLabel("$1 to $5,000");
JLabel jlblSA2 = new JLabel("$5,000 to $10,000");
JLabel jlblSA3 = new JLabel("Above $10,000");
JLabel jlblCR1 = new JLabel("8 Percent");
JLabel jlblCR2 = new JLabel("10 Percent");
JLabel jlblCR3 = new JLabel("12 Percent");

jtfSalesAmount = new JTextField(15);
jtfCommissions = new JTextField(15);

JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 2));
p1.add(jlblInputSalesAmount);
p1.add(jtfSalesAmount);
p1.add(jlblInputCommissions);
p1.add(jtfCommissions);
p1.setBorder(new TitledBorder("Sales Amount and Commissions"));

JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(4, 2));
p2.add(jlblSalesAmount);
p2.add(jlblCommissionRate);
p2.add(jlblSA1);
p2.add(jlblCR1);
p2.add(jlblSA2);
p2.add(jlblCR2);
p2.add(jlblSA3);
p2.add(jlblCR3);
p2.setBorder(new TitledBorder("Commission Rate"));

getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.CENTER);
getContentPane().add(p2, BorderLayout.SOUTH);

jtfSalesAmount.addActionListener(this);
jtfCommissions.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
double salesAmount, commission;

if (e.getSource() == jtfSalesAmount)
if (jtfSalesAmount.getText().trim() != null)
{
salesAmount = Double.parseDouble(jtfSalesAmount.getText().trim());

if (salesAmount > 10000)
commission = 5000*0.08+5000*0.1+(salesAmount-10000)*0.12;
else if (salesAmount > 5000)
commission = 5000*0.08+(salesAmount-5000)*0.1;
else
commission = salesAmount*0.08;

jtfCommissions.setText(String.valueOf(commission));
}
else if (e.getSource() == jtfCommissions)
if (jtfCommissions.getText().trim() != null)
{
commission = Double.parseDouble(jtfCommissions.getText().trim());

if (commission > 900)
salesAmount = 10000+(commission-900)/0.12;
else if (commission > 400)
salesAmount = 5000+(commission-400)/0.10;
else
salesAmount = commission/0.08;

jtfSalesAmount.setText(String.valueOf(salesAmount));
}
}
}


现在运行的结果就是,可以做到“用户在Sales Amount文本域中键入销售总额并按下回车键后,Commissions文本域中显示提成”,但是做不到“用户键入提成后,显示相应的销售总额”,键入提成后按下回车键,程序没反应

偶不知道问题出在哪里,请NN帮忙看一下,3q


wwwhahchn edited on 2006-05-06 12:33


关于女孩子从事软件开发的问题
作者 Re:问一个JAVA编程题 [Re:wwwhahchn]
sunjavaduke

乒乓球国手-张怡宁



发贴: 176
积分: 6
于 2006-05-06 13:21 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 java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;

public class Exercise_9_10 extends JFrame implements ActionListener
{
private JTextField jtfSalesAmount, jtfCommissions;

public static void main(String[] args)
{
Exercise_9_10 frame = new Exercise_9_10();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public Exercise_9_10()
{
setTitle("Exercise 9.10");

JLabel jlblInputSalesAmount = new JLabel("Sales Amount");
JLabel jlblInputCommissions = new JLabel("Commissions");
JLabel jlblSalesAmount = new JLabel("Sales Amount");
JLabel jlblCommissionRate = new JLabel("Commission Rate");
JLabel jlblSA1 = new JLabel("$1 to $5,000");
JLabel jlblSA2 = new JLabel("$5,000 to $10,000");
JLabel jlblSA3 = new JLabel("Above $10,000");
JLabel jlblCR1 = new JLabel("8 Percent");
JLabel jlblCR2 = new JLabel("10 Percent");
JLabel jlblCR3 = new JLabel("12 Percent");

jtfSalesAmount = new JTextField(15);
jtfCommissions = new JTextField(15);

JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(2, 2));
p1.add(jlblInputSalesAmount);
p1.add(jtfSalesAmount);
p1.add(jlblInputCommissions);
p1.add(jtfCommissions);
p1.setBorder(new TitledBorder("Sales Amount and Commissions"));

JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(4, 2));
p2.add(jlblSalesAmount);
p2.add(jlblCommissionRate);
p2.add(jlblSA1);
p2.add(jlblCR1);
p2.add(jlblSA2);
p2.add(jlblCR2);
p2.add(jlblSA3);
p2.add(jlblCR3);
p2.setBorder(new TitledBorder("Commission Rate"));

getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.CENTER);
getContentPane().add(p2, BorderLayout.SOUTH);

jtfSalesAmount.addActionListener(this);
jtfCommissions.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
double salesAmount, commission;

if (e.getSource() == jtfSalesAmount)
{
if (jtfSalesAmount.getText().trim() != null)
{
salesAmount = Double.parseDouble(jtfSalesAmount.getText().trim());

if (salesAmount > 10000)
commission = 5000*0.08+5000*0.1+(salesAmount-10000)*0.12;
else if (salesAmount > 5000)
commission = 5000*0.08+(salesAmount-5000)*0.1;
else
commission = salesAmount*0.08;

jtfCommissions.setText(String.valueOf(commission));
}
}
else if (e.getSource() == jtfCommissions)
{
if (jtfCommissions.getText().trim() != null)
{
commission = Double.parseDouble(jtfCommissions.getText().trim());

if (commission > 900)
salesAmount = 10000+(commission-900)/0.12;
else if (commission > 400)
salesAmount = 5000+(commission-400)/0.10;
else
salesAmount = commission/0.08;

jtfSalesAmount.setText(String.valueOf(salesAmount));
}
}
}
}



-----------------------------------------------------------------
icd.Neusoft Co,.Ltd.
mail:zhangzhongl@neusoft.com
tel:13591718127
QQ:176932855
------------------------------------------------------------------

关于 异常 。。。。 谢谢指教
作者 Re:问一个JAVA编程题 [Re:wwwhahchn]
sunjavaduke

乒乓球国手-张怡宁



发贴: 176
积分: 6
于 2006-05-06 13:27 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
现在可以运行了
说明
在事件处理中public void actionPerformed(ActionEvent e)
{
你的if语句是有问题的,也就是说,当事件发生的初识对象是jtfSalesAmount时,执行下面的操作,由于if语句没有些语句块的标志{},所以if的体为紧跟着它的那一条语句,即下一个
if (jtfSalesAmount.getText().trim() != null)
{
salesAmount = Double.parseDouble(jtfSalesAmount.getText().trim());

if (salesAmount > 10000)
commission = 5000*0.08+5000*0.1+(salesAmount-10000)*0.12;
else if (salesAmount > 5000)
commission = 5000*0.08+(salesAmount-5000)*0.1;
else
commission = salesAmount*0.08;

jtfCommissions.setText(String.valueOf(commission));
}

下面你就应该懂了,if-----else if的配对原则你应该知道吧,所以elseif是与你的内层的if配对,而不是与判断是否事件发生对象那个if配对
}
还有,建议你对输入框中输入为空时进行一下处理,否则会有异常发生,造成程序停止,比如给出用户提示“输入为空”,或者输入为空时,将提成设置成0。
建议而已。



-----------------------------------------------------------------
icd.Neusoft Co,.Ltd.
mail:zhangzhongl@neusoft.com
tel:13591718127
QQ:176932855
------------------------------------------------------------------

如何用JTable设置表头多行
作者 Re:问一个JAVA编程题 [Re:wwwhahchn]
wwwhahchn





发贴: 5
积分: 0
于 2006-05-06 14:33 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
哇,非常感谢,原来是配对出问题了,这下明白了

你的建议很对,我还是初学,没注意到,谢谢

严重感谢Thumbs up




加入CJSDN真好!
作者 Re:问一个JAVA编程题 [Re:wwwhahchn]
sunjavaduke

乒乓球国手-张怡宁



发贴: 176
积分: 6
于 2006-05-06 15:10 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
learn from each other~


-----------------------------------------------------------------
icd.Neusoft Co,.Ltd.
mail:zhangzhongl@neusoft.com
tel:13591718127
QQ:176932855
------------------------------------------------------------------

Eclipse開發平台快速入門

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