【问题标题】:Prevent JFrame from showing any Icon防止 JFrame 显示任何图标
【发布时间】:2014-09-08 23:07:26
【问题描述】:

我不希望我的 jframe 在任务栏中显示任何图标。基本上,如果我们不指定任何IconImage,那么它会显示默认图标。但在我的程序中,我不希望显示任何图标。

 setUndecorated(true);
 setSize(208, 58);
 setImageIcon(null); // same result

如果我将透明图像用作Icon,即使这样,系统也会显示一个半透明的矩形图标。

我的问题是直截了当的。我认为我不需要为它提供任何编码。如果有任何方法可以做到这一点,请告诉我。
一种方法是使用JWindowWindow,但使用它有很多缺点,我不想这样做。

【问题讨论】:

  • JWindow 也会做你想做的事,但你会失去窗口边框......
  • 我不想要窗口边框,正如你在我的编码中看到的那样,我有enabeled underation。问题不在于。@MadProgrammer 但如果我使用 JWindow,我将失去许多其他好处。
  • frame.setIcon(new ImageIcon(new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB))); // important part is.. ARGB 好吧,要么在OS X..上运行代码;)

标签: java swing jframe icons


【解决方案1】:

一种可能的解决方案:不显示 JFrame,而是显示 JDialog。请注意,您放入 JFrame 的 contentPane 中的任何复杂 GUI 都可以放入 JDialog 中,这是避免创建 JFrame 子类的应用程序的另一个原因。例如:

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

public class TestJDialog {
   private static void createAndShowGui() {
      JDialog dialog = new JDialog((JFrame)null, "Test JDialog", true);

      // Using rigid area just to give the dialog size, but you
      // could put any complex GUI in a JPanel in here
      dialog.getContentPane().add(Box.createRigidArea(new Dimension(400, 400)));
      dialog.pack();
      dialog.setLocationByPlatform(true);
      dialog.setVisible(true);
      System.exit(0); // to end Swing event thread
   }

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

缺点是您丢失了窗口右上角的最小化和恢复按钮,但无论如何您都不应该拥有这些按钮,因为您没有用于恢复 GUI 的关联图标。

【讨论】:

  • 谢谢气垫船,它很有帮助,如果我找不到任何其他方法,我会使用它。实际上,我认为使用 JDialog 代替 JFrame 并不好。但你的回答很有帮助。
  • @afzalex:关于"Actually I don't think it will be good to use JDialog in the place of JFrame."——你认为这是为什么?一如既往,很高兴为您服务!
  • @afzalex:如果您喜欢 C 编码,您还可以使用 JNI 或 JNA 进行操作系统调用以从任务栏中删除任何图标。这个解决方案当然是特定于操作系统的。
  • +1 我不知道,但我会想使用 JFrame(这很常见),我没有找到任何代码或任何建议(如来自 oracle 文档)直接创建JDialog,而不是 JFrame。如果我能使用 JFrame 实现它,我会很高兴。
  • @afzalex:不是我对你的问题投反对票,但我仍然想知道你为什么更喜欢使用 JFrame。你这样做的理由是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
  • 1970-01-01
  • 2012-10-15
相关资源
最近更新 更多