2013-12-08 44 views
0

我想让一个applet作为JFrame运行。我下面的代码很简单,但应该可以工作。它将作为一个JApplet运行,但是当我运行时 - >没有任何显示。为什么此代码注释可以作为JFrame运行?

import java.awt。*; import java.applet.Applet;

import javax.swing.BoxLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class LifeCycle extends Applet 
{ 

    private static final long serialVersionUID = 1L; 
    String output = "test"; 
    String event; 

    public void init() 
    { 
       gui(); //I am not certain if this needs to be there. 
     event = "\nInitializing..."; 
     printOutput(); 
    } 

    public void start() 
    { 
     event = "\nStarting..."; 
     printOutput(); 
    } 

    public void stop() 
    { 
     event = "\nStopping..."; 
     printOutput(); 
    } 

    public void destroy() 
    { 
     event = "\nDestroying..."; 
     printOutput(); 
    } 

    private void printOutput() 
    { 
     System.out.println(event); 
     output += event; 
     repaint(); 
    } 

    private void gui() { 
      JFrame f = new JFrame("Not resizable"); 
      JPanel d = new JPanel(); 
     // LifeCycle a = new LifeCycle(); 
     // a.init();//not working 
     d.setLayout(new BorderLayout()); 
     d.add(new JButton("a")); 
     d.add(new JButton("b")); 
     d.setBackground(Color.RED); 
     //f.add(new LifeCycle()); 
     f.add(d); 
     f.setSize(545,340); 
     f.setResizable(false); 
     f.setLocationRelativeTo(null); 
     f.setTitle("Test"); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     //a.destroy(); 
    } 

    public void paint(Graphics g) 
    { 
     System.out.println("Graphics Paint Method!"); 
     g.drawString(output, 100, 100); 
    } 

    public static void main(String[] args) { 
     LifeCycle l = new LifeCycle(); 
     l.gui(); 
    } 
} 

我想看看应该改变的代码,但我似乎无法找到为什么这不起作用。我已将按钮添加到要显示的面板。

回答

4
  • 请勿将AWT(Applet)与Swing组件混合使用。坚持只是摇摆。
  • 将你的课程转化为创建JPanel。然后,如果您想要JFrame,则可以将它放在JApplet中,如果您需要小程序或JFrame。
  • 阅读使用BorderLayout - 您将多个组件添加到默认的BorderLayout.CENTER位置,并且只会添加最后一个添加的组件。

例如...

LifeCycle2.java

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridLayout; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JPanel; 

class LifeCycle2 { 
    private static final int GAP = 5; 
    private static final int PREF_W = 545; 
    private static final int PREF_H = 340; 
    private JPanel mainPanel = new JPanel() { 
     @Override 
     public Dimension getPreferredSize() { 
     return LifeCycle2.this.getPreferredSize(); 
     } 
    }; 

    public LifeCycle2() { 
     JPanel buttonPanel = new JPanel(new GridLayout(1, 0, GAP, 0)); 
     buttonPanel.add(new JButton("A")); 
     buttonPanel.add(new JButton("B")); 
     buttonPanel.setOpaque(false); 
     JPanel flowLayoutPanel = new JPanel(); 
     flowLayoutPanel.setOpaque(false); 
     flowLayoutPanel.add(buttonPanel); 

     mainPanel.setLayout(new BorderLayout()); 
     mainPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP)); 
     mainPanel.setBackground(Color.red); 
     mainPanel.add(flowLayoutPanel, BorderLayout.NORTH); 
    } 

    public Dimension getPreferredSize() { 
     return new Dimension(PREF_W, PREF_H); 
    } 

    public JComponent getMainPanel() { 
     return mainPanel; 
    } 
} 

显示为一个JFrame, LifeCycleFrame.java

import javax.swing.*; 

public class LifeCycleFrame { 

    private static void createAndShowGui() { 
     LifeCycle2 lifeCycle2 = new LifeCycle2(); 

     JFrame frame = new JFrame("LifeCycleTest"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(lifeCycle2.getMainPanel()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 
} 

显示为一个小程序, LifeCycleApplet.java

import java.lang.reflect.InvocationTargetException;  
import javax.swing.JApplet; 
import javax.swing.SwingUtilities; 

public class LifeCycleApplet extends JApplet { 
    @Override 
    public void init() { 
     try { 
     SwingUtilities.invokeAndWait(new Runnable() { 
      public void run() { 
       LifeCycle2 lifeCycle2 = new LifeCycle2(); 
       getContentPane().add(lifeCycle2.getMainPanel()); 
      } 
     }); 
     } catch (InvocationTargetException | InterruptedException e) { 
     e.printStackTrace(); 
     } 
    } 
} 
相关问题