⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 通过实例可以更好的了解java
💻
📖 第 1 页 / 共 2 页
字号:
14-例子1
import java.applet.*;
import java.awt.*; 
public class Example14_1 extends Applet 
{ Choice choice1,choice2; 
  TextField text1,text2;
  public void init()
  { setLayout(new GridLayout(2,2));
    choice1=new Choice();      choice2=new Choice();
    text1=new TextField(10);   text2=new TextField(10);
    choice1.add("音乐天地");     choice1.add("武术天地");
    choice1.add("象棋乐园");     choice1.add("交友聊天");
    choice2.add("辽宁日报");     choice2.add("北京日报");
    choice2.add("上海日报");     choice2.add("足球晚报");
    add(choice1);  add(choice2);add(text1); add(text2);
  }   
}



14-例子2
import java.applet.*; import java.awt.*;
import java.awt.event.*;
public class Example14_2 extends Applet implements ItemListener
{ Choice choice1,choice2; 
  TextField text1,text2;
  public void init()
 { setLayout(new GridLayout(2,2));
  choice1=new Choice();
  choice2=new Choice();
  text1=new TextField(10);
  text2=new TextField(10);
  choice1.add("音乐天地");
  choice1.add("武术天地");
  choice1.add("象棋乐园");
  choice1.add("交友聊天");
  choice2.add("辽宁日报");    choice2.add("北京日报");
    choice2.add("上海日报");    choice2.add("足球晚报");
  add(choice1);  add(choice2);  add(text1); add(text2);
  choice1.addItemListener(this);choice2.addItemListener(this);
  } 
  public void itemStateChanged(ItemEvent e)
  {  if(e.getItemSelectable()==choice1)
      {  if(choice1.getSelectedIndex()==0)
        { text1.setText("小提琴曲:梁祝") ;}
       else if(choice1.getSelectedIndex()==1)
        { text1.setText("少林寺:少林拳") ;} 
       else if(choice1.getSelectedIndex()==2)
        { text1.setText("和为贵") ;}
       else if(choice1.getSelectedIndex()==3)
        { text1.setText("知心朋友") ;} 
     }
    else if(e.getItemSelectable()==choice2)
     {  if(choice2.getSelectedIndex()==0)
        { text2.setText("信息很好") ;}
       else if(choice2.getSelectedIndex()==1)
       { text2.setText("每天六版") ;} 
       else if(choice2.getSelectedIndex()==2)
        { text2.setText("很难看到") ;}
       else if(choice2.getSelectedIndex()==3)
        { text2.setText("球迷的天地") ;} 
    }
}
}



14-例子3
import java.applet.*;
import java.awt.*;
public class Example14_3 extends Applet 
{ List list1,list2,list3;  TextField text1,text2;
  public void init()
  { setLayout(new GridLayout(2,3));
    list1=new List(1);   list2=new List(2);   list3=new List(2,true);
    text1=new TextField(10);    text2=new TextField(10);
    list1.add("音乐天地");    list1.add("武术天地");
    list1.add("象棋乐园");    list1.add("交友聊天");
    list2.add("辽宁日报");    list2.add("北京日报");
    list2.add("上海日报");    list2.add("足球晚报");
    list3.add("拳击俱乐部");  list3.add("击剑俱乐部");
    list3.add("摔交俱乐部");  list3.add("游泳俱乐部");
    list3.add("健美俱乐部");  list3.add("吹牛俱乐部");
    add(list1);add(list2);  add(list3);
    add(text1);add(new Label());add(text2);
  }   
}




14-例子4
import java.applet.*;import java.awt.*;
import java.awt.event.*;
public class Example14_4 extends Applet implements ItemListener,
ActionListener
{ List list1,list2;   TextArea text1,text2;
  public void init()
  { setLayout(new GridLayout(2,2));
  list1=new List(3,false); list2=new List(3,false);
  Panel p1=new Panel();p1.add(list1);
  Panel p2=new Panel();p2.add(list2);
  text1=new TextArea(10,10);text2=new TextArea(10,10);
  list1.add("计算1+2+...");list1.add("计算1*1+2*2+3*3...");
  list1.add("计算1*1*1+2*2*2+3*3*3...");
  for(int i=1;i<=100;i++)
   {list2.add("前"+i+"项和");
   }
  add(p1);  add(p2);add(text1); add(text2);
  list1.addItemListener(this);list2.addActionListener(this);
  } 
  public void itemStateChanged(ItemEvent e)
  {  if(e.getItemSelectable()==list1)
    {if(list1.getSelectedIndex()==0)
     { text1.setText("计算1+2+...") ;}
     else if(list1.getSelectedIndex()==1)
     { text1.setText("计算1*1+2*2+3*3...") ;} 
     else if(list1.getSelectedIndex()==2)
      { text1.setText("计算1*1*1+2*2*2+3*3*3...") ;}
    }
  }
 public void actionPerformed(ActionEvent e)
 { if(e.getSource()==list2)
   { if(list1.getSelectedIndex()==0)
      { String s=list2.getSelectedItem();
        int n=list2.getSelectedIndex();
        long sum=0;
        for(long i=1;i<=n+1;i++)
        {sum=sum+i;}
        text2.setText("结果:"+sum);
       }
     else if(list1.getSelectedIndex()==1)
      { String s=list2.getSelectedItem();
        int n=list2.getSelectedIndex();
        long sum=0;
        for(long i=1;i<=n+1;i++)
        {sum=sum+i*i;}
        text2.setText("结果:"+sum);
       }
      else if(list1.getSelectedIndex()==2)
      { String s=list2.getSelectedItem();
        int n=list2.getSelectedIndex();
        long sum=0;
        for(long i=1;i<=n+1;i++)
        {sum=sum+i*i*i;}
        text2.setText("结果:"+sum);
       }
   } 
 }
}



14-例子5
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example14_5 extends Applet implements ActionListener
{ Button button1,button2,button3;
  TextField text1;TextArea text2;
  Font f1,f2,f3,f4,f5; 
  Toolkit toolkit; 
  public void init()
  { f1=new Font("Helvetica",Font.PLAIN,18);//创建字体对象。
    f2=new Font("Helvetica",Font.BOLD,10);
    f3=new Font("Helvetica",Font.ITALIC,12);
    f4=new Font("Courier",Font.PLAIN,12);
    f5=new Font("TimesRoman",Font.BOLD+Font.ITALIC,14);
    toolkit=getToolkit();//获得一个工具包对象。
    text1=new TextField(15);
    text2=new TextArea(15,15);
    button1=new Button("Save");
    button1.setFont(f1);//设置按钮的字体
    button1.setBackground(Color.red);//设置按钮的颜色。
    button1.setForeground(Color.blue);//按钮名字的颜色。
    button2=new Button("确定");button3=new Button("老猫");
    button2.setFont(f2);button2.setBackground(Color.blue);
    button1.setForeground(Color.red);
    text1.setBackground(Color.cyan);
    text2.setBackground(Color.green);//设置文本框的底色。
    text1.setForeground(Color.red);
    text2.setForeground(Color.yellow);//文本框中的文本所呈现的颜色。
    text2.setFont(f3);
    add(text1); add(button1);add(button2);add(button3);add(text2);
    button2.addActionListener(this); 
    button3.addActionListener(this); 
  } 
 public void actionPerformed(ActionEvent e)
  {if(e.getSource()==button2)
   {for(int i=0;i<=2;i++)
     {toolkit.beep();}//发出"嘟嘟嘟"声。
    button1.setFont(f4);button1.setBackground(Color.green);
    button1.setForeground(Color.red);
    button1.setSize(10,10);
    text1.setVisible(false);
    text2.setForeground(Color.red);
    text2.setFont(f5);
   }
  else if(e.getSource()==button3)
 {  for(int i=0;i<=6;i++)
   {toolkit.beep();}//发出"嘟嘟嘟"声。
    button1.setFont(f4);button1.setBackground(Color.green);
    button1.setForeground(Color.red);
    button1.setSize(40,60);
    text1.setVisible(true);
    text2.setForeground(Color.blue);text2.setBackground(Color.red);
    text2.setFont(f1);
     text1.setLocation(120,200);
  } 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -