2012-08-15 59 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