我试图使小程序作为JFrame运行。我下面的代码很简单,但应该可以工作。它将作为JApplet运行,但是当我转到RUN AS->时,什么也没有出现。

导入java.awt。*;
导入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();
    }
}


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

最佳答案

不要将AWT(Applet)与Swing组件混合使用。坚持挥杆。
使您的课程更适合创建JPanels。然后,如果需要小程序,可以将其放在JApplet中;如果需要JFrame,则可以将其放在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();
      }
   }
}

关于java - 为什么此代码注释可以作为JFrame运行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20456774/

10-11 22:38
查看更多