2012-07-04 228 views
0

Hı,我有一個Java小程序。在Eclıpseıt完美運行並顯示所有的擺動項目,但是當我將它嵌入到html中時,該applet不會顯示或僅顯示瀏覽器中的一些項目。以下是我的代碼。有人可以幫我解決我的問題嗎?Java applet不在瀏覽器中顯示

import java.applet.Applet; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 
import javax.swing.JApplet; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.SwingConstants; 

import java.applet.*; 
import java.net.*; 
public class TestApplet extends JApplet implements ActionListener { 

    Button talkToAllButton; 
    Button sendPhotoToAll; 
    int Tikla1Basim, Tikla2Basim; 
    JLabel header; 

    JLabel statusLabel; 
    JLabel posLabel; 
    JLabel idLabel; 
    JLabel iDLabel; 
    JLabel heartRateLabel; 
    JLabel heartRateLabel2; 
    JLabel backgroundLabel; 
    JButton sendPicButton; 
    JButton talkOnOffButton; 
    public void init() { 

     this.setLayout(null); 
     setSize(1200, 900); 


     idLabel=new JLabel(new ImageIcon(getImage(getDocumentBase(), "res/call_sign.png"))); 
     idLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
     idLabel.setBackground(Color.white); 
     idLabel.setForeground(Color.black); 
     idLabel.setOpaque(true); 
     idLabel.setBounds(getWidth()-190,200,180,50); 
     //idLabel.setBorder(border); 
     add(idLabel); 

     iDLabel=new JLabel("NONE",JLabel.CENTER); 
     iDLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
     iDLabel.setBackground(new Color(36,48,24)); 
     iDLabel.setForeground(Color.WHITE); 
     iDLabel.setOpaque(true); 
     iDLabel.setBounds(getWidth()-190,250,180,50); 
     //iDLabel.setBorder(border); 
     add(iDLabel); 

     statusLabel=new JLabel(new ImageIcon(getImage(getDocumentBase(), "res/status_alive.png"))); 
     statusLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
     statusLabel.setBackground(Color.green); 
     statusLabel.setForeground(Color.black); 
     statusLabel.setOpaque(true); 
     statusLabel.setBounds(getWidth()-190,300,180,50); 
     //statusLabel.setBorder(border); 
     add(statusLabel); 

     posLabel=new JLabel("NO DATA"); 
     posLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
     posLabel.setHorizontalAlignment(SwingConstants.CENTER); 
     posLabel.setBackground(Color.green); 
     posLabel.setForeground(Color.WHITE); 
     posLabel.setOpaque(true); 
     posLabel.setBounds(getWidth()-190,350,180,70); 
     //posLabel.setBorder(border); 
     add(posLabel); 

     heartRateLabel=new JLabel(new ImageIcon(getImage(getDocumentBase(), "res/hearth_rate.png"))); 
     heartRateLabel.setFont(new Font("Serif", Font.BOLD, 20)); 
     heartRateLabel.setBackground(Color.red); 
     heartRateLabel.setForeground(Color.black); 
     heartRateLabel.setOpaque(true); 
     heartRateLabel.setBounds(getWidth()-190,450,180,50); 
     //heartRateLabel.setBorder(border); 
     add(heartRateLabel); 

     heartRateLabel2=new JLabel("NO DATA",JLabel.CENTER); 
     heartRateLabel2.setFont(new Font("Serif", Font.BOLD, 20)); 
     heartRateLabel2.setBackground(new Color(180,2,2)); 
     heartRateLabel2.setForeground(Color.WHITE); 
     heartRateLabel2.setOpaque(true); 
     heartRateLabel2.setBounds(getWidth()-190,500,180,50); 
     //heartRateLabel2.setBorder(border); 
     add(heartRateLabel2); 

     sendPicButton=new JButton(new ImageIcon(getImage(getDocumentBase(), "res/send_pic.png"))); 
     sendPicButton.setFont(new Font("Serif", Font.BOLD, 20)); 
     sendPicButton.setBackground(Color.red); 
     sendPicButton.setForeground(Color.black); 
     sendPicButton.setOpaque(true); 
     sendPicButton.setBounds(getWidth()-190,580,180,50); 
     //sendPicButton.setBorder(border); 
     add(sendPicButton); 
     sendPicButton.addActionListener(this); 

     talkOnOffButton=new JButton(new ImageIcon(getImage(getDocumentBase(), "res/talk_off.png"))); 
     talkOnOffButton.setFont(new Font("Serif", Font.BOLD, 20)); 

     talkOnOffButton.setOpaque(true); 
     talkOnOffButton.setBounds(getWidth()-190,650,180,50); 
     //talkOnOffButton.setBorder(border); 
     add(talkOnOffButton); 
     talkOnOffButton.addActionListener(this); 
     //*********************************************************** 
     //isFpsLimited = true; 
     backgroundLabel=new JLabel(new ImageIcon(getImage(getDocumentBase(), "res/bg.png"))); 
     backgroundLabel.setOpaque(true); 
     backgroundLabel.setBounds(getWidth()-200,0,200,800); 
     add(backgroundLabel); 

     ImageIcon headerImg=new ImageIcon(getImage(getDocumentBase(),"res/erlink_header.png")); 
     header=new JLabel(headerImg); 
     header.setBounds(-45, -40, 1800, 200); 

     add(header); 
     sendPhotoToAll = new Button("Send Photo(All)"); 
     sendPhotoToAll.setBounds(850,40, 150, 50); 
     header.add(sendPhotoToAll); 
     talkToAllButton = new Button("Talk(All)"); 
     talkToAllButton.setBounds(850,90, 100, 50); 
     header.add(talkToAllButton); 

     sendPhotoToAll.addActionListener(this); 
     talkToAllButton.addActionListener(this); 
     /*sendPhotoToAll.setLocation(10, 10); 
     sendPhotoToAll.setSize(150, 30); 

     talkToAllButton.setLocation(10, 40); 
     talkToAllButton.setSize(250, 30);*/ 

    } 

    public void actionPerformed(ActionEvent event) { 
    //event things 
    } 
    // TODO overwrite start(), stop() and destroy() methods 
} 
+0

請參見[課:Java小程序(http://docs.oracle.com/javase/tutorial/deployment/applet/) – user1329572

+0

不要混用有沒有充分的理由Swing組件AWT。 'Button' - >'JButton'。我在屏幕上看到您的GUI,但很難相信您希望安排這些組件。你可以提供ASCII藝術或繪圖哦GUI的外觀應該如何? –

回答

2
  1. 使用的佈局。
  2. 在添加所有組件後調用validate()
  3. 在EDT上構建GUI。有關更多詳細信息,請參閱Concurrency in Swing
+0

三個好點 – mKorbel

+0

但我看到了佈局的例子。實際上避免使用佈局將會使我的工作減輕很多。我在頂部有一個大橫幅,整個小程序的寬度,右側有一個按鈕列表。我不確定我在這裏做錯了什麼,以便不使用佈局。 – dramaticlook

+0

*「但是我看到了一些沒有佈局的例子。」*它們是不好的例子。別理他們。 *「實際上避免使用佈局將會減輕我的工作量。」*不,它不會。確保開發過程中,但一旦部署完成,它將「全部崩潰」。那些你看過的'例子'的提供者沒有指出。如果你正在設計一個Java GUI來部署到2個或更多的人,佈局是必不可少的。看到[setLayout(null)從來沒有必要。永遠!](https://forums.oracle.com/forums/thread.jspa?threadID=1351374)更多.. –