精品文档精品文档Java实验三实验报告利用Applet画图:importjava.applet.Applet;importjava.awt.Graphics;importjava.awt.Polygon;publicclassApplet2extendsApplet{publicvoidpaint(Graphicsg){intxCenter=60;intyCenter=100;intradius=30;Polygonpolygon=newPolygon();polygon.addPoint(xCenter+radius,yCenter);polygon.addPoint((int)(xCenter+radius*Math.cos(2*Math.PI/6)),(int)(yCenter+radius*Math.sin(2*Math.PI/6)));polygon.addPoint((int)(xCenter+radius*Math.cos(2*2*Math.PI/6)),(int)(yCenter+radius*Math.sin(2*2*Math.PI/6)));polygon.addPoint((int)(xCenter+radius*Math.cos(3*2*Math.PI/6)),(int)(yCenter+radius*Math.sin(3*2*Math.PI/6)));polygon.addPoint((int)(xCenter+radius*Math.cos(4*2*Math.PI/6)),(int)(yCenter+radius*Math.sin(4*2*Math.PI/6)));polygon.addPoint((int)(xCenter+radius*Math.cos(5*2*Math.PI/6)),(int)(yCenter+radius*Math.sin(5*2*Math.PI/6)));g.drawPolygon(polygon);//画六边形g.drawLine(5,5,50,50);//画直线g.drawRect(60,5,40,50);//画矩形精品文档精品文档g.drawOval(120,5,50,50);//画圆形g.drawOval(120,65,60,40);//画椭圆}}GUI:1、编程包含一个标签和一个按钮,单击按钮时,标签的内容在“你好”和“再见”之间切换。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassSwitchextendsJFrameimplementsActionListener{JLabell;JButtonb;publicSwitch(){l=newJLabel(你好);b=newJButton(切换);b.addActionListener(this);setLayout(newFlowLayout());add(l);add(b);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(600,200);setVisible(true);}publicvoidactionPerformed(ActionEvente){if(l.getText().equals(你好))精品文档精品文档l.setText(再见);elsel.setText(你好);}publicstaticvoidmain(Stringargs[]){newSwitch();}}2、编程包含一个文本框和一个文本区域,文本框内容改变时,将文本框中的内容显示在文本区域中;在文本框中按回车键时,清空文本区域的内容。importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JTextArea;importjavax.swing.JTextField;importjavax.swing.WindowConstants;importjavax.swing.event.DocumentEvent;importjavax.swing.event.DocumentListener;importjavax.swing.text.BadLocationException;importjavax.swing.text.Document;importjavax.swing.text.PlainDocument;publicclasstext2extendsjavax.swing.JFrame{privateJTextFieldinput;privateJTextAreaoutput;publicstaticvoidmain(String[]args){text2inst=newtext2();inst.setLocationRelativeTo(null);精品文档精品文档inst.setVisible(true);}publictext2(){super();initGUI();}privatevoidinitGUI(){setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);{input=newJTextField();getContentPane().add(input,BorderLayout.NORTH);input.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){//这句话就是让文本域中显示文本框内容output.setText();}});PlainDocumentdoc=(PlainDocument)input.getDocument();doc.addDocumentListener(newDocumentListener(){publicvoidchangedUpdate(DocumentEvente){}精品文档精品文档publicvoidinsertUpdate(DocumentEvente){try{//这里进行同步处理Documentdoc=e.getDocument();intoffset=e.getOffset();Stringtext=doc.getText(offset,e.getLength());Documentadoc=output.getDocument();adoc.insertString(adoc.getLength(),text,null);}catch(BadLocationExceptione1){e1.printStackTrace();}}publicvoidremoveUpdate(DocumentEvente){}});}{output=newJTextArea();getContentPane().add(output,BorderLayout.CENTER);output.setLineWrap(true);}pack();setSize(400,300);}}3、编程包含一个复选按钮和一个普通按钮,复选按钮选中时,普通按钮的背景色为青色,未选中时为灰色。精品文档精品文档importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassbcolorextendsJFrameimplementsActionListener{JCheckBoxch;JButtonb1;publicbcolor(){setSize(400,300);setLayout(newFlowLayout());b1=newJButton(测试);ch=newJCheckBox(改变颜色);add(b1);add(ch);ch.addActionListener(this);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);}publicvoidactionPerformed(ActionEvente){if(e.getSource().equals(ch)){if(ch.isSelected()){b1.setBackground(Color.green);}else{b1.setBackground(Color.gray);}精品文档精品文档}}publicstaticvoidmain(Stringargs[]){newbcolor();}}4、编程包含一个单选按钮组和一个普通按钮,单选按钮组中包含三个单选,文本说明分别为“普通”、“黑体”和“斜体”。选择文本标签为“普通”的单选按钮时,普通按钮中的文字为普通字体,选择文本标签为“黑体”的按钮时,普通按钮中的文字的字体为黑体,选择文本标签为“斜体”的单选按钮时,普通按钮中的文字的字体为斜体。importjava.awt.Font;importjava.awt.GridLayout;importjavax.swing.ButtonGroup;importjavax.swing.JFrame;importjavax.swing.JRadioButton;importjavax.swing.JButton;importjava.awt.event.*;classtest4extendsJFrame{JRadioButtona1,a2,a3;JButtonqd;test4(){setLayout(newGridLayout(5,1));a1=newJRadioButton(楷体,true);a2=newJRadioButton(黑体,true);a3=newJRadioButton(斜体,true);精品文档精品文档qd=newJButton(改变字体);add(a1);add(a2);add(a3);add(qd);ButtonGroupbg=newButtonGroup();bg.add(a1);bg.add(a2);bg.add(a3);bg.add(qd);setVisible(true);a1.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(a1.isSelected()){qd.setFont(newFont(楷体,Font.PLAIN,Integer.parseInt(16)));}}});a2.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(a2.isSelected()){qd.setFont(newFont(黑体,Font.PLAIN,Integer.parseInt(16)));}}});a3.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(a3.isSelected()){精品文档精品文档qd.setFont(newFont(斜体,Font.PLAIN,Integer.parseInt(16)));}}});}publicstaticvoidmain(String[]args){test4q=newtest4();q.setTitle(单选);q.setSize(300,400);q.setLocation(300,300);q.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);q.setV