2012-08-15 63 views
-1

我有以下类是一个简单的GUI,我想使它成为一个小程序,以便它可以在浏览器中显示。我知道如何将代码嵌入到HTML页面中(完成后)......但是如何让我的课程成为一个小程序?另外,我假设我不需要Web服务器只是为了我的浏览器中显示的小程序...我该如何让这个类是一个小程序

package tester1; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

public class PanelTest implements ActionListener { 

    JFrame frame; 
    JLabel inputLabel; 
    JLabel outputLabel; 
    JLabel outputHidden; 
    JTextField inputText; 
    JButton button; 
    JButton clear; 
    JButton about; 

    public PanelTest() { 
     frame = new JFrame("User Name"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new GridLayout(3, 2, 10, 10)); 

     //creating first row 
     JPanel row1 = new JPanel(); 
     inputLabel = new JLabel("Your Name"); 
     inputText = new JTextField(15); 
//  FlowLayout flow1 = new FlowLayout(FlowLayout.CENTER, 10, 10); 
//  row1.setLayout(flow1); 
     row1.add(inputLabel); 
     row1.add(inputText); 
     frame.add(row1); 
     //creating second row 
     JPanel row2 = new JPanel(); 
     button = new JButton("Display"); 
     clear = new JButton("Clear"); 
     about = new JButton("About"); 
     button.addActionListener(this); 
     clear.addActionListener(this); 
     about.addActionListener(new displayAbout()); 
     row2.add(button); 
     row2.add(clear); 
     row2.add(about); 
     frame.add(row2); 
     //creating third row 
     JPanel row3 = new JPanel(); 
     outputLabel = new JLabel("Output:", JLabel.LEFT); 
     outputHidden = new JLabel("", JLabel.RIGHT); 
//  FlowLayout flow2 = new FlowLayout(FlowLayout.CENTER, 10, 10); 
//  row3.setLayout(flow2); 
     row3.add(outputLabel); 
     row3.add(outputHidden); 
     frame.add(row3); 

     frame.pack(); 
     frame.setVisible(true); 

    } 

    //same method listen for two different events 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     String command = e.getActionCommand(); 
     if(command.equals("Display")) { 
      outputHidden.setText(inputText.getText()); 
     } 
     if(command.equals("Clear")) { 
      outputHidden.setText(""); 
      inputText.setText(""); 
     }   
    } 

    //another way to listen for events 
    class displayAbout implements ActionListener { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      JOptionPane.showMessageDialog(frame, "Username 1.1 \n by Jorge L. Vazquez"); 
     } 
    }  

    public static void main(String[] args) { 
     PanelTest frameTest = new PanelTest(); 
    } 

} 
+0

您需要阅读[applet教程](http://docs.oracle.com/javase/tutorial/deployment/applet/index.html),纯粹而简单。对不起,但你的帖子不是一个可回答的问题,除了是时候做你的学习了。投票结束。 – 2012-08-15 21:33:28

+2

*“我想使它成为一个小程序”*不宜。相反,启动应用程序。从一个链接使用[Java Web Start](http://stackoverflow.com/tags/java-web-start/info)。 – 2012-08-15 22:15:44

+1

另请参阅此[答案](http://stackoverflow.com/a/11479113/230513)。 – trashgod 2012-08-16 01:06:18

回答

0

使用JApplet而非JFrame。请确保您阅读the relevant Java Tutorial,其中涵盖了小程序生命周期方法,如init,start,stopdestroy


作为一个侧面说明,你应该是建立在事件派发线程之外的UI。

0

使用JApplet,而不是像JFrame说德维尔,但你也必须删除frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.pack();frame.setVisible(true);

而且,与init()更换main(String[] args)

+0

什么时候需要扩展JApplets类 – miatech 2012-08-15 20:58:15

+0

您将'frame'更改为JApplet。 – Doorknob 2012-08-15 20:59:04

+0

netbeans发出错误“class panelTest没有主要方法” – miatech 2012-08-15 21:02:18