Changing DesktopIcon.width on nimbus

独自空忆成欢 提交于 2019-12-06 14:53:07

You might be able to override the getBoundsForIconOf(JInternalFrame) method of DefaultDesktopManager:

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

public class DesktopIconWidthTest {
  public JComponent makeUI() {
    JDesktopPane desktop = new JDesktopPane();
    desktop.setDesktopManager(new DefaultDesktopManager() {
      @Override protected Rectangle getBoundsForIconOf(JInternalFrame f) {
        Rectangle r = super.getBoundsForIconOf(f);
        //System.out.println(r);
        r.width = 300;
        return r;
      }
    });
    desktop.add(createFrame(0));
    desktop.add(createFrame(1));
    return desktop;
  }
  private JInternalFrame createFrame(int i) {
    JInternalFrame f = new JInternalFrame("title" + i, true, true, true, true);
    //TEST:
    //f.setDesktopIcon(new JInternalFrame.JDesktopIcon(f) {
    //  @Override public Dimension getPreferredSize() {
    //    Dimension d = super.getPreferredSize();
    //    d.width = 300;
    //    return d;
    //  }
    //  @Override public Rectangle getBounds() {
    //    Rectangle r = super.getBounds();
    //    r.width = 300;
    //    return r;
    //  }
    //});
    f.setSize(300, 200);
    f.setVisible(true);
    f.setLocation(10 + 60 * i, 10 + 40 * i);
    return f;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      try {
        for (UIManager.LookAndFeelInfo laf: UIManager.getInstalledLookAndFeels()) {
          if ("Nimbus".equals(laf.getName())) {
            UIManager.setLookAndFeel(laf.getClassName());
            break;
          }
        }
        //UIManager.put("DesktopIcon.width", 500);
      } catch (Exception e) {
        e.printStackTrace();
      }
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new DesktopIconWidthTest().makeUI());
      f.setSize(640, 480);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

  • Another option is to use a JInternalFrame.JDesktopIcon:
    • change the Font of the minimized JInternalFrame
import java.awt.*;
import javax.swing.*;
//import javax.swing.plaf.basic.*;
import javax.swing.plaf.synth.*;

public class DesktopIconWidthTest2 {
  public JComponent makeUI() {
    JDesktopPane desktop = new JDesktopPane();
    desktop.add(createFrame(0));
    desktop.add(createFrame(1));
    desktop.add(createFrame(2));
    return desktop;
  }
  private JInternalFrame createFrame(int i) {
    JInternalFrame f = new JInternalFrame("title: " + i, true, true, true, true);
    f.setDesktopIcon(new JInternalFrame.JDesktopIcon(f) {
      @Override public Dimension getPreferredSize() {
        return new Dimension(300, 40);
      }
      @Override public void updateUI() {
        super.updateUI();
        if (getUI() instanceof SynthDesktopIconUI) {
          //NimbusLookAndFeel:
          setUI(new SynthDesktopIconUI() {
            @Override
            protected void installComponents() {
              super.installComponents();
              iconPane.setFont(getFont());
            }
          });
        }
      }
      //BasicLookAndFeel work fine
      @Override public Font getFont() {
        Font font = UIManager.getFont("InternalFrame.titleFont");
        return font.deriveFont(30f);
      }
    });

    //TEST:
    //Container c = ((BasicInternalFrameUI) f.getUI()).getNorthPane();
    //c.setFont(new Font("Monospaced", Font.BOLD, 50));

    f.setSize(300, 200);
    f.setVisible(true);
    f.setLocation(10 + 60 * i, 10 + 40 * i);
    return f;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      try {
        for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
          if ("Nimbus".equals(laf.getName())) {
            UIManager.setLookAndFeel(laf.getClassName());
            break;
          }
        }
        //TEST:
        //UIManager.put("DesktopIcon.width", 500);
        //UIManager.put("InternalFrame.useTaskBar", Boolean.TRUE);
        //Font font = UIManager.getFont("InternalFrame.titleFont");
        //UIManager.put("InternalFrame.titleFont", font.deriveFont(60f));
      } catch (Exception e) {
        e.printStackTrace();
      }
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new DesktopIconWidthTest2().makeUI());
      f.setSize(640, 480);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!