1、Pp5.15 统计元音字母个数package Statistics_aeiou;import java.awt.Dimension;import java.awt.Toolkit;import javax.swing.JFrame;public class Statistics public static void main(String args) JFrame frame=new JFrame(“元音字母统计柱状图“);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(500,500);Dimension
2、screenSize=Toolkit.getDefaultToolkit().getScreenSize();int x=(screenSize.width-frame.getWidth()/2;int y=(screenSize.height-frame.getHeight()/2;frame.setLocation(x, y);AeiouPanel panel=new AeiouPanel();frame.getContentPane().add(panel);frame.pack();frame.setVisible(true);package Statistics_aeiou;impo
3、rt java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.JButton
4、;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextField;public class AeiouPanel extends JPanelprivate JLabel input,label1,label2,label3,label4,label5,label6;private JTextField text;private JButton sure,exit;private JPanel panel,panel1;private final int N=60,WITH=30;public
5、AeiouPanel()input =new JLabel(“Input Character:“);text =new JTextField(15);sure =new JButton(“确定“);exit =new JButton(“退出 “);panel =new JPanel();panel1 =new JPanel();label1=new JLabel(“a:“);label2=new JLabel(“e:“);label3=new JLabel(“i:“);label4=new JLabel(“o:“);label5=new JLabel(“u:“);label6=new JLab
6、el(“其它字符:“);AeiouListener listener=new AeiouListener();sure.addActionListener(listener);exit.addActionListener(listener);panel.setLayout(new FlowLayout();panel.setBackground(Color.cyan);panel.add(input);panel.add(text);panel.add(sure);panel.add(exit);panel1.setLayout(new BoxLayout(panel1,BoxLayout.Y
7、_AXIS);panel1.setBackground(Color.cyan);panel1.add(label1);panel1.add(Box.createRigidArea(new Dimension(0,20);panel1.add(label2);panel1.add(Box.createRigidArea(new Dimension(0,20);panel1.add(label3);panel1.add(Box.createRigidArea(new Dimension(0,20);panel1.add(label4);panel1.add(Box.createRigidArea(
8、new Dimension(0,20);panel1.add(label5);panel1.add(Box.createRigidArea(new Dimension(0,20);panel1.add(label6);setLayout(new BorderLayout();add(panel,BorderLayout.NORTH);add(panel1,BorderLayout.EAST);setBackground(Color.cyan);setPreferredSize(new Dimension(500,500);private class AeiouListener implemen
9、ts ActionListenerpublic void actionPerformed(ActionEvent e)if(e.getSource()=sure)repaint();if(e.getSource()=exit)System.exit(0);public void paintComponent(Graphics page)super.paintComponent(page);String a=text.getText();int j=0,l=0,m=0,n=0,k=0,p=0;for(int i=0;ia.length();i+)switch(a.charAt(i)case a:
10、j+;break;case e:l+;break;case i:m+;break;case o:n+;break;case u:p+;break;default:k+;label1.setText(“a:“+j);label2.setText(“e:“+l);label3.setText(“i:“+m);label4.setText(“o:“+n);label5.setText(“u:“+p);label6.setText(“其它字符:“+k);int x=50,y=50,x1=75;page.setColor(Color.black);page.drawLine(x, 400, 450, 4
11、00);page.setColor(Color.black);page.drawLine(x, y, x, 400);page.setColor(Color.red);page.fillRect(x1, 400-10*j,WITH, 10*j);page.setColor(Color.blue);page.fillRect(x1+N, 400-10*l,WITH, 10*l);page.setColor(Color.green);page.fillRect(x1+2*N, 400-10*m,WITH, 10*m);page.setColor(Color.yellow);page.fillRec
12、t(x1+3*N, 400-10*n,WITH, 10*n);page.setColor(Color.gray);page.fillRect(x1+4*N, 400-10*p,WITH, 10*p);page.setColor(Color.pink);page.fillRect(x1+5*N, 400-10*k,WITH, 10*k);运行界面截图:输入前界面:输入后界面:Pp5.22 画 100 个随机圆。package DrawCircle;import java.awt.Dimension;import java.awt.Toolkit;import javax.swing.JFrame
13、;public class DrawCircle public static void main(String args) JFrame frame=new JFrame(“Circles“);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(350,375);Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();int x=(screenSize.width-frame.getWidth()/2;int y=(screenSize.h
14、eight-frame.getHeight()/2;frame.setLocation(x, y);CirclePanel panel=new CirclePanel();frame.getContentPane().add(panel);frame.pack();frame.setVisible(true);package DrawCircle;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.A
15、ctionEvent;import java.awt.event.ActionListener;import java.util.Random;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JPanel;import DrawCircle1.CirclePanel1.CircleListner;public class CirclePanel extends JPanelprivate static final long serialVersio
16、nUID = 1L;private final int NUMCIRCLES=100,MAXRADIOUS=100;private final int MAX_X=250,MAX_Y=250;private Random generator;private JButton again,exit;private JPanel panel;public CirclePanel()generator=new Random();again=new JButton(“again“);exit=new JButton(“exit“);panel=new JPanel();panel.setLayout(n
17、ew BoxLayout(panel, BoxLayout.X_AXIS);panel.add(again);panel.add(Box.createHorizontalGlue();panel.add(exit);panel.setBackground(Color.green);setLayout(new BorderLayout();add(panel,BorderLayout.SOUTH);CircleListner listener=new CircleListner();again.addActionListener(listener);exit.addActionListener(
18、listener);setBackground(Color.green);setPreferredSize(new Dimension(350,375);public class CircleListner implements ActionListenerpublic void actionPerformed(ActionEvent event)if(event.getSource()=again)repaint();if(event.getSource()=exit)System.exit(0);public void paintComponent(Graphics page)super.
19、paintComponent(page);int x,y,radious;for(int count=0;countNUMCIRCLES;count+)x=generator.nextInt(MAX_X)+1;y=generator.nextInt(MAX_Y)+1;radious=generator.nextInt(MAXRADIOUS)+1;if(radious=35)page.setColor(Color.blue);page.drawOval(x, y, radious, radious);elseif(radious=70)page.setColor(Color.yellow);pa
20、ge.drawOval(x, y, radious, radious);elsepage.setColor(Color.red);page.drawOval(x, y, radious, radious);运行界面截图Pp5.29 画点。package DrawDots;import java.awt.Dimension;import java.awt.Toolkit;import javax.swing.JFrame;public class DrawDots public static void main(String args) JFrame frame=new JFrame();fra
21、me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(500,500);Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();int x=(screenSize.width-frame.getWidth()/2;int y=(screenSize.height-frame.getHeight()/2;frame.setLocation(x, y);DotPanel panel=new DotPanel();frame.getContentPane
22、().add(panel);frame.pack();frame.setVisible(true);package DrawDots;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Random;import javax.swing.Box;import javax.
23、swing.BoxLayout;import javax.swing.JButton;import javax.swing.JPanel;import DrawCircle1.CirclePanel1.CircleListner;public class DotPanel extends JPanel/* */private static final long serialVersionUID = 1L;private final int MAXDOTS=20000,BOUNDARY=250,MAXSIDE=500,WIDTH=1;private Random generator;privat
24、e JButton again,exit;private JPanel panel;public DotPanel()generator=new Random();again=new JButton(“again“);exit=new JButton(“exit“);panel=new JPanel();panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS);panel.add(again);panel.add(Box.createHorizontalGlue();panel.add(exit);panel.setBackground(Co
25、lor.cyan);setLayout(new BorderLayout();add(panel,BorderLayout.SOUTH);DotListner listener=new DotListner();again.addActionListener(listener);exit.addActionListener(listener);setBackground(Color.cyan);setPreferredSize(new Dimension(500,500);public class DotListner implements ActionListenerpublic void
26、actionPerformed(ActionEvent event)if(event.getSource()=again)repaint();if(event.getSource()=exit)System.exit(0);public void paintComponent(Graphics page)super.paintComponent(page);int x,y;for(int i=0;iMAXDOTS;i+)x=generator.nextInt(MAXSIDE)+1;y=generator.nextInt(MAXSIDE)+1;if(x=BOUNDARY)page.setColo
27、r(Color.red);page.fillRect(x, y, WIDTH, WIDTH);elsepage.setColor(Color.green);page.fillRect(x, y, WIDTH, WIDTH);运行界面截图:Pp5.30 画 10 个随机圆。package DrawCircle1;import java.awt.Dimension;import java.awt.Toolkit;import javax.swing.JFrame;public class DrawCircle1 public static void main(String args) JFrame
28、 frame=new JFrame(“Circles“);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(350,450);Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();int x=(screenSize.width-frame.getWidth()/2;int y=(screenSize.height-frame.getHeight()/2;frame.setLocation(x, y);CirclePanel1 panel
29、=new CirclePanel1();frame.getContentPane().add(panel);frame.pack();frame.setVisible(true);package DrawCircle1;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.util.Random;import java.awt.event.ActionEvent;import java.awt.event.ActionLi
30、stener;import javax.swing.Box;import javax.swing.BoxLayout;import javax.swing.JButton;import javax.swing.JPanel;public class CirclePanel1 extends JPanelprivate final int NUMCIRCLES=10,MAXRADIOUS=100;private final int MAX_X=250,MAX_Y=250;private Random generator;private JButton again,exit;private JPa
31、nel panel;public CirclePanel1()generator=new Random();again=new JButton(“again“);exit=new JButton(“exit“);panel=new JPanel();panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS);panel.add(again);panel.add(Box.createHorizontalGlue();panel.add(exit);panel.setBackground(Color.cyan);setLayout(new Bord
32、erLayout();add(panel,BorderLayout.SOUTH);CircleListner listener=new CircleListner();again.addActionListener(listener);exit.addActionListener(listener);setBackground(Color.cyan);setPreferredSize(new Dimension(350,400);public class CircleListner implements ActionListenerpublic void actionPerformed(Act
33、ionEvent event)if(event.getSource()=again)repaint();if(event.getSource()=exit)System.exit(0);public void paintComponent(Graphics page)super.paintComponent(page);int x,y;int radious=new int10;for(int i=0;iNUMCIRCLES;i+)radiousi=generator.nextInt(MAXRADIOUS)+1;int MAX;for(int i=0;iNUMCIRCLES;i+)MAX=ra
34、dious0;for(i=1;iNUMCIRCLES;i+)if(MAXradiousi)MAX=radiousi;for( i=0;iNUMCIRCLES;i+)x=generator.nextInt(MAX_X)+1;y=generator.nextInt(MAX_Y)+1;if(radiousi=MAX)page.setColor(Color.red);page.fillOval(x, y, MAX, MAX);elsepage.setColor(Color.blue);page.drawOval(x, y, radiousi, radiousi);运行界面截图:Pp5.35 字体大小。
35、package StyleOptions;import java.awt.Dimension;import java.awt.Toolkit;import javax.swing.JFrame;public class StyleOptions public static void main(String args) JFrame frame =new JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(400, 400);Dimension screenSize=Toolkit.getDefa
36、ultToolkit().getScreenSize();int x=(screenSize.width-frame.getWidth()/2;int y=(screenSize.height-frame.getHeight()/2;frame.setLocation(x, y);StyleOptionsPanel panel=new StyleOptionsPanel();frame.getContentPane().add(panel);frame.pack();frame.setVisible(true);package StyleOptions;import java.awt.*;im
37、port javax.swing.*;import javax.swing.border.EmptyBorder;import java.awt.event.*;public class StyleOptionsPanel extends JPanel/* */private static final long serialVersionUID = 1L;private JLabel saying,writing;private JCheckBox bold,italic;private JButton sure,exit;private JTextField text;private JPa
38、nel panel,panel1;private int size = 36;public StyleOptionsPanel()saying=new JLabel(“Say it with style!“);saying.setFont (new Font(“Helvetica“,Font.PLAIN,size);bold=new JCheckBox(“Bold“);bold.setBackground(Color.cyan);italic=new JCheckBox(“Italic“);italic.setBackground(Color.cyan);writing=new JLabel(
39、“Size“);text=new JTextField(2);sure=new JButton(“确定“);exit=new JButton(“退出“);panel=new JPanel();panel1=new JPanel();StyleListener listener =new StyleListener();bold.addActionListener(listener);italic.addActionListener(listener);sure.addActionListener(listener);exit.addActionListener(listener);setLay
40、out(new BoxLayout(this,BoxLayout.X_AXIS);panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS);panel1.setLayout(new FlowLayout(FlowLayout.CENTER);panel1.add(text);panel1.add(writing);panel1.setBackground(Color.cyan);panel.add(bold);panel.add(Box.createVerticalGlue();panel.add(italic);panel.add(Box.c
41、reateVerticalGlue();panel.add(panel1);panel.add(sure);panel.add(Box.createVerticalGlue();panel.add(exit);panel.setBackground(Color.cyan);add(Box.createRigidArea(new Dimension(50,0);add(saying);add(Box.createHorizontalGlue();add(panel);setBackground(Color.cyan);setPreferredSize(new Dimension(400,200)
42、;private class StyleListener implements ActionListenerpublic void actionPerformed(ActionEvent event) int style=Font.PLAIN;String a=text.getText();if(bold.isSelected()style=Font.BOLD;if(italic.isSelected()style+=Font.ITALIC;if(event.getSource()=sure)size=Integer.parseInt(a);saying.setFont(new Font(“H
43、elvetica“,style,size);if(event.getSource()=exit)System.exit(0);运行界面截图:小风扇package DrawArcs;import javax.swing.*;import java.awt.*;public class DrawArcs extends JFrameprivate static final long serialVersionUID = 1L;public DrawArcs()setTitle(“DrawArcs“);getContentPane().add(new ArcsPanel();public stati
44、c void main(String args)DrawArcs frame = new DrawArcs();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(550, 500);Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();int x=(screenSize.width-frame.getWidth()/2;int y=(screenSize.height-frame.getHeight()/2;frame.setLocat
45、ion(x,y);frame.setVisible(true);package DrawArcs;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Graphics;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.Box;import javax.swing.BoxLayout
46、;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JSlider;import javax.swing.Timer;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;public class ArcsPanel extends JPanelprivate static final long serialVersionUID = 1L;privat
47、e Timer timer1,timer2;private JButton start,end,up,down,converse,exit;private JSlider slid;private JLabel label;private JPanel panel1,panel2;static int time=30;static int z=0;public ArcsPanel()timer1=new Timer(time,new ArcsListener();timer2=new Timer(time,new ArcsListener();start=new JButton(“开始“);e
48、nd=new JButton(“停止“);up=new JButton(“加速“);down=new JButton(“减速“);converse=new JButton(“反转“);exit=new JButton(“退出 “);label=new JLabel(“speed:30“);slid=new JSlider(JSlider.VERTICAL,0,120,0);panel1=new JPanel();panel2=new JPanel();setLayout(new BorderLayout();slid.setMajorTickSpacing(30);slid.setMinorT
49、ickSpacing(5);slid.setPaintTicks(true);slid.setPaintLabels(true);slid.setValue(30);slid.setBackground(Color.cyan);panel1.setLayout(new FlowLayout();panel1.add(start);panel1.add(end);panel1.setBackground(Color.cyan);panel2.setLayout(new BoxLayout(panel2,BoxLayout.Y_AXIS);panel2.add(label);panel2.add(Box.createRigidArea(new Dimension(0,120);panel2.add(up)