将JApplet添加到JFrame [英] Adding JApplet into JFrame

查看:72
本文介绍了将JApplet添加到JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JFrame中查看JApplet.

I am trying to view a JApplet within a JFrame.

Class: Paint
public void paint(Graphics g) {
  g.drawString("hi", 50, 50);
}

public static void main(String args[]) {
  JFrame frame = new JFrame("test");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setJMenuBar(methodThatReturnsJMenuBar());

  JPanel panel = new JPanel(new BorderLayout());
  frame.add(panel);

  JApplet applet = new Paint();
  panel.add(applet, BorderLayout.CENTER);
  applet.init();
  frame.pack();

  frame.setVisible(true);
}

该小程序显示在窗口"中,但是没有背景(透明),当我单击菜单"时,该列表将被覆盖.如何使菜单列表不被覆盖并且有背景?

The applet shows up in the Window, but there is no background (it's transparent), and when I click on the Menu, the list is covered. How do I make it so that the Menu list isn't covered, and there is a background?

当我绘制一个白色矩形时,它解决了背景问题,但是菜单列表仍然被覆盖.

When I draw a white rectangle, it fixes the background problem, but the Menu list is still covered.

推荐答案

我将把我的GUI创建调整为制作JPanel,然后根据需要在JApplet或JFrame中使用JPanel.例如,

I would gear my GUI creation towards making a JPanel and then use the JPanel as I desire, either in an JApplet or a JFrame. For e.g.,

import java.awt.*;
import javax.swing.*;

public class MyPanel extends JPanel {
   private static final Dimension PREF_SIZE = new Dimension(400, 300);

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawString("hi", 50, 50);
   }

   @Override
   public Dimension getPreferredSize() {
      return PREF_SIZE;
   }

   public JMenuBar methodThatReturnsJMenuBar() {
      JMenu menu = new JMenu("Menu");
      JMenuBar menuBar = new JMenuBar();
      menuBar.add(menu);
      return menuBar;
   }
}

然后在小程序中使用:

import javax.swing.JApplet;

public class MyApplet extends JApplet {
   public void init() {
      try {
         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               createGUI();
            }
         });
      } catch (Exception e) {
         System.err.println("createGUI didn't successfully complete");
      }
   }

   private void createGUI() {
      getContentPane().add(new MyPanel());
   }
}

或在JFrame中:

import javax.swing.JFrame;

public class MyStandAlone {
   private static void createAndShowUI() {
      MyPanel myPanel = new MyPanel();
      JFrame frame = new JFrame("MyPanel");
      frame.getContentPane().add(myPanel);
      frame.setJMenuBar(myPanel.methodThatReturnsJMenuBar());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

这篇关于将JApplet添加到JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆