请教一个Java编程问题
java吧
全部回复
仅看楼主
level 1
hxtlinda 楼主
请问如何把以下三个小程序合成一个大程序(彩票机选系统的背景颜色可以根据按钮切换,并且字幕有聚光灯效果),三个小程序独立运行没有问题
第一个是彩票机选系统
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class caipiao extends JApplet implements ActionListener{
JLabel Label1 , zhi;
JTextField xianshi, he ;
JTextArea output;
JButton roll;
public void init()
{ Container c=getContentPane();
c.setLayout(new FlowLayout());
output = new JTextArea(5,5);
output.setFont(new Font("courier",Font.BOLD+Font.ITALIC,27));
c.add(output);
output.setText(" 欢迎使用机选抽奖系统 ");
output.setEditable(false);
Label1=new JLabel("您选出的数字为:");
c.add(Label1);
xianshi = new JTextField(18);
xianshi.setEditable(false);
c.add(xianshi);
zhi = new JLabel("总值为:");
c.add(zhi);
he = new JTextField(5);
he.setEditable(false);
c.add(he);
roll = new JButton("开始选择");
roll.addActionListener(this);
c.add(roll);
}
public void actionPerformed(ActionEvent e)
{ play(); }
public void play()
{ int sum=0;
String shuchu[] , printout = "";
int die[] = new int[8];
shuchu = new String[8];
for(int i=0 ; i<8; i++)
{
die[i]= 1 + (int)(Math.random()*30);
sum+=die[i];
}
for(int k=0;k<8;k++)
{ for(int n=k+1;n<8;n++)
if(die[k]==die[n])
die[n] = 1 + (int)(Math.random()*30);
}
paixu(die);
for(int j=0;j<8;j++)
{
shuchu[j] = Integer.toString(die[j]);
printout+= shuchu[j]+ " ";
}
xianshi.setText(printout);
he.setText(Integer.toString(sum));
}
public void paixu(int b[])
{ for(int pass=0;pass<b.length;pass++)
for(int i=0;i<b.length-1;i++)
if(b[i]>b[i+1])
swap(b , i , i+1);
}
public void swap(int c[], int first, int second)
{ int hold ;
hold = c[first];
c[first] = c[second];
c[second] = hold;
}
}
########################
第二个是点击按钮改变颜色
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTestApplet extends JApplet
{
public void init()
{
        ButtonPanel buttonpanel = new ButtonPanel();
        Container container = getContentPane();

2009年01月08日 12点01分 1
level 1
hxtlinda 楼主
        container.add(buttonpanel);
}
}
class ButtonPanel extends JPanel
{
    private class ColorAction implements ActionListener
    {
        private Color backgroundColor;
        public void actionPerformed(ActionEvent actionevent)
        {
            setBackground(backgroundColor);
            repaint();
        }
        public ColorAction(Color color)
        {
            backgroundColor = color;
        }
    }
    public ButtonPanel()
    {
        JButton jbutton = new JButton("橙色");
        JButton jbutton1 = new JButton("蓝色");
        JButton jbutton2 = new JButton("红色");
        add(jbutton);
        add(jbutton1);
        add(jbutton2);
        ColorAction coloraction = new ColorAction(Color.orange);
        ColorAction coloraction1 = new ColorAction(Color.blue);
        ColorAction coloraction2 = new ColorAction(Color.red);
        jbutton.addActionListener(coloraction);
        jbutton1.addActionListener(coloraction1);
        jbutton2.addActionListener(coloraction2);
    }
}
####################
第三个是聚光灯效果的字幕
import java.awt.*;
import java.applet.*;
public class SpotLight extends Applet implements Runnable
{
   private String text;
   private Font font;
   private Color c, spot;
   private int font_size;
   private Thread thread;
   private int textpt = 50;
   private int text_size = 20;

2009年01月08日 12点01分 2
level 1
hxtlinda 楼主
   private int text_width = 0;
   private int font_height,base_line,w;
   public void init()
   {
      String fonts, temp;
      text = getParameter("message");
      if(text == null)
         text = "我是明星~I'm super star~";
      fonts = getParameter("font_size");
      if(fonts == null)
         font_size = 27;
      else
         font_size = Integer.parseInt(fonts);
      font = new Font("TimesRoman", Font.BOLD, font_size);
      FontMetrics fm = getFontMetrics(font);
      font_height = fm.getHeight();
      base_line = getSize().height / 2 + font_height / 3;
      text_width = fm.stringWidth(text);
      w = fm.stringWidth(text);
      w = (getSize().width - w) / 2;
      textpt = w;
      c = new Color(255, 0, 0);
      spot = new Color(255, 255, 255);
      setBackground(Color.black);
   }
   public void start()
   {
      if(thread == null)
          {
         thread = new Thread(this);
         thread.start();
      }
   }
   public void stop()
   {
      thread.stop();
      thread = null;
   }
   public void run()
   {
      while(true)
          {
         repaint();
         try { Thread.sleep(50); }
                 catch(InterruptedException e) { }
      }
   }
   public void update(Graphics g)
   {
       paint(g);
   }
   public void paint(Graphics g)
   {
      g.setFont(font);
      g.setColor©;
      g.drawString(text, w, base_line);
      g.clipRect(textpt, 0, text_size, getSize().height);
      g.setColor(spot);
      g.drawString(text, w, base_line);
      textpt = (textpt + 1) % (text_width + 100);
   }
}

2009年01月08日 12点01分 3
level 1
hxtlinda 楼主
呀,一下子发了三篇 =口=
2009年01月08日 12点01分 4
1