我被要求在我的 Java 程序中使用 RadioButtons 和 Checkboxes 以便用户可以轻松地选择他们想要使用的选项(这是一个“加油站”)。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class New_Gas_Bar extends JFrame
{
public JPanel panel1,panel2,panel3,panel4,panel5;
public JLabel main1, main2, main3;
public JLabel gasBar,total;
public JButton button1,button2,button3,button4;
public JRadioButton bronzeG,silverG,goldG,selfS,fullS,fullService,selfService;
public JCheckBox oilC,windWash,windClean;
static double fullCost,selfCost;
public New_Gas_Bar()
{
super (" New Gas Bar");
setSize(640,640);
Container container = getContentPane();
panel1=new JPanel();
panel2=new JPanel();
panel1.setBackground(new Color(107,202,226));
panel1.setLayout(new GridLayout(7,1));
main1 = new JLabel (" Gas Bar Project ");
main2 = new JLabel (" ICS4U0 - September 2013");
main3 = new JLabel (" ");
button1 = new JButton ("Gas Station");
button2 = new JButton ("Car Wash");
button3 = new JButton ("Total");
button4 = new JButton ("Exit");
panel2.setBackground(new Color(144,160,170));
panel2.setLayout(new GridLayout(4,1));
panel2.add (button1);
panel2.add (button2);
panel2.add (button3);
panel2.add (button4);
panel1.add (main1);
panel1.add (main2);
//panel2.add(gasBar);
//panel3.add(total);
container.setLayout(new BorderLayout());
container.add(panel1,BorderLayout.NORTH);
container.add(panel2,BorderLayout.CENTER);
ButtonHandler handler = new ButtonHandler ();
button1.addActionListener (handler);
button2.addActionListener (handler);
button3.addActionListener (handler);
button4.addActionListener (handler);
ButtonGroup serveStyle = new ButtonGroup();
fullS = new JRadioButton ("Full Serve");
selfS = new JRadioButton ("Self Serve");
serveStyle.add (fullS);
serveStyle.add (selfS);
ButtonGroup serveGas = new ButtonGroup();
bronzeG = new JRadioButton("Bronze Service");
silverG = new JRadioButton("Silver Service");
goldG = new JRadioButton("Gold Service");
serveGas.add (bronzeG);
serveGas.add (silverG);
serveGas.add (goldG);
oilC = new JCheckBox("Oil Change");
windWash = new JCheckBox("Windshield Wash");
windClean = new JCheckBox("Windshield Cleaning");
RadioButtonHandler radioHand = new RadioButtonHandler ();
bronzeG.addItemListener (radioHand);
CheckBoxHandler checkHand = new CheckBoxHandler();
oilC.addItemListener (checkHand);
setVisible(true);
pack();
}
public static void main (String [] args)
{
New_Gas_Bar application = new New_Gas_Bar();
}
public class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == button1)
{
panel1.setVisible(true);
panel2.setVisible(false);
panel3.setVisible(true);
panel4.setVisible(false);
panel5.setVisible(false);
}
else if (event.getSource () == button2)
{
}
else if (event.getSource () == button3)
{
}
else if (event.getSource () == button4)
{
System.exit(0);
}
}
}
public class RadioButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == fullS)
{
}
else if (event.getSource () == selfS)
{
}
if (event.getSource () == bronzeG)
{
}
else if (event.getSource () == silverG)
{
}
else if (event.getSource () == goldG)
{
}
}
}
public class CheckBoxHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == checkBox1)
{
}
else if (event.getSource () == checkBox2)
{
}
else if (event.getSource () == checkBox3)
{
}
}
}
}
}
我得到的错误是在这部分
RadioButtonHandler radioHand = new RadioButtonHandler ();
bronzeG.addItemListener (radioHand);
CheckBoxHandler checkHand = new CheckBoxHandler();
oilC.addItemListener (checkHand);
我不明白为什么在bronzeG.addItemListener,它说
addItemListener(java.awt.event.ItemListener) in javax.swing.AbstractButton cannot be applied to (New_Gas_Bar.RadioButtonHandler)
最佳答案
addItemListener期望将 ItemListener 作为参数传递。您正在传递一个扩展 ActionListener
代替
bronzeG.addItemListener(radioHand);
使用
bronzeG.addActionListener(radioHand);
关于java - 不确定这是什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18742586/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串