将 JApplet 添加到 JFrame 中

发布于 2024-11-07 07:04:51 字数 633 浏览 0 评论 0原文

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

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

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

编辑:当我绘制白色矩形时,它修复了背景问题,但菜单列表仍然被覆盖。

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个ˇ局外人 2024-11-14 07:04:51

我会将 GUI 创建转向制作 JPanel,然后根据需要在 JApplet 或 JFrame 中使用 JPanel。例如,

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

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

Then to use in an applet:

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

Or in a 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();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文