下面的代码是项目的动作侦听器。基本上,我有 4 个单选按钮,当我单击一个时,我希望它更改屏幕上的变量。当我运行代码时,它只是将所有值相加。还有其他方法可以做到这一点吗?
class Calc implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double base = 0.00;
double options;
double total;
if (Button25.isSelected());
{
base = base + 999.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
if (Button32.isSelected());
{
base = base + 1049.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
if (Button35.isSelected());
{
base = base + 1099.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
if (Button42.isSelected());
{
base = base + 1155.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
}
}
问题是,对于每个< code>if()语句,比如if(button 32 . is selected());,你有一个<代码>;符号在末尾。这不应该在那里。这是正确的代码...
class Calc implements ActionListener {
public void actionPerformed(ActionEvent event){
double base = 0.00;
double options;
double total;
if (Button25.isSelected()){ // changed
base = base + 999.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
else if (Button32.isSelected()){ // changed
base = base + 1049.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
else if (Button35.isSelected()){ // changed
base = base + 1099.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
else if (Button42.isSelected()){ // changed
base = base + 1155.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
}
}
作为替代方案,为什么不获取从Action事件
中单击的按钮,然后在if-否则分支中使用该按钮……
class Calc implements ActionListener {
public void actionPerformed(ActionEvent event){
double base = 0.00;
double options;
double total;
Object clickedObject = event.getSource();
if (clickedObject instanceof JRadioButton){
JRadioButton clickedButton = (JRadioButton)clickedObject;
if (clickedButton == Button25){
base = base + 999.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
else if (clickedButton == Button32){
base = base + 1049.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
else if (clickedButton == Button35){
base = base + 1099.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
else if (clickedButton == Button42){
base = base + 1155.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
}
}
}
导入javax.swing.*; class Labels extensions JFrame{ JPanel pnl = new JPanel(); } 如果我想将其用作JApplet怎么办?必须做什么?很难更改吗? JFrame上运行的东西和JApplet上的东西是一样的吗?
@override public void actionPerformed(ActionEvent e){ }
我想根据单选按钮的选择设置文本框的可编辑选项?如何对单选按钮上的动作监听器进行编码?
我目前正在尝试对我的实现一个操作侦听器,以便在选择它时,它将打开一个供用户选择他们希望GUI使用的文件。对于初学者,我如何让控制台打印出“框单击!”当用户选中该框时? 它已经有一段时间,因为我已经在摇摆编程,所以任何建议都有帮助!
我正在处理JButton事件。我有一个JPanel类,我们叫Panel1,包含一个公共的JButton,我们叫它Button1。单击此按钮时: 但我有兴趣: 有什么建议吗?
我有一个简单的HibernateInterceptor,基本上我想自动设置几个字段。此拦截器(如下所示)扩展了EmptyInterceptor: 我使用spring配置文件进行布线,如下所示: 但是,永远无法到达拦截器。有人有什么线索吗?我还尝试将以下内容添加到事务管理器bean定义中,如下所示: