首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Swing问题

Java Swing问题
EN

Stack Overflow用户
提问于 2018-02-21 20:48:21
回答 3查看 86关注 0票数 0

我从一本关于swing的旧书中扩展了一个小的MDI示例程序,这样UI看起来就像系统UI。下面是代码

代码语言:javascript
复制
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class DesktopSample {

  public static void main(final String[] args) {
    Runnable runner = new Runnable() {
      public void run() {
        try {
          UIManager.setLookAndFeel(
                  UIManager.getSystemLookAndFeelClassName());
        }
        catch (UnsupportedLookAndFeelException e) { }
        catch (ClassNotFoundException e) { }
        catch (InstantiationException e) { }
        catch (IllegalAccessException e) { }

        String title = (args.length==0 ? "Desktop Sample" : args[0]);
        JFrame frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JDesktopPane desktop = new JDesktopPane();
        JInternalFrame internalFrames[] = {
          new JInternalFrame("Can Do All", true, true, true, true),
          new JInternalFrame("Not Resizable", false, true, true, true),
          new JInternalFrame("Not Closable", true, false, true, true),
          new JInternalFrame("Not Maximizable", true, true, false, true),
          new JInternalFrame("Not Iconifiable", true, true, true, false)
        };

        InternalFrameListener internalFrameListener = new InternalFrameIconifyListener();

        int pos = 0;
        for(JInternalFrame internalFrame: internalFrames) {
          // Add to desktop
          desktop.add(internalFrame);

          // Position and size
          internalFrame.setBounds(pos*25, pos*25, 200, 100);
          pos++;

          // Add listener for iconification events
          internalFrame.addInternalFrameListener(internalFrameListener);

          JLabel label = new JLabel(internalFrame.getTitle(), JLabel.CENTER);
          internalFrame.add(label, BorderLayout.CENTER);

          // Make visible
          internalFrame.setVisible(true);
        }

        JInternalFrame palette = new JInternalFrame("Palette", true, false, true, false);
        palette.setBounds(350, 150, 100, 100);
        palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
        desktop.add(palette, JDesktopPane.PALETTE_LAYER);
        palette.setVisible(true);

        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

        frame.add(desktop, BorderLayout.CENTER);
        frame.setSize(500, 300);
        frame.setVisible(true);
      }
    };
    EventQueue.invokeLater(runner);
  }
}

但随着这一变化,MDI窗口的“桌面”变成了黑色。如何将此颜色更改为系统颜色?

EN

回答 3

Stack Overflow用户

发布于 2018-02-21 23:50:08

,但是有了这个改变,MDI窗口的“桌面”变成了黑色。

这是Windows桌面窗格的默认颜色。

有关每个Swing组件的默认值列表,请参见UIManager Defaults

如何将此颜色更改为系统颜色?

代码语言:javascript
复制
desktop.setBackground( Color.RED );

或者从UIManagerDefaults程序中,您可以从组合框中选择“系统颜色”,以获得与系统相关的颜色列表,然后选择使用该属性。

票数 0
EN

Stack Overflow用户

发布于 2018-02-21 23:50:16

我的猜测是,这是一个模糊的答案……是JDesktopPaneComponentUI没有在你的系统LAF中定义BackgroundColor (Windows7中是黑色的),所以默认使用黑色(r=0,g=0,b=0) (或者实际上是选择的颜色)。

现在,对于解决方案,您可以使用JComponent.setBackground(Color)设置所需的颜色。

顺便说一句,您应该将组件添加到框架的内容窗格中,而不是框架本身:

代码语言:javascript
复制
frame.getContentPane().add(desktop, BorderLayout.CENTER);
票数 0
EN

Stack Overflow用户

发布于 2018-02-22 02:06:12

所以我现在的解决方案是:

代码语言:javascript
复制
Color panelBackground = UIManager.getColor ( "Panel.background" );
...
desktop.setBackground( panelBackground );

只是不知道面板背景是否是最适合MDI-background的颜色。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48906362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档