else
if( p==\ else
if(p==\
else if (p==”正”)
{value=Math.abs(Float.parseFloat(t1.getText())); t1.setText(String.valueOf(value)); count-=2;}
else if(p==\负\ t1.setText(String.valueOf(value));count-=2;} t1.setText(String.valueOf(value));value=0;}} }
public static void main(String[] args) { new Calulator(); }}
2.import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class ZhuanH extends JFrame implements ActionListener{ TextField t1,t2,t3,t4; public ZhuanH()
{super(\十进制整数转换\
this.setBackground(Color.BLUE); t1=new TextField(5); t2=new TextField(5); t3=new TextField(5); t4=new TextField(5); t1.setText(null);
this.setSize(400,200);
this.setLayout(new GridLayout(4,2)); this.add(new Label(\十进制\
this.add(t1);t1.addActionListener(this); this.add(new Label(\二进制\this.add(new Label(\八进制\this.add(new Label(\十六进制\this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); }
public void actionPerformed(ActionEvent e){ String c = t1.getText();
t2.setText(Integer.toBinaryString(Integer.parseInt(c))); t3.setText(Integer.toOctalString(Integer.parseInt(c)));
34
t4.setText(Integer.toHexString(Integer.parseInt(c))); }
public static void main(String[] args) { new ZhuanH(); }}
3.import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class PingFen extends JFrame implements ActionListener{ JPanel p,p1; JTextField t;
JTextField t1[]=new JTextField[10]; JButton b; float s=0,k; int t2,t3;
public PingFen() {
super(\模拟裁判评分\ this.setSize(300,120);
p1=new JPanel(new GridLayout(2,5)); this.add(p1,\
p=new JPanel(new FlowLayout(FlowLayout.RIGHT)); this.add(p,\ b=new JButton(\平均分\ t=new JTextField(10); p.add(b);p.add(t); for(int i=0;i<10;i++) {
t1[i]=new JTextField(6); t1[i].setText(null);
p1.add(t1[i]); }
b.addActionListener(this);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true);}
public void actionPerformed(ActionEvent e) {float max,min;
for(int i=0;i<10;i++) { try{
if(Float.parseFloat(t1[i].getText())>10.0) throw new Exception(); else continue;} catch(Exception ev)
{JOptionPane.showMessageDialog(this,t1[i].getText()+\超出范围\
35
请重新输入\
t1[i].setText(null);} }
max=Float.parseFloat(String.valueOf(t1[0].getText())); min=Float.parseFloat(String.valueOf(t1[0].getText())); for(int i=1;i<10;i++) {
if((k=Float.parseFloat(String.valueOf(t1[i].getText())))>max){max=k;t2=i;} else if
((k=Float.parseFloat(String.valueOf(t1[i].getText()))) for(int i=0;i<10;i++) { if(t2!=i&&i!=t3) s+=Float.parseFloat(String.valueOf(t1[i].getText()));} t.setText(s/10.0+\ public static void main(String[] args) { new PingFen();}} 4. import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class MoonApplet extends Applet implements MouseListener { int x,y; public void init(){ x=this.getWidth()/4; y=this.getHeight()/4; addMouseListener(this); } public void paint(Graphics g) //在Applet上绘图 { g.setColor(Color.red); g.drawString(\ int diameter = Math.min(this.getWidth()/2, this.getHeight()/2); //圆的直径 g.setColor(Color.yellow); g.fillOval(x,y,diameter,diameter); //画圆 g.setColor(this.getBackground()); //设置为背景色 g.fillOval(x-20,y-20,diameter,diameter); //画圆 } public void mouseClicked(MouseEvent e){ 36 x=e.getX(); y=e.getY(); repaint(); } 5. package package2; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LuoXuan extends JFrame implements ActionListener{ private static final long serialVersionUID = 1L; private LuoXuanCanvas canvas; //自定义画布组件 public LuoXuan() {super(\阿基米德螺线\ Dimension dim=getToolkit().getScreenSize(); this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/2); JPanel p=new JPanel(); this.add(p,\ JButton b=new JButton(\选择颜色\p.add(b); b.addActionListener(this); this.canvas=new LuoXuanCanvas(); this.getContentPane().add(this.canvas, \this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true);} public void actionPerformed(ActionEvent e) {Color c=JColorChooser.showDialog(this, \选择颜色\ this.canvas.setColor(c); this.canvas.repaint();} public static void main(String[] args) { new LuoXuan();} } class LuoXuanCanvas extends Canvas {private Color color; public void LuoXuan(Color color) {this.setColor(color);} public void setColor(Color color){this.color=color;} public void paint(Graphics g) {int x0=this.getWidth()/2; int y0 = this.getHeight()/2; g.setColor(this.color); g.drawLine(x0, 0, x0, y0*2); g.drawLine(0, y0, x0*2, y0); 37 for(int i=0;i<4000;i++) {double angle=i*Math.PI/512; double radius=angle*0.5; int x=(int)Math.round(radius*angle*Math.cos(angle)); int y=(int)Math.round(radius*angle*Math.sin(angle)); g.drawOval(x0+x, y0+y, 1, 1);} }} 四、实验结果与分析(程序运行结果及其分析) (分析每题采用的布局管理器、事件处理类和主要功能实现方法) 1. 2. 3. 4. 38