Changing DesktopIcon.width on nimbus

2019-06-04 03:40发布

I'm trying to change the JInternalFrame iconifying size, I've tried changing the L&F defaults with javax.swing.UIManager.put("DesktopIcon.width", 300); but it doesn't work on Nimbus, I've also tried changing the DesktopIconUI with

javax.swing.UIManager.put("DesktopIconUI",  javax.swing.plaf.basic.BasicDesktopIconUI.class.getName());

but as soon as I minimize the JInternalFrame it dissapears, any sugestions?

1条回答
Root(大扎)
2楼-- · 2019-06-04 04:36

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);
    });
  }
}
查看更多
登录 后发表回答