java二级试卷24

import javax.swing.*;

public class Java_1
{
   //*********Found**********
   public static void main(String[] args)
   {
      System.out.println();
      System.out.println("这是一个指定球半径,求球体积的程序。");
      String input=JOptionPane.showInputDialog("请输入球半径。");
      //*********Found**********
      double r=Double.parseDouble(input);
      System.out.println("当球的半径是" + r + "时,该球的体积是    " + (Math.PI*r*r*r*4/3));
      System.exit(0);
   }
}

public  class  Java_2
{    
   public static void main(String[] args)
   {
      Thread t = new SimpleThread("Testing_Thread");
       //*********Found**********
      t.start() ;   
   }
} 
 //*********Found**********
class SimpleThread extends Thread 
{
   public SimpleThread(String str)
   {
      //*********Found**********
      super(str)  ;
   }
    //*********Found**********
   public void run()
   {  
      System.out.println("Running the " + getName() + ":");
      for (int i = 0; i < 5; i++)
      {
         System.out.println("---" + i + "---" + getName());
         try
         {
            sleep((int)(2 * 100));
         }
         //*********Found**********
         catch(InterruptedException e) { }
      }
   }
}

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;
   }
}

猜你喜欢

转载自blog.csdn.net/qq_38640439/article/details/82417290