为什么此代码注释可以作为JFrame运行?

时间:2013-12-08 17:51:04

标签: java swing jframe

我正在尝试将applet作为JFrame运行。我下面的代码很简单,但应该可以工作。它将作为JApplet运行但是当我转到RUN AS时 - >没有出现。

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();
    }
}

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

2 个答案:

答案 0 :(得分:4)

  • 不要将AWT(Applet)与Swing组件混合使用。坚持只是摆动。
  • 让你的班级开始创建JPanels。如果你想要一个JFrame,你可以将它放在JApplet中,如果你想要一个JFrame,你可以将它放在JApplet中。
  • 阅读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();
         }
      });
   }
}

显示为applet, 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();
      }
   }
}

答案 1 :(得分:1)

f.setVisible(true);添加到gui()方法的末尾。如果没有此调用,您的框架将不会显示。

请阅读"How to Make Frames" Tutorial