java – 如何从添加到JLabel的JComponent返回XxxSize

前端之家收集整理的这篇文章主要介绍了java – 如何从添加到JLabel的JComponent返回XxxSize前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何正确地从添加到JLabel的JComponent返回XxxSize

1.图>>让LayoutManager像JPanel一样工作,JLabel返回Size(0,0)

第2位.图>>将一些PreferredSize添加到JLabel

3.图>>从添加到JLabel的JComponent计算的PreferredSize

4.图>>让LayoutManager将JLabel改为JPanel,现在LayoutManager正确计算了Dimension而不使用任何XxxSize

注意到使用了Nimbus L& F,所有可访问的L& F都有相同的输出

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5. import javax.swing.*;
  6.  
  7. public class NimbusBorderPainterDemo extends JFrame {
  8.  
  9. private static final long serialVersionUID = 1L;
  10. private JFrame frame = new JFrame();
  11. private JPanel fatherPanel = new JPanel(),titlePanel = new JPanel();
  12. private JLabel buttonPanel = new JLabel();
  13.  
  14.  
  15. //figure ---> 4th. switch JLabel with JPanel
  16. //private JPanel buttonPanel = new JPanel();
  17. private Queue<Icon> iconQueue = new LinkedList<Icon>();
  18.  
  19. public NimbusBorderPainterDemo() {
  20. iconQueue.add(UIManager.getIcon("OptionPane.errorIcon"));
  21. iconQueue.add(UIManager.getIcon("OptionPane.informationIcon"));
  22. iconQueue.add(UIManager.getIcon("OptionPane.warningIcon"));
  23. JButton button0 = createButton();
  24. JButton button1 = createButton();
  25. JButton button2 = createButton();
  26. button2.addActionListener(new ActionListener() {
  27.  
  28. public void actionPerformed(ActionEvent e) {
  29. System.exit(1);
  30. }
  31. });
  32. int gap = 5;
  33. buttonPanel.setLayout(new GridLayout(0,3,gap,0));
  34. buttonPanel.add(button0);
  35. buttonPanel.add(button1);
  36. buttonPanel.add(button2);
  37.  
  38. // figure 1st. ---> without PreferredSize
  39.  
  40. // figure 2nd. --->
  41. //buttonPanel.setPreferredSize(new Dimension(160,30));
  42.  
  43. // figure 3rd. --->
  44. /*Dimension dim = button0.getPreferredSize();
  45. int w = dim.width;
  46. int h = dim.height;
  47. w = (w + 5) * 3;
  48. h += 4;
  49. dim = new Dimension(w,h);
  50. buttonPanel.setPreferredSize(dim);*/
  51.  
  52. titlePanel.setLayout(new BorderLayout());
  53. titlePanel.add(new JLabel(nextIcon()),BorderLayout.WEST);
  54. titlePanel.add(new JLabel("My Frame"),BorderLayout.CENTER);
  55. titlePanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
  56. titlePanel.add(buttonPanel,BorderLayout.EAST);
  57. fatherPanel.setLayout(new BorderLayout());
  58. fatherPanel.add(titlePanel,BorderLayout.CENTER);
  59. frame.setUndecorated(true);
  60. frame.add(fatherPanel);
  61. frame.setLocation(50,50);
  62. frame.pack();
  63. frame.setDefaultCloSEOperation(JFrame.DO_NOTHING_ON_CLOSE);
  64. frame.setVisible(true);
  65. }
  66.  
  67. private JButton createButton() {
  68. JButton button = new JButton();
  69. button.setBorderPainted(false);
  70. button.setBorder(null);
  71. button.setFocusable(false);
  72. button.setMargin(new Insets(0,0));
  73. button.setContentAreaFilled(false);
  74. button.setIcon(nextIcon());
  75. //button.setRolloverIcon(nextIcon());
  76. //button.setPressedIcon(nextIcon());
  77. //button.setDisabledIcon(nextIcon());
  78. nextIcon();
  79. return button;
  80. }
  81.  
  82. private Icon nextIcon() {
  83. Icon icon = iconQueue.peek();
  84. iconQueue.add(iconQueue.remove());
  85. return icon;
  86. }
  87.  
  88. public static void main(String[] args) {
  89. SwingUtilities.invokeLater(new Runnable() {
  90.  
  91. @Override
  92. public void run() {
  93. try {
  94. UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  95. } catch (Exception fail) {
  96. }
  97. UIManager.getLookAndFeelDefaults().put("nimbusFocus",Color.RED);
  98. NimbusBorderPainterDemo nimbusBorderPainterDemo = new NimbusBorderPainterDemo();
  99. }
  100. });
  101. }
  102. }

解决方法

默认的首选大小计算是使用布局管理器来确定组件的首选大小.这意味着布局管理器遍历所有子组件以确定每个子组件的首选大小.对于要用作Container的JPanel,使用此计算.

但是,对于其他Swing组件,始终会覆盖getPreferredSize()方法,以便为给定组件提供合理的大小.

对于JLabel,首选大小计算会考虑使用的文本和图标.由于您没有提供任何首选大小为零.当然,如果您使用setPreferredSize()方法手动覆盖此计算,则组件将具有首选大小.

因此,即使Swing允许您向任何组件添加组件并使用布局管理器来布局子组件,这些子组件也不会在首选大小计算中使用.

这不仅仅是一个Nimbus问题.

猜你在找的Java相关文章