html嵌入applet及applet实现界面跳转的方法

来源:互联网 发布:图书馆数据库需求分析 编辑:程序博客网 时间:2024/05/16 18:12

mainUi.html  文件

<html><head><title>applet test</title></head><body> <APPLET CODE="mainUi.class" archive=mainUi.jar width=1020 height=600></APPLET></body></html>

mainUi.java


import java.util.*;import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import javax.swing.JPanel;import java.net.URL;public class mainUi extends JApplet{private JButton suspend, cold, bt, flight;private JButton ar, ss, sm, lr;private JButton runCase;JComboBox selecCase;String[] usrCase = { "case1", "case2", "case3" };private static int SUSPEND = 0;public void init() {String laf = UIManager.getSystemLookAndFeelClassName();try {    //UIManager.setLookAndFeel(laf);UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");    // If you want the Cross Platform L&F instead, comment out the above line and    // uncomment the following:    // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());} catch (UnsupportedLookAndFeelException exc) {    System.err.println("Warning: UnsupportedLookAndFeel: " + laf);} catch (Exception exc) {    System.err.println("Error loading " + laf + ": " + exc);}ImageIcon image = new ImageIcon("/home/stedy/gy/android.png");//JFrame jf = new JFrame("Swing Test");//Container conPane = jf.getContentPane();Container conPane = getContentPane();conPane.setLayout(new GridLayout(5, 1, 20, 20));JSeparator separator = new JSeparator(JSeparator.HORIZONTAL);Font font = new Font("name",Font.ITALIC,30);mainListener listener = new mainListener();// top containerJPanel top = new JPanel();top.setLayout(new GridLayout(1, 2, 50, 50));JLabel r1 = new JLabel("Performance");r1.setIcon(image);r1.setIconTextGap(10);r1.setHorizontalTextPosition(JLabel.CENTER);r1.setVerticalTextPosition(JLabel.BOTTOM);r1.setFont(font);top.add(r1);// performanceJPanel performance = new JPanel();performance.setLayout(new GridLayout(4, 1));suspend = new JButton("Suspend");cold = new JButton("Cold Boot");bt = new JButton("BT");flight = new JButton("Flight Mode");ImageIcon susImage = new ImageIcon("/home/stedy/gy/android.png");ImageIcon coldImage = new ImageIcon("/home/stedy/gy/android.png");ImageIcon btImage = new ImageIcon("/home/stedy/gy/android.png");ImageIcon flightImage = new ImageIcon("/home/stedy/gy/android.png");suspend.setIcon(susImage);cold.setIcon(coldImage);bt.setIcon(btImage);flight.setIcon(flightImage);suspend.setBorder(BorderFactory.createEmptyBorder());suspend.addActionListener(listener);cold.addActionListener(listener);bt.addActionListener(listener);flight.addActionListener(listener);performance.add(suspend);performance.add(cold);performance.add(bt);performance.add(flight);top.add(performance);// middle containerJPanel middle = new JPanel();middle.setLayout(new GridLayout(1, 2, 50, 50));// middle.setBackground(Color.BLUE);JLabel r2 = new JLabel("SQ Helper");r2.setFont(font);middle.add(r2);// sq helperJPanel sqhelper = new JPanel();sqhelper.setLayout(new GridLayout(4, 1));ar = new JButton("Action Recorder");ss = new JButton("Screen Spyder");sm = new JButton("System Monitor");lr = new JButton("Log Recorder");ar.addActionListener(listener);ss.addActionListener(listener);sm.addActionListener(listener);lr.addActionListener(listener);sqhelper.add(ar);sqhelper.add(ss);sqhelper.add(sm);sqhelper.add(lr);middle.add(sqhelper);// bottom containerJPanel bottom = new JPanel();bottom.setLayout(new GridLayout(1, 2, 50, 50));// bottom.setBackground(Color.GREEN);JLabel r3 = new JLabel("Customize test case");r3.setFont(font);bottom.add(r3);// customize test caseJPanel customCase = new JPanel();customCase.setLayout(new GridLayout(2, 1));selecCase = new JComboBox(usrCase);selecCase.addItemListener(new listListener());runCase = new JButton("Run");runCase.addActionListener(listener);customCase.add(selecCase);customCase.add(runCase);bottom.add(customCase);//add top/middle/botton containerconPane.add(top);conPane.add(separator);conPane.add(middle);conPane.add(separator);conPane.add(bottom);// jf.pack();// jf.show();}public void start() {}public void stop() {}public void destory() {}private class listListener implements ItemListener{public void itemStateChanged(ItemEvent evt){if(evt.SELECTED == evt.getStateChange()){JOptionPane.showMessageDialog(getParent(), evt.getItem().toString());}}}public void buttonHandler(int id){if(SUSPEND == id){try{//getAppletContext().showDocument(new URL("http://localhost:8080/test/test.jsp"));getAppletContext().showDocument(new URL("http://localhost:8080/SwingApplet/SwingApplet.html"));}catch(Exception e){e.printStackTrace();}}}private class mainListener implements ActionListener {public void actionPerformed(ActionEvent e) {if (e.getSource() == suspend) {JOptionPane.showMessageDialog(getParent(), "suspend is clicked");buttonHandler(SUSPEND);} else if (e.getSource() == cold) {JOptionPane.showMessageDialog(getParent(), "cold is clicked");} else if (e.getSource() == bt) {JOptionPane.showMessageDialog(getParent(), "bt is clicked");} else if (e.getSource() == flight) {JOptionPane.showMessageDialog(getParent(), "flight is clicked");} else if (e.getSource() == ar) {JOptionPane.showMessageDialog(getParent(), "ar is clicked");} else if (e.getSource() == ss) {JOptionPane.showMessageDialog(getParent(), "ss is clicked");} else if (e.getSource() == sm) {JOptionPane.showMessageDialog(getParent(), "sm is clicked");} else if (e.getSource() == lr) {JOptionPane.showMessageDialog(getParent(), "lr is clicked");} else if (e.getSource() == runCase) {JOptionPane.showMessageDialog(getParent(), "runCase is clicked");} else {// do nothing}}}}

将mainUi.java 与mainUi.html置于相同文件下,然后分别执行 javac mainUi.java                 jar -cvf mainUi.jar(此处与mainUi.html对应)  mainUi.class

将整个文件夹置于tomcate webapps目录下,即可通过浏览器访问,注意 本地访问需去掉代理。





原创粉丝点击