二级Java程序题--03综合应用:源代码(01-42)
目录
3.综合应用
3.1
3.2
3.3
3.4
3.5
3.6(*)
3.7
3.8
3.9
3.10
3.11
3.12
3.13
3.14
3.15
3.16
3.17
3.18
3.19
3.20
3.21
3.22
3.23
3.24
3.25
3.26
3.27
3.28
3.29
3.30
3.31
3.32
3.33
3.34
3.35
3.36
3.37
3.38
3.39
3.40
3.41
3.42
3.综合应用
3.1
import java.text.DecimalFormat; import javax.swing.*; import java.awt.*; import java.awt.event.*; //*********Found******** public class Java_3 extends JFrame implements ActionListener { private JTextField input1, input2, output; private int number1, number2; private double result; // 初始化 public Java_3() { //*********Found******** super( "示范异常" ); Container c = getContentPane(); c.setLayout( new GridLayout( 3, 2 ) ); c.add( new JLabel( "输入分子", SwingConstants.RIGHT ) ); input1 = new JTextField( 10 ); c.add( input1 ); c.add( new JLabel( "输入分母和回车", SwingConstants.RIGHT ) ); input2 = new JTextField( 10 ); c.add( input2 ); input2.addActionListener( this ); c.add( new JLabel( "计算结果", SwingConstants.RIGHT ) ); output = new JTextField(); c.add( output ); setSize( 425, 100 ); show(); } //处理 GUI 事件 public void actionPerformed( ActionEvent e ) { DecimalFormat precision3 = new DecimalFormat( "0.000" ); output.setText( "" ); // 空的JTextField输出 //*********Found******** try{ number1 = Integer.parseInt( input1.getText() ); number2 = Integer.parseInt( input2.getText() ); result = quotient( number1, number2 ); //*********Found******** output.setText(precision3.format(result)); } catch ( NumberFormatException nfe ) { JOptionPane.showMessageDialog( this, "你必须输入两个整数", "非法数字格式", JOptionPane.ERROR_MESSAGE ); } catch ( Exception dbze ) { //*********Found******** JOptionPane.showMessageDialog( this, "除法异常", "除数为零", JOptionPane.ERROR_MESSAGE ); } } // 定义求商的方法,如遇除数为零时,能抛出异常。 public double quotient( int numerator, int denominator ) throws Exception { if ( denominator == 0 ) throw new Exception(); return ( double ) numerator / denominator; } public static void main( String args[] ) { Java_3 app = new Java_3(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { e.getWindow().dispose(); System.exit( 0 ); } } ); } } /* JOptionPane类的常用静态方法如下: showInputDialog() showConfirmDialog() showMessageDialog() showOptionDialog() */
3.2
import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class Java_3 { public static void main(String[] args) { FontFrame frame = new FontFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } //*********Found******** class FontFrame extends JFrame { public FontFrame() { setTitle("沁园春.雪"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); FontPanel panel = new FontPanel(); Container contentPane = getContentPane(); //*********Found******** contentPane.add(panel); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; } //*********Found******** class FontPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; String message = "数风流人物,还看今朝!"; Font f = new Font("隶书", Font.BOLD, 24); g2.setFont(f); FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; double ascent = -bounds.getY(); double baseY = y + ascent; g2.setPaint(Color.RED); //*********Found******** g2.drawString(message, (int)x, (int)(baseY)); } }
3.3
public class Java_3{ public static void main(String[] args) { String text = "Beijing, the Capital City, is the political," + "cultural and diplomatic centre of China. It has" + "become a modern international cosmopolitan city" + "with more than 11 million people. The Capital" + "International Airport, 23.5 km from the city centre," + "is China's largest and most advanced airport."; int vowels = 0 ; //*********Found********* int textLength= text.length(); for(int i = 0; i < textLength; i++) { //*********Found********* char ch = Character.toLowerCase(text.charAt(i)); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { //*********Found********* vowels++; } } System.out.println("The text contained vowels: " + vowels + "\n" ); } }
3.4
import java.awt.*; import javax.swing.*; public class Java_3{ public static void main(String[] args){ BeijingFrame frame = new BeijingFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } //*********Found********* class BeijingFrame extends JFrame{ public BeijingFrame(){ setTitle("Welcome to Beijing"); Container contentPane = getContentPane(); BeijingPanel panel = new BeijingPanel(); contentPane.add(panel); pack(); } } class BeijingPanel extends JPanel{ public BeijingPanel(){ //*********Found********* setLayout(new BorderLayout() ); ImageIcon icon = new ImageIcon("tiantan.jpg"); //*********Found********* jLC = new JLabel( icon ); add(jLC, BorderLayout.CENTER); lpanel = new JPanel(); jLS = new JLabel("The Temple of Heaven"); lpanel.add(jLS); add(lpanel, BorderLayout.SOUTH); } private JLabel jLC; private JLabel jLS; private JPanel panel; private JPanel lpanel; }
3.5
//*********Found********** import javax.swing.JFrame; import java.awt.*; public class Java_3{ static final int WIDTH=300; static final int HEIGHT=200; public static void main(String[] args){ //*********Found********** JFrame jf=new JFrame(); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found********** jf.setTitle("股票分析系统"); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screenSize=kit.getScreenSize(); int width=screenSize.width; int height=screenSize.height; int x=(width-WIDTH)/2; int y=(height-HEIGHT)/2; jf.setLocation (x,y); //*********Found********** jf.setVisible(true); } }
3.6(*)
import java.io.*; public class Java_3{ public static int data[]={32,18,41,23,2,56,36,67,59,20}; public static void main(String args[]){ int i; //*********Found********** int index=data.length; System.out.println("排序前:"); for(i=0;i<index;i++) System.out.print(" "+data[i]+" "); System.out.println(); //*********Found********** BubbleSort( index ); System.out.println("排序后:"); for(i=0;i<index;i++) System.out.print(" "+data[i]+" "); System.out.println(); } // 冒泡法排序 public static void BubbleSort(int index){ int i,j; int temp; for(j=1;j<index;j++){ for(i=index-1;i>=j;i--){ if(data[i]<data[i-1]){ //比较相邻的两个数 temp=data[i]; data[i]=data[i-1]; //*********Found********** data[i-1]= temp ; } } } } }
3.7
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Java_3{ public static void main(String[] args){ MulticastFrame frame = new MulticastFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class MulticastFrame extends JFrame{ public MulticastFrame(){ setTitle("MulticastTest"); setSize(WIDTH, HEIGHT); MulticastPanel panel = new MulticastPanel(); Container contentPane = getContentPane(); //*********Found********** contentPane.add(panel); } public static final int WIDTH = 300; public static final int HEIGHT = 200; } class MulticastPanel extends JPanel{ public MulticastPanel(){ JButton newButton = new JButton("New"); add(newButton); ActionListener newListener = new ActionListener(){ public void actionPerformed(ActionEvent event){ makeNewFrame(); } }; newButton.addActionListener(newListener); closeAllButton = new JButton("Close all"); add(closeAllButton); } private void makeNewFrame(){ final BlankFrame frame = new BlankFrame(); frame.show(); ActionListener closeAllListener = new ActionListener(){ public void actionPerformed(ActionEvent event){ //*********Found********** frame.dispose(); //使窗口隐藏或消除 } }; //*********Found********** closeAllButton.addActionListener(closeAllListener); } private JButton closeAllButton; } class BlankFrame extends JFrame{ public BlankFrame(){ //*********Found********** counter++ ; setTitle("Frame " + counter); setSize(WIDTH, HEIGHT); setLocation(SPACING * counter, SPACING * counter); } public static final int WIDTH = 200; public static final int HEIGHT = 150; public static final int SPACING = 30; private static int counter = 0; }
3.8
import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; //*********Found********** public class Java_3 implements ActionListener{ JFrame f; JTextArea ta; JFileChooser fc; Container c; File myFile; public static void main(String args[]){ Java_3 demo=new Java_3(); demo.go(); } void go(){ JFrame f=new JFrame("File Chooser Demo"); JButton b=new JButton("Open file"); ta=new JTextArea("Where is your file path?",10,30); //*********Found********** b.addActionListener(this); c=f.getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add("South",b); f.getContentPane().add("Center",ta); f.setSize(300,300); f.setVisible(true); } public void actionPerformed(ActionEvent e){ fc=new JFileChooser(); //*********Found********** int selected=fc.showOpenDialog(c); if (selected==JFileChooser.APPROVE_OPTION){ myFile=fc.getSelectedFile(); //*********Found********** ta.setText("You have selected file: "+myFile.getName()); } } }
3.9
import javax.swing.*; import java.awt.*; import java.awt.event.*; //*********Found********** public class Java_3 extends WindowAdapter implements ActionListener { private JFrame frame; private JTextField name; private JPasswordField pass; private JLabel nameLabel; private JLabel passLabel; private JPanel textPanel; private JPanel labelPanel; private JButton button; private JTextArea textArea; public void initGUI() { frame=new JFrame("Frame with Dialog"); frame.addWindowListener(this); button=new JButton("JDialog"); //*********Found********** button.addActionListener(this); textArea=new JTextArea("",3,10); frame.getContentPane().add(textArea,BorderLayout.CENTER); frame.getContentPane().add(button,BorderLayout.NORTH); frame.setSize(400,300); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { final JDialog dia=new JDialog(frame,"login information"); JButton ok=new JButton("ok"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.setText(""); textArea.append("name:"+name.getText()+"\r\n"); textArea.append("passWord:"+new String(pass.getPassword())+"\r\n"); //*********Found********** dia.setVisible(false); //隐藏对话框 } }); name=new JTextField("",10); pass=new JPasswordField("",10); pass.setEchoChar('*'); textPanel=new JPanel(); textPanel.setLayout(new GridLayout(2,1,10,10)); textPanel.add(name); textPanel.add(pass); nameLabel=new JLabel("name"); passLabel=new JLabel("passWord"); labelPanel=new JPanel(); labelPanel.setLayout(new GridLayout(2,1,20,20)); labelPanel.add(nameLabel); labelPanel.add(passLabel); dia.getContentPane().add(labelPanel,BorderLayout.WEST); dia.getContentPane().add(textPanel,BorderLayout.CENTER); dia.getContentPane().add(ok,BorderLayout.SOUTH); dia.setSize(200,130); dia.setVisible(true); } public void windowClosing(WindowEvent event) { frame.setVisible(false); System.exit(0); } public static void main(String args[]) { Java_3 example=new Java_3(); //*********Found********** example.initGUI(); } }
3.10
import java.awt.*; import java.awt.event.*; //*********Found********** import javax.swing.*; public class Java_3 { public static void main(String[ ] args) { JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found********** frame.getContentPane().add(new Change()); frame.pack(); frame.setVisible(true); } } class Change extends JPanel { int count = 200; JLabel l1; JButton b1, b2; public Change( ) { setPreferredSize(new Dimension(280, 60)); l1 = new JLabel("200"); b1 = new JButton("增大"); b2 = new JButton("减小"); add(l1); //*********Found********** add(b1); //*********Found********** add(b2); b1.addActionListener( new BListener( ) ); b2.addActionListener( new BListener( ) ); } //*********Found********** private class BListener implements ActionListener { //*********Found********** public void actionPerformed(ActionEvent e) { if (e.getSource( ) == b1) { count++; } else { count--; } l1.setText("" + count); } } }
3.11
import java.awt.*; import java.awt.event.*; //*********Found********** import javax.swing.*; //*********Found********** public class Java_3 extends JPanel{ private int counter = 0; private JButton closeAllButton; public Java_3() { JButton newButton = new JButton("New"); //*********Found********** add(newButton); newButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ CloseFrame f = new CloseFrame(); counter++; f.setTitle("窗体 " + counter); f.setSize(200, 150); f.setLocation(30 * counter, 30 * counter); //*********Found********** f.setVisible(true); closeAllButton.addActionListener(f); } }); closeAllButton = new JButton("Close all"); add(closeAllButton); } public static void main(String[ ] args) { JFrame frame = new JFrame(); frame.setTitle("多窗体测试"); frame.setSize(300, 200); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = frame.getContentPane(); contentPane.add(new Java_3()); frame.setVisible(true) ; } } //*********Found********** class CloseFrame extends JFrame implements ActionListener { public void actionPerformed(ActionEvent evt) { setVisible(false); } }
3.12
import java.io.*; import java.awt.event.* ; import javax.swing.*; public class Java_3 implements ActionListener{ public static void main(String args[]){ Java_3 t = new Java_3(); //*********Found********** JFrame f = new JFrame("Test"); JButton b = new JButton("复制文件"); b.setSize(100,40); b.addActionListener(t); f.setSize(400,400); //*********Found********** f.getContentPane().add(b); f.pack(); f.setVisible(true) ; f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent event){ try{ //*********Found********** FileInputStream in=new FileInputStream("a.txt"); //*********Found********** FileOutputStream out=new FileOutputStream("b.txt"); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); } catch( Exception e){ } } }
3.13
//*********Found********** import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; //*********Found********** public class Java_3 implements ActionListener { JFrame f; JPanel p; JColorChooser cc; Container c; Color myColor; JMenuBar mb; JMenu m1; JMenuItem mi1; public static void main(String args[]) { Java_3 demo = new Java_3(); demo.go(); } void go() { JFrame f = new JFrame("File Chooser Demo"); mb = new JMenuBar(); f.setJMenuBar(mb); m1 = new JMenu("Edit"); mb.add(m1); mi1 = new JMenuItem("Choose Color"); m1.add(mi1); mi1.addActionListener(this); //*********Found********** c =f .getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p = new JPanel(); myColor = Color.red; p.setBackground(myColor); c.add("Center", p); f.setSize(300, 300); //*********Found********** f.setVisible(true); } public void actionPerformed(ActionEvent e) { //*********Found********** cc = new JColorChooser(); myColor = cc.showDialog(c, "Choose window background color", Color.white); p.setBackground(myColor); } }
3.14
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Java_3 { public static void main(String[ ] args) { JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found********** frame.getContentPane().add(new Change()); frame.pack(); //*********Found********** frame.setVisible(true); } } class Change extends JPanel{ int count = 200; JLabel l1; JButton b1, b2; public Change() { setPreferredSize(new Dimension(280, 60)); l1 = new JLabel("200"); b1 = new JButton("增大"); b2 = new JButton("减小"); add(l1); add(b1); add(b2); b1.addActionListener(new BListener1()); //*********Found********** b2.addActionListener(new BListener2()); } private class BListener1 implements ActionListener { public void actionPerformed(ActionEvent e) { count++; l1.setText("" + count); } } private class BListener2 implements ActionListener { public void actionPerformed(ActionEvent e) { //*********Found********** count --; l1.setText("" + count); } } }
3.15
import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; //*********Found********** public class Java_3 implements ActionListener{ JFrame f; JPanel p; JColorChooser cc; Container c; Color myColor; JMenuBar mb; JMenu m1; JMenuItem mi1; public static void main(String args[]){ Java_3 demo=new Java_3(); demo.go(); } void go(){ JFrame f=new JFrame("File Chooser Demo"); mb=new JMenuBar(); f.setJMenuBar(mb); //*********Found********** m1=new JMenu("Edit"); mb.add(m1); mi1=new JMenuItem("Choose Color"); m1.add(mi1); //*********Found********** mi1.addActionListener(this); c=f.getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p=new JPanel(); myColor=Color.red; p.setBackground(myColor); c.add("Center",p); f.setSize(300,300); f.setVisible(true); } public void actionPerformed(ActionEvent e){ cc=new JColorChooser(); //*********Found********** myColor=cc.showDialog(c,"Choose window background color",Color.white); p.setBackground(myColor); } }
3.16
import javax.swing.*; //*********Found******** import java.awt.event.*; import java.io.*; import java.awt.*; //*********Found******** public class Java_3 implements ActionListener{ JFrame f; JTextArea ta; //*********Found******** JFileChooser fc; Container c; File myFile; public static void main(String args[]){ Java_3 demo = new Java_3(); demo.go(); } void go(){ f = new JFrame("File Chooser Demo"); //*********Found******** JButton b = new JButton("Open file"); ta = new JTextArea("Where is your file path?",10,30); //*********Found******** b.addActionListener(this); c = f.getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found******** f.getContentPane().add("South", b); f.getContentPane().add("Center",ta); f.setSize(300,300); f.setVisible(true); } public void actionPerformed(ActionEvent e){ fc = new JFileChooser(); int selected = fc.showOpenDialog(c); if (selected==JFileChooser.APPROVE_OPTION){ myFile = fc.getSelectedFile(); ta.setText("You have selected file: "+ myFile.getName()); } } }
3.17
//*********Found******** public class Java_3 extends Thread{ static RegistrationAgent agent; static boolean timetoquit=false; public static void main(String[] args){ agent = new RegistrationAgent(); Thread[] t= new Thread[3]; for (int i=0; i<3; i++){ t[i] = new Java_3(); //*********Found******** t[i].start(); } } public void run( ){ //*********Found******** while (!timetoquit){ boolean r = agent.reg(); if (!r) timetoquit = true; try{ Thread.sleep(2); }catch(Exception e){} } } } class RegistrationAgent { private int quota = 0; public boolean reg(){ synchronized(this){ if( quota < 10){ //*********Found******** quota++; System.out.print(Thread.currentThread().getName()); System.out.println( " Registered one student, and total " + quota +" students registered."); return true; } else //*********Found******** return false; } } }
3.18
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.imageio.*; import javax.swing.*; public class Java_3 extends JFrame { private JLabel label; private JFileChooser fileChooser; private ImagePanel panel; public Java_3() { setTitle("图片浏览器"); setSize(500, 400); fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new File("."));//设置默认路径为当前目录 JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("文件"); menuBar.add(menu); JMenuItem openItem = new JMenuItem("打开图片"); menu.add(openItem); panel = new ImagePanel(); add(panel); //*********Found******** openItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ int result = fileChooser.showOpenDialog(null); if(result==JFileChooser.APPROVE_OPTION){ String name = fileChooser.getSelectedFile().getPath(); //*********Found******** panel.setImage(name); panel.repaint(); } } }); JMenuItem exitItem = new JMenuItem("退出图片"); menu.add(exitItem); exitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ System.exit(0); } }); } public static void main(String[] args) { //*********Found******** Java_3 frame = new Java_3 (); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found******** frame.setVisible(true); } } //*********Found******** class ImagePanel extends JPanel { private Image image; private int showWidth; private int showHeight; public void setImage(String fileName) { try { image = ImageIO.read(new File(fileName)); } catch (IOException e) { e.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); if (image == null) return; int imageWidth = image.getWidth(this); int imageHeight = image.getHeight(this); int width = getWidth(); int height = getHeight(); if(imageWidth>width){ this.showWidth = width; }else{ this.showWidth = imageWidth; } if(imageHeight>height){ this.showHeight = height; }else{ this.showHeight = imageHeight; } g.drawImage(image, 0, 0, showWidth, showHeight, null, null); } }
3.19
import java.io.*; import java.lang.Thread; class MyThread extends Thread{ public int x = 0; //*********Found******** public void run(){ System.out.println(++x); } } //*********Found******** class RThread implements Runnable{ private int x = 0; public void run(){ System.out.println(++x); } } public class Java_3 { public static void main(String[] args) throws Exception{ for(int i=0;i<5;i++){ Thread t = new MyThread(); //*********Found******** t.start(); } Thread.sleep(1000); System.out.println(); //*********Found******** RThread r = new RThread(); //*********Found******** for(int i=0;i<5;i++){ Thread t = new Thread(r); t.start(); } } }
3.20
//打印无符号整数位 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Java_3 extends JFrame { public Java_3(){ super( "打印无符号整数位" ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); c.add( new JLabel( "请输入整数: " ) ); final JTextField output = new JTextField( 33 ); JTextField input = new JTextField( 10 ); input.addActionListener( new ActionListener() { //*********Found******** public void actionPerformed( ActionEvent e ){ int val = Integer.parseInt( e.getActionCommand() ); //*********Found******** output.setText( getBits( val ) ); } } ); c.add( input ); c.add( new JLabel( "该数的二进制位表示是" ) ); output.setEditable( false ); //*********Found******** c.add( output ); setSize( 720, 70 ); setVisible(true); } private String getBits( int value ){ int displayMask = 1 << 31; StringBuffer buf = new StringBuffer( 35 ); for ( int c = 1; c <= 32; c++ ) { buf.append( ( value & displayMask ) == 0 ? '0' : '1' ); value <<= 1; if ( c % 8 == 0 ) buf.append( ' ' ); } return buf.toString(); } public static void main( String args[] ){ Java_3 app = new Java_3(); app.addWindowListener( new WindowAdapter() { //*********Found******** public void windowClosing( WindowEvent e ){ System.exit( 0 ); } } ); } }
3.21
import java.io.*; public class Java_3 { public static void main(String[] args) { Java_3 exceptionExample = new Java_3(); //*********Found********** try { FileInputStream fi = new FileInputStream("C:" + "\\" + "abc.txt"); } //*********Found********** catch(FileNotFoundException ex) { //*********Found********** System.out.println(ex.getMessage()+ "请确认文件路径及文件名是否正确!"); } } }
3.22
import java.awt.*; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet { //*********Found********** public void init() { Container contentPane = getContentPane(); JLabel label = new JLabel("One World One Dream",SwingConstants.CENTER); label.setFont(new Font("Arial", Font.BOLD, DEFAULT_SIZE)); //*********Found********** contentPane.add(label); } private static final int DEFAULT_SIZE = 24; }
3.23
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class Java_3 { public static void main(String[] args) { JFrame frame = new ImageViewerFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found********** frame.setVisible(true); } } class ImageViewerFrame extends JFrame { private JLabel label; private JLabel labelT; private JFileChooser chooser; private JComboBox faceCombo; private static final int DEFAULT_SIZE = 24; public static final int DEFAULT_WIDTH = 570; public static final int DEFAULT_HEIGHT = 400; public ImageViewerFrame() { setTitle("ImageViewer"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); label = new JLabel(); Container contentPane = getContentPane(); contentPane.add(label,BorderLayout.CENTER); chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); //*********Found********** JMenu menu = new JMenu("File"); menuBar.add(menu); JMenuItem openItem = new JMenuItem("Open"); //*********Found********** menu.add(openItem); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int r = chooser.showOpenDialog(null); if(r == JFileChooser.APPROVE_OPTION) { //*********Found********** String name = chooser.getSelectedFile().getPath(); label.setIcon(new ImageIcon(name)); } } }); labelT = new JLabel("红军不怕远征难"); labelT.setFont(new Font("隶书", Font.PLAIN, DEFAULT_SIZE)); contentPane.add(labelT, BorderLayout.NORTH ); faceCombo = new JComboBox(); faceCombo.setEditable(true); faceCombo.addItem("隶书"); faceCombo.addItem("华文新魏"); faceCombo.addItem("华文行楷"); faceCombo.addItem("华文隶书"); faceCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { //*********Found********** labelT.setFont(new Font((String)faceCombo.getSelectedItem(), Font.PLAIN, DEFAULT_SIZE)); } }); JPanel comboPanel = new JPanel(); comboPanel.add(faceCombo); contentPane.add(comboPanel, BorderLayout.SOUTH); } }
3.24
import java.awt.event.*; import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class Java_3 { public static void main(String[] args) { FontFrame frame = new FontFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } //*********Found********** class FontFrame extends JFrame { public FontFrame() { setTitle("北京 2008"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); FontPanel panel = new FontPanel(); Container contentPane = getContentPane(); //*********Found********** contentPane.add(panel); } public static final int DEFAULT_WIDTH = 400; public static final int DEFAULT_HEIGHT = 250; } class FontPanel extends JPanel { public FontPanel() { JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Green"); add(yellowButton); add(blueButton); add(redButton); ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction greenAction = new ColorAction(Color.GREEN); yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(greenAction); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; String message = "同一个世界,同一个梦想!"; Font f = new Font("隶书", Font.BOLD, 27); g2.setFont(f); FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; double ascent = -bounds.getY(); double baseY = y + ascent; g2.setPaint(Color.RED); g2.drawString (message, (int)x, (int)(baseY)); } //*********Found********** private class ColorAction implements ActionListener { public ColorAction(Color c) { BackgroundColor = c; } //*********Found********** public void actionPerformed(ActionEvent event) { setBackground(BackgroundColor); } private Color BackgroundColor; } }
3.25
import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; public class Java_3 implements ActionListener { private JFrame frame; private JButton button; private JButton saveButton; private JTextArea textArea; private JFileChooser dia; private JPanel buttonPanel; public void initGUI() { //*********Found********** frame=new JFrame("file chooser"); button=new JButton("open file"); button.setActionCommand("open"); //*********Found********** button.addActionListener(this); saveButton=new JButton("save file"); saveButton.setActionCommand("save"); //*********Found********** saveButton.addActionListener(this); textArea=new JTextArea("",10,10); buttonPanel=new JPanel(); dia=new JFileChooser(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); buttonPanel.add(button); buttonPanel.add(saveButton); frame.getContentPane().add(buttonPanel,BorderLayout.NORTH); frame.getContentPane().add(textArea,BorderLayout.CENTER); frame.setSize(300,300); //*********Found********** frame.setVisible(true); } //*********Found********** public void actionPerformed(ActionEvent event) { if(event.getActionCommand().equals("open")) { dia.showOpenDialog( frame ); dia.setVisible(true); File file=dia.getSelectedFile(); String fileName=file.getAbsolutePath(); textArea.append("path of selected file: "+fileName+"\r\n"); } else if(event.getActionCommand().equals("save")) { dia.showSaveDialog(frame); dia.setVisible(true); File file=dia.getSelectedFile(); String fileName=file.getAbsolutePath(); textArea.append("path of saved file: "+fileName+"\r\n"); } } public static void main(String args[]) { Java_3 example=new Java_3(); example.initGUI(); } }
3.26
import javax.swing.*; import java.awt.event.*; import java.awt.*; //*********Found********** public class Java_3 extends MouseAdapter implements ActionListener { //*********Found********** private JPopupMenu pop; private JMenu subPop; private JMenuItem color; private JMenuItem exit; private JMenuItem red; private JMenuItem blue; private JTextArea textArea; private JFrame frame; public void initGUI() { pop=new JPopupMenu(); subPop=new JMenu("color"); //*********Found********** red=new JMenuItem("red"); red.addActionListener(this); blue=new JMenuItem("blue"); blue.addActionListener(this); subPop.add(red); subPop.add(blue); exit=new JMenuItem("exit"); exit.addActionListener(this); pop.add(subPop); pop.add(exit); frame=new JFrame("popup frame"); textArea=new JTextArea("",10,10); textArea.addMouseListener(this); //*********Found********** frame.getContentPane().add(textArea); frame.setSize(300,300); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent event) { if(event.getSource()==red) { //*********Found********** textArea.setForeground(Color.red); textArea.setText("red menu is selected"); } else if(event.getSource()==blue) { textArea.setForeground(Color.blue); textArea.setText("blue menu is selected"); } else if(event.getSource()==exit) { frame.setVisible(false); System.exit(0); } } public void mousePressed(MouseEvent e) { if(e.getModifiers()==e.BUTTON3_MASK) { pop.show(e.getComponent(),e.getX(),e.getY()); } } public static void main(String args[]) { Java_3 example=new Java_3(); example.initGUI(); } }
3.27
import java.awt.Graphics; import javax.swing.JApplet; //*********Found********** public class Java_3 extends JApplet { //*********Found********** public void paint(Graphics g){ int counter = 1; do { //*********Found********** g.drawOval( 110 - counter * 10, 110 - counter * 10, counter * 20, counter * 20 ); //*********Found********** counter++; } while (counter<=10); } }
3.28
//*********Found********** import javax.swing.*; import java.awt.event.*; import java.awt.*; //*********Found********** public class Java_3 extends MouseAdapter implements ActionListener { //*********Found********** private JPopupMenu pop; private JMenu subPop; private JMenuItem color; private JMenuItem exit; private JMenuItem red; private JMenuItem blue; private JTextArea textArea; private JFrame frame; public void initGUI() { pop=new JPopupMenu(); subPop=new JMenu("color"); red=new JMenuItem("red"); red.addActionListener(this); blue=new JMenuItem("blue"); blue.addActionListener(this); subPop.add(red); subPop.add(blue); exit=new JMenuItem("exit"); exit.addActionListener(this); pop.add(subPop); pop.add(exit); //*********Found********** frame=new JFrame("popupframe"); textArea=new JTextArea("",10,10); textArea.addMouseListener(this); frame.getContentPane().add(textArea); frame.setSize(300,300); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { //*********Found********** public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent event) { if(event.getSource()==red) { textArea.setForeground(Color.red); textArea.setText("red menu is selected"); } else if(event.getSource()==blue) { textArea.setForeground(Color.blue); textArea.setText("blue menu is selected"); } else if(event.getSource()==exit) { frame.setVisible(false); System.exit(0); } } public void mousePressed(MouseEvent e) { if(e.getModifiers()==e.BUTTON3_MASK) { pop.show(e.getComponent(),e.getX(),e.getY()); } } public static void main(String args[]) { Java_3 example=new Java_3(); example.initGUI(); } }
3.29
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class Java_3 { public static void main(String[] args) { ExceptTestFrame frame = new ExceptTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class ExceptTestFrame extends JFrame { public ExceptTestFrame() { setTitle("ExceptTest"); Container contentPane = getContentPane(); ExceptTestPanel panel = new ExceptTestPanel(); //*********Found******** contentPane.add(panel); pack(); } } class ExceptTestPanel extends Box { public ExceptTestPanel() { super(BoxLayout.Y_AXIS); group = new ButtonGroup(); addRadioButton("整数被零除", new ActionListener() { //*********Found******** public void actionPerformed(ActionEvent event) { a[1] = 1 / (a.length - a.length); } }); textField = new JTextField(30); add(textField); } //*********Found******** private void addRadioButton(String s, ActionListener listener) { JRadioButton button = new JRadioButton(s, false) { protected void fireActionPerformed(ActionEvent event) { try { textField.setText("No exception"); super.fireActionPerformed(event); } catch (Exception exception) { //*********Found******** textField.setText(exception.toString()); } } }; button.addActionListener(listener); add(button); group.add(button); } private ButtonGroup group; private JTextField textField; private double[] a = new double[10]; }
3.30
import java.awt.*; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet{ //*********Found********** public void init(){ Container contentPane = getContentPane(); JLabel label = new JLabel("Java的诞生是对传统计算模式的挑战!", SwingConstants.CENTER); //*********Found********** contentPane.add(label); } }
3.31
import javax.swing.JOptionPane; public class Java_3 { public static void main( String args[] ){ String firstNumber, //存储第1个输入数据 secondNumber, //存储第2个输入数据 //*********Found******** result; //字符串输出 int number1, //用来比较的第1个int型数据 number2; //用来比较的第2个int型数据 //以字符串格式读输入数据 firstNumber = JOptionPane.showInputDialog( "请输入第1个整数:" ); secondNumber = JOptionPane.showInputDialog( "请输入第2个整数:" ); //将字符串转换为int整数 //*********Found******** number1 = Integer.parseInt( firstNumber ); //*********Found******** number2 = Integer.parseInt( secondNumber ); //用空字符串初始化结果变量 result = ""; if ( number1 == number2 ) result = number1 + " == " + number2; if ( number1 != number2 ) result = number1 + " != " + number2; if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2; //显示结果 JOptionPane.showMessageDialog( null, result, "比较结果", JOptionPane.INFORMATION_MESSAGE); //*********Found******** System.exit( 0 ); } }
3.32
import java.awt.Graphics; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet{ double sum; //存和的变量 //*********Found********** public void init(){ String firstNumber, //输入第1个字符串格式的数 secondNumber; //输入第2个字符串格式的数 double number1, //加数 number2; //被加数 //读入第1个输入的数 firstNumber = JOptionPane.showInputDialog( "Enter first floating-point value" ); //读入第2个输入的数 secondNumber = JOptionPane.showInputDialog( "Enter second floating-point value" ); //将字符串数据转换成双字长类型 number1 = Double.parseDouble( firstNumber ); number2 = Double.parseDouble( secondNumber ); //数据相加 sum = number1 + number2; } public void paint( Graphics g ) { //用g.drawString给结果 g.drawRect( 15, 10, 270, 20 ); g.drawString( "数相加之和为:" + sum, 25, 25 ); } } <html> //*********Found********** <applet code="Java_3.class" width=300 height=50> </applet> </html>
3.33
import java.awt.*; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet{ JTextArea outputArea; public void init(){ outputArea = new JTextArea(); //*********Found********** Container c = getContentPane(); //*********Found********** c.add( outputArea ); //计算0至10的阶乘 for ( long i = 0; i <= 10; i++ ) outputArea.append( i + "! = " + factorial( i ) + "\n" ); } //阶乘的递归定义 public long factorial( long number ){ if ( number <= 1 ) return 1; else return number * factorial( number - 1 ); } }
3.34
import java.awt.*; import java.awt.event.*; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet implements ActionListener{ JLabel prompt; JTextField input; public void init(){ Container c = getContentPane(); c.setLayout( new FlowLayout() ); //*********Found********** prompt = new JLabel( "输入球半径: " ); input = new JTextField( 10 ); //*********Found********** input.addActionListener(this); c.add( prompt ); c.add( input ); } public void actionPerformed( ActionEvent e ){ double radius = Double.parseDouble( e.getActionCommand() ); showStatus( "体积 " + sphereVolume( radius ) ); } public double sphereVolume( double radius ){ double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 ); return volume; } }
3.35
import java.lang.*; import java.util.*; public class Java_3{ public static void main(String[ ]args){ int bound=100; int i=0,j=0,counter=0,k=0; int temp=0; boolean first=true; for(i=1;i<bound;i++){ for(j=1;j<bound;j++){ //*********Found********** temp=i*i +5*j*j; k=(int)Math.sqrt(temp); //*********Found********** if(k<bound && k*k == temp ){ if(first){ System.out.println("The first component: ("+i+", "+j+", "+k+")"); //*********Found********** first=false; } //*********Found********** counter++; } }; } System.out.print("Total number is: "+counter); System.exit(0); } }
3.36
import java.lang.*; import java.util.*; public class Java_3{ public static void main(String[ ]args){ int bound=100; int i=0,j=0,counter=0,k=0; int temp=0; boolean first=true; for(i=1;i<bound;i++){ for(j=1;j<bound;j++){ //*********Found********** temp=i*i +5*j*j; k=(int)Math.sqrt(temp); //*********Found********** if(k<bound && k*k == temp ){ if(first){ System.out.println("The first component: ("+i+", "+j+", "+k+")"); //*********Found********** first=false; } //*********Found********** counter++; } }; } System.out.print("Total number is: "+counter); System.exit(0); } }
3.37
import java.awt.*; import java.awt.event.* ; //*********Found******** import javax.swing.*; //*********Found******** public class Java_3 implements ActionListener{ public static void main(String args[ ]){ Java_3 tb = new Java_3(); //*********Found******** JFrame f = new JFrame("Button Test"); f.setSize(200,100); f.setLayout(new FlowLayout(FlowLayout.CENTER)); JButton b = new JButton("Press the Button!"); /JButton //*********Found******** b.addActionListener(tb); //*********Found******** f.add(b); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); f.setVisible(true) ; } //*********Found******** public void actionPerformed(ActionEvent e){ JFrame fr = new JFrame("An Other"); fr.setBackground(Color.green); fr.add(new JLabel("This frame shows when "+"pressing the button in Button Test")); fr.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); fr.pack(); fr.setVisible(true) ; } }
3.38
import java.io.*; import java.util.*; public class Java_3 { //*********Found********** public static void main (String args[]) throws IOException { FileOutputStream unbufStream = new FileOutputStream("test.one"); BufferedOutputStream bufStream = new BufferedOutputStream( //*********Found********** new FileOutputStream ("test.two")); System.out.println(); System.out.println("这是一个测试缓冲流和非缓冲流速度的程序。"); System.out.println(); //*********Found********** int flag = time(unbufStream)-time(bufStream); if(flag > 0) { System.out.println("测试结果:缓冲流的传输速度快于非缓冲流。"); System.out.println(); } else System.out.println("测试结果:缓冲流的传输速度慢于非缓冲流。"); } //*********Found********** static int time(OutputStream out) throws IOException { //*********Found********** Date then = new Date(); for (int i=0; i<1000; i++) { out.write(1); } //*********Found********** out.close (); return (int)((new Date()).getTime() - then.getTime()); } }
3.39
import java.awt.*; import java.awt.event.*; import javax.swing.*; //**********found********** public class Java_3 extends JFrame { private JTextField username; private JPasswordField password; private JLabel jl1; private JLabel jl2; private JLabel jl3; private JLabel jl4; private JButton bu1; private JButton bu2; private JButton bu3; private JCheckBox jc1; private JCheckBox jc2; private JComboBox jcb; public Java_3() { this.setTitle("QQ2022正式版"); init(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置布局方式为绝对定位 this.setLayout(null); this.setBounds(0, 0, 355, 265); // 设置窗体的标题图标 Image image = new ImageIcon("a.png").getImage(); this.setIconImage(image); // 窗体大小不能改变 this.setResizable(false); // 居中显示 this.setLocationRelativeTo(null); //**********found********** this.setVisible(true); } public void init() { Container con = this.getContentPane(); jl1 = new JLabel(); // 设置背景图片 Image image1 = new ImageIcon("background.jpg").getImage(); jl1.setIcon(new ImageIcon(image1)); jl1.setBounds(0, 0, 355, 265); jl2 = new JLabel(); Image image2 = new ImageIcon("a.gif").getImage(); jl2.setIcon(new ImageIcon(image2)); jl2.setBounds(40, 95, 50, 60); //**********found********** username = new JTextField(); username.setBounds(50, 50, 150, 20); jl3 = new JLabel("注册账号"); jl3.setBounds(210, 50, 70, 20); password = new JPasswordField(); password.setBounds(50, 80, 150, 20); jl4 = new JLabel("找回密码"); jl4.setBounds(210, 80, 70, 20); jc1 = new JCheckBox("记住密码"); jc1.setBounds(125, 135, 80, 15); jc2 = new JCheckBox("自动登录"); jc2.setBounds(215, 135, 80, 15); jcb = new JComboBox(); jcb.addItem("在线"); jcb.addItem("隐身"); jcb.addItem("离开"); jcb.setBounds(40, 135, 55, 20); bu1 = new JButton("登录"); bu1.setBounds(250, 200, 65, 20); bu2 = new JButton("多账号"); bu2.setBounds(25, 200, 75, 20); bu3 = new JButton("设置"); bu3.setBounds(140, 200, 65, 20); bu3.addActionListener(new ActionListener() { //**********found********** public void actionPerformed(ActionEvent e) { if (jc1.isSelected()==true) JOptionPane.showConfirmDialog(null,"确定记住密码吗?"); } }); // 所有组件用容器装载 jl1.add(jl2); jl1.add(jl3); jl1.add(jl4); jl1.add(jc1); jl1.add(jc2); jl1.add(jcb); jl1.add(bu1); jl1.add(bu2); jl1.add(bu3); con.add(jl1); con.add(username); con.add(password); } //**********found********** public static void main (String[] args) { Java_3 qq = new Java_3(); } }
3.40
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Java_3 { public static void main(String[] args) { JFrame frame = new JFrame("Mouse Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new MousePanel()); frame.pack(); frame.setVisible(true); } } //*********Found********** class MousePanel extends JPanel { private JLabel b; public MousePanel() { b = new JLabel(" 起始状态 "); setLayout(new BorderLayout()); add(b, BorderLayout.SOUTH); addMouseMotionListener(new MouseMotion()); setPreferredSize(new Dimension(300, 200)); } //*********Found********** private class MouseMotion extends MouseMotionAdapter { public void mouseMoved(MouseEvent e) { //*********Found********** b.setText(" 鼠标当前位置: "+ e.getX()+ " , " + e.getY()); } } }
3.41
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Java_3 extends JFrame implements ActionListener{ private double x = 0; private double y = 0; JTextField xval = new JTextField(10); JButton calcBtn = new JButton("计算"); JTextArea result = new JTextArea(10,20); void initFrame(){ Container content = getContentPane(); content.setLayout(new BorderLayout()); JPanel calcPanel = new JPanel(); calcPanel.setLayout(new FlowLayout()); calcPanel.add(new JLabel("角度")); calcPanel.add(xval); //***************************Found********************* calcPanel.add(calcBtn); content.add(calcPanel,"North"); //***************************Found********************* calcBtn.addActionListener(this); content.add(result,"Center"); //***************************Found********************* result.setEditable(false); } public Java_3(){ super("计算正弦函数"); setSize(500,200); initFrame(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ //***************************Found********************* if (e.getSource()==calcBtn){ x = Double.parseDouble(xval.getText())/180*3.14; y = Math.sin(x); //***************************Found********************* String str="sin("+ xval.getText() +"*3.14/180)= "+y+'\n'; //***************************Found********************* result.append(str); } } public static void main(String[] args){ new Java_3(); } }
3.42
import java.awt.*; import java.awt.event.*; import javax.swing.*; //**************found***************** public class Java_3 extends Frame implements ItemListener{ Color color = Color.blue; CheckboxGroup cg; Checkbox cb1,cb2,cb3; public Java_3(){ cg = new CheckboxGroup(); setSize(300,300); cb1 = new Checkbox("blue",cg,true); cb2 = new Checkbox("red",cg,true); cb3 = new Checkbox("green",cg,true); cg.setSelectedCheckbox(cb1); add(cb1); add(cb2); add(cb3); cb1.addItemListener(this); cb2.addItemListener(this); //**************found***************** cb3.addItemListener(this); } public void itemStateChanged(ItemEvent e){ if(e.getSource()==cb1) color=Color.blue; if(e.getSource()==cb2) color=Color.red; if(e.getSource()==cb3) color=Color.green; repaint(); } class MyEvent1 extends WindowAdapter { public void windowClosing(WindowEvent e){ System.exit(0); } } class MyEvent2 extends ComponentAdapter { public void componentResized(ComponentEvent e){ repaint(); } } //**************found***************** public void paint(Graphics g) { int w = getWidth(),h=getHeight(); int x0 = w/2,y0 = h/2; g.setColor(color); g.drawLine(x0,0,x0,h); g.drawLine(0,y0,w,y0); double pi = 3.1415926,angle,rou; int r,i,x,y; for(r=10;r<200;r+=20){ for(i=0;i<1023;i++){ angle = i * pi/512; rou = r * Math.sin(2*angle); x = (int)Math.round(rou*Math.cos(angle)); y = (int)Math.round(rou*Math.sin(angle)); g.drawOval(x0+x,y0+y,1,1); } } } public static void main(String[] args){ Java_3 f = new Java_3(); MyEvent1 me1 = f.new MyEvent1(); MyEvent2 me2 = f.new MyEvent2(); //**************found***************** f.setTitle("四叶玫瑰线"); f.setLayout(new FlowLayout()); f.addWindowListener(me1); f.addComponentListener(me2); //**************found***************** f.setVisible(true); } }