欢迎来到天天文库
浏览记录
ID:39550078
大小:145.00 KB
页数:6页
时间:2019-07-06
《java作业图形用户界面》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第一题:importjavax.swing.JFrame;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassFahrenheit{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubJFrameframe=newJFrame("Fahrenheit");frame.setDefaultCloseOperation(JFrame.EXIT_ON_C
2、LOSE);FahrenheitPanelpanel=newFahrenheitPanel();frame.getContentPane().add(panel);frame.pack();frame.setVisible(true);}}classFahrenheitPanelextendsJPanel{privateJLabelinputLabel,outputLabel,resultLabel;privateJButtonOK;privateJTextFieldfahrenheit;publicFahrenheitPanel(){inpu
3、tLabel=newJLabel("EnterFahrenheittemperature:");outputLabel=newJLabel("TemperatureinCelsius:");resultLabel=newJLabel("---");OK=newJButton("OK!");fahrenheit=newJTextField(5);fahrenheit.addActionListener(newTempListener());OK.addActionListener(newTempListener());add(inputLabel
4、);add(fahrenheit);add(outputLabel);add(resultLabel);add(OK);setPreferredSize(newDimension(300,75));setBackground(Color.green);}privateclassTempListenerimplementsActionListener{publicvoidactionPerformed(ActionEventevent){intfahrenheitTemp,celsiusTemp;Stringtext=fahrenheit.get
5、Text();fahrenheitTemp=Integer.parseInt(text);celsiusTemp=(fahrenheitTemp-31)*5/9;resultLabel.setText(Integer.toString(celsiusTemp));}}}第二题:importjavax.swing.JFrame;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassRandom{/***@paramargs*/publicstaticvoidm
6、ain(String[]args){JFrameframe=newJFrame("Randomgame");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);RandomPanelpanel=newRandomPanel();frame.getContentPane().add(panel);frame.pack();frame.setVisible(true);}}classRandomPanelextendsJPanel{privateJLabelresultLabel;private
7、intrandomNumber;privateJButtonOK;publicRandomPanel(){resultLabel=newJLabel("---");OK=newJButton("确定!");OK.addActionListener(newButtonListener());add(resultLabel);add(OK);setPreferredSize(newDimension(300,100));setBackground(Color.blue);}privateclassButtonListenerimplementsAc
8、tionListener{privatefinalintBASE=100;publicvoidactionPerformed(ActionEvente
此文档下载收益归作者所有