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

📄 appletservlet.java

📁 J2EE编程实例
💻 JAVA
字号:
package servletapplet;import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import javax.swing.border.*;import com.borland.jbcl.layout.*;import java.net.*;import java.io.*;import java.util.*;public class AppletServlet extends Applet{  boolean isStandalone = false;  BorderLayout borderLayout1 = new BorderLayout();  //建立布局管理器对象  XYLayout xYLayout1 = new XYLayout();  XYLayout xYLayout4 = new XYLayout();  XYLayout xYLayout3 = new XYLayout();  XYLayout xYLayout2 = new XYLayout();  ButtonGroup buttonGroup1 = new ButtonGroup();  JPanel jMainPanel = new JPanel();  JLabel jLabelTitle = new JLabel();  Border border1;  Border border2;  Border border3;  Border border4;  Border border5;  Border border6;  Border border7;  JLabel jLabel_Result3 = new JLabel();  JLabel jLabel_Result2 = new JLabel();  JLabel jLabel_Result1 = new JLabel();  JPanel jFlowerMeansPanel = new JPanel();  JRadioButton jRadButtonBirthday = new JRadioButton();  JPanel jPanelValue = new JPanel();  JRadioButton jRadButtonLiYi = new JRadioButton();  Choice ChoiceLiYi = new Choice();  JRadioButton jRadButtonConstellation = new JRadioButton();  Choice ChoiceConstellation = new Choice();  JButton jButtonSearch = new JButton();  JLabel jLabel3 = new JLabel();  JLabel jLabel2 = new JLabel();  Choice ChoiceBirthday = new Choice();  JLabel jLabel1 = new JLabel();  JPanel jFlowerMeansChoosePanel = new JPanel();  URLConnection urlconn = null;   //定义Servlet连接对象  URL curl = null;    //定义统一资源定位器(URL)对象  public String getParameter(String key, String def)  {   /* 获得一个参数的值 */    return isStandalone ? System.getProperty(key, def) :      (getParameter(key) != null ? getParameter(key) : def);  }  public AppletServlet()  {   /* 这是一个构造器,由于构造一个AppletServlet对象 */  }  public void init()  {    /* 初始化Applet */    try    {      jbInit();    }    catch(Exception e)    {      e.printStackTrace();    }  }  private void jbInit() throws Exception  {   /* 初始化Applet的组件 */    border1 = BorderFactory.createMatteBorder(1,1,1,1,new Color(255, 105, 223));    border2 = BorderFactory.createMatteBorder(1,1,1,1,new Color(255, 112, 0));    border3 = BorderFactory.createMatteBorder(1,1,1,1,new Color(59, 230, 118));    border4 = BorderFactory.createMatteBorder(1,1,1,1,Color.blue);    border5 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.blue),"选择查询对象");    border6 = new TitledBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.blue),"查询结果");    border7 = BorderFactory.createMatteBorder(0,0,1,0,Color.orange);    this.setLayout(borderLayout1);    jMainPanel.setBackground(SystemColor.textHighlightText);    jMainPanel.setBorder(border1);    jMainPanel.setLayout(xYLayout1);    jLabelTitle.setFont(new java.awt.Font("Dialog", 3, 22));    jLabelTitle.setForeground(new Color(0, 190, 112));    jLabelTitle.setText("网 上 花 店 信 息 系 统");    jRadButtonConstellation.addMouseListener(new AppletServlet_jRadButtonConstellation_mouseAdapter(this));    jRadButtonLiYi.addMouseListener(new AppletServlet_jRadButtonLiYi_mouseAdapter(this));    jRadButtonBirthday.addMouseListener(new AppletServlet_jRadButtonBirthday_mouseAdapter(this));    ChoiceLiYi.setEnabled(false);    ChoiceBirthday.setEnabled(false);    buttonGroup1.add(jRadButtonConstellation);    buttonGroup1.add(jRadButtonLiYi);    buttonGroup1.add(jRadButtonBirthday);    this.addItemIntoChoice();    jLabel_Result3.setBorder(border7);    jLabel_Result3.setText(" ");    jLabel_Result2.setText(" ");    jLabel_Result2.setBorder(border7);    jLabel_Result1.setBorder(border7);    jLabel_Result1.setText(" ");    jFlowerMeansPanel.setBackground(SystemColor.info);    jFlowerMeansPanel.setBorder(border2);    jFlowerMeansPanel.setLayout(xYLayout2);    jRadButtonBirthday.setBackground(SystemColor.info);    jRadButtonBirthday.setText("诞辰与花卉");    jPanelValue.setBackground(SystemColor.info);    jPanelValue.setBorder(border6);    jPanelValue.setLayout(xYLayout4);    jRadButtonLiYi.setText("礼仪与花卉");    jRadButtonLiYi.setBackground(SystemColor.info);    jRadButtonConstellation.setBackground(SystemColor.info);    jRadButtonConstellation.setSelected(true);    jRadButtonConstellation.setText("星座与花卉");    jButtonSearch.setText("查    询");    jButtonSearch.addActionListener(new AppletServlet_jButtonSearch_actionAdapter(this));    jLabel3.setText("寓    意:");    jLabel2.setText("花    名:");    jLabel1.setText("星    座:");    jFlowerMeansChoosePanel.setBackground(SystemColor.info);    jFlowerMeansChoosePanel.setBorder(border5);    jFlowerMeansChoosePanel.setLayout(xYLayout3);    this.add(jMainPanel, BorderLayout.CENTER);    jMainPanel.add(jLabelTitle,      new XYConstraints(131, 8, -1, -1));    jMainPanel.add(jFlowerMeansPanel,       new XYConstraints(8, 70, 465, 285));    jPanelValue.add(jLabel1, new XYConstraints(25, 6, 54, -1));    jPanelValue.add(jLabel_Result1, new XYConstraints(81, 6, 96, -1));    jPanelValue.add(jLabel_Result2, new XYConstraints(263, 5, 162, -1));    jPanelValue.add(jLabel2, new XYConstraints(207, 5, 54, -1));    jPanelValue.add(jLabel_Result3, new XYConstraints(82, 47, 342, -1));    jPanelValue.add(jLabel3, new XYConstraints(26, 47, 54, -1));    jFlowerMeansPanel.add(jFlowerMeansChoosePanel, new XYConstraints(7, 12, 449, 146));    jFlowerMeansPanel.add(jPanelValue, new XYConstraints(6, 161, 449, 116));    jFlowerMeansChoosePanel.add(jRadButtonConstellation, new XYConstraints(27, 9, -1, -1));    jFlowerMeansChoosePanel.add(ChoiceConstellation, new XYConstraints(127, 11, 76, -1));    jFlowerMeansChoosePanel.add(ChoiceLiYi, new XYConstraints(338, 10, 75, -1));    jFlowerMeansChoosePanel.add(jRadButtonLiYi, new XYConstraints(238, 8, -1, -1));    jFlowerMeansChoosePanel.add(jButtonSearch, new XYConstraints(338, 86, 73, 24));    jFlowerMeansChoosePanel.add(ChoiceBirthday, new XYConstraints(128, 43, 74, -1));    jFlowerMeansChoosePanel.add(jRadButtonBirthday, new XYConstraints(28, 41, -1, -1));  }  public String getAppletInfo()  {    /* 获得这个Applet的信息 */    return "Applet Information";  }  public String[][] getParameterInfo()  {    /* 获得这个Applet的参数信息 */    return null;  }  public void addItemIntoChoice()  {   /* 给ChoiceConstellation加入初始值 */   String[] Constellation = {"牡羊座","金牛座","双子座","巨蟹座","狮子座","处女座","天秤座","天蝎座","射手座","魔羯座","水瓶座","双鱼座"};   String[] LiYi = {"结 婚","生 产","乔 迁","生 日","探 病","丧 事"};   String[] BirthdayMonth = {"一  月","二  月","三  月","四  月","五  月","六  月","七  月","八  月","九  月","十  月","十一月","十二月"};   int i = 0;   for(i = 0;i < 12; i++)    {     ChoiceConstellation.add(Constellation[i]);    }   for(i = 0;i < 6; i++)    {     ChoiceLiYi.add(LiYi[i]);    }   for(i = 0;i < 12; i++)    {     ChoiceBirthday.add(BirthdayMonth[i]);    }  }  void jButtonSearch_actionPerformed(ActionEvent e)  {   /* 当按钮被按下时,处理Applet与Servlet之间的通信关系 */   String ssqlcmd = null;   int v = 0;   String queryString = null;   String responseString = null;   String responseString_chs = null;   DataInputStream in = null;   StringTokenizer st = null;   if (jRadButtonConstellation.isSelected() == true)     {      v = ChoiceConstellation.getSelectedIndex() + 1;      ssqlcmd = "Select Constellation,FlowerName,Means from Constellation where ID=" + v;     }   if (jRadButtonLiYi.isSelected() == true)     {      v = ChoiceLiYi.getSelectedIndex() + 1;      ssqlcmd = "Select LiYi,FlowerName,Means from LiYi where ID=" + v;     }   if (jRadButtonBirthday.isSelected() == true)     {      v = ChoiceBirthday.getSelectedIndex() + 1;      ssqlcmd = "Select BirthdayMonth,FlowerName,Means from BirthdayMonth where ID=" + v;     }   if(ssqlcmd != null)     {      /* 格式化URL连接串,并且将其转换为www-form-urlencode的格式 */      try        {         queryString = "http://127.0.0.1/examples/servlet/ServletConnect?ssqlcmd=";         queryString = queryString + URLEncoder.encode(ssqlcmd); //规格化URL串         /* 与Servlet进行通信 */         urlconn = (new URL(curl,queryString)).openConnection();  //打开连接         /* 设置连接时Cache的使用情况 */         urlconn.setDefaultUseCaches(false);         urlconn.setUseCaches(false);         /* 设置输入/输出流的使用情况 */         urlconn.setDoInput(true);         urlconn.setDoOutput(false);         urlconn.connect(); //进行连接         in = new DataInputStream(urlconn.getInputStream());         responseString = in.readLine().toString();  //获取Servlet反馈的信息         responseString_chs = new String(responseString.getBytes("iso-8859-1"));         st = new StringTokenizer(responseString_chs,",");         jLabel_Result1.setText(st.nextToken());         jLabel_Result2.setText(st.nextToken());         jLabel_Result3.setText(st.nextToken());        }      catch(Exception err1)        {         JOptionPane.showMessageDialog(null,"在与Servlet通信过程中出现了下列错误:\n"+err1.toString(),"警  告!",2);        }     }  }  void jRadButtonConstellation_mouseClicked(MouseEvent e)  {   /* 当鼠标选中这个单选框时,激活相应的下拉式列表框 */   ChoiceConstellation.setEnabled(true);   ChoiceLiYi.setEnabled(false);   ChoiceBirthday.setEnabled(false);   jLabel1.setText("星    座:");  }  void jRadButtonLiYi_mouseClicked(MouseEvent e)  {   ChoiceConstellation.setEnabled(false);   ChoiceLiYi.setEnabled(true);   ChoiceBirthday.setEnabled(false);   jLabel1.setText("礼    仪:");  }  void jRadButtonBirthday_mouseClicked(MouseEvent e)  {   ChoiceConstellation.setEnabled(false);   ChoiceLiYi.setEnabled(false);   ChoiceBirthday.setEnabled(true);   jLabel1.setText("生    日:");  }}class AppletServlet_jButtonSearch_actionAdapter implements java.awt.event.ActionListener{  /* 建立事件监听器 */  AppletServlet adaptee;  AppletServlet_jButtonSearch_actionAdapter(AppletServlet adaptee)  {    this.adaptee = adaptee;  }  public void actionPerformed(ActionEvent e)  {    adaptee.jButtonSearch_actionPerformed(e);  }}class AppletServlet_jRadButtonConstellation_mouseAdapter extends java.awt.event.MouseAdapter{  AppletServlet adaptee;  AppletServlet_jRadButtonConstellation_mouseAdapter(AppletServlet adaptee)  {    this.adaptee = adaptee;  }  public void mouseClicked(MouseEvent e)  {    adaptee.jRadButtonConstellation_mouseClicked(e);  }}class AppletServlet_jRadButtonLiYi_mouseAdapter extends java.awt.event.MouseAdapter{  AppletServlet adaptee;  AppletServlet_jRadButtonLiYi_mouseAdapter(AppletServlet adaptee)  {    this.adaptee = adaptee;  }  public void mouseClicked(MouseEvent e)  {    adaptee.jRadButtonLiYi_mouseClicked(e);  }}class AppletServlet_jRadButtonBirthday_mouseAdapter extends java.awt.event.MouseAdapter{  AppletServlet adaptee;  AppletServlet_jRadButtonBirthday_mouseAdapter(AppletServlet adaptee)  {    this.adaptee = adaptee;  }  public void mouseClicked(MouseEvent e)  {    adaptee.jRadButtonBirthday_mouseClicked(e);  }}

⌨️ 快捷键说明

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