2013-04-03 67 views
1

我從java2s得到了一個JToggleButton的例子。在代碼中,如果文本是「West」,我看到切換按鈕不能正確顯示文本。 West顯示爲We ..其他一切都沒問題。但是當text =「west」,即w很小時沒有問題。這是一個錯誤?JToggleButton API中可能存在的錯誤?

enter image description here

代碼:

import java.awt.BorderLayout; 

import javax.swing.JFrame; 
import javax.swing.JToggleButton; 

public class MainClass { 
public static void main(String args[]) { 
JFrame f = new JFrame("JToggleButton Sample"); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.add(new JToggleButton("North"), BorderLayout.NORTH); 
f.add(new JToggleButton("East"), BorderLayout.EAST); 
f.add(new JToggleButton("West"), BorderLayout.WEST); 
f.add(new JToggleButton("Center"), BorderLayout.CENTER); 
f.add(new JToggleButton("South"), BorderLayout.SOUTH); 
f.setSize(300, 200); 
f.setVisible(true); 
} 
} 
+1

嘗試使用f.pack();而不是f.setSize(...) – MadProgrammer 2013-04-03 21:14:39

+0

@MadProgrammer - 較小的窗口,但錯誤仍然存​​在。它發生在文本= West時。 – SuperStar 2013-04-03 21:16:50

+0

那麼你把所有的按鈕文字都改爲「West」了? – MadProgrammer 2013-04-03 21:17:30

回答

2

MetalLookAndFeel - 的getPreferredSize計算不正確

enter image description here

SystemLookAndFeel

enter image description here

雨雲

enter image description here

物質

enter image description here

從代碼(通過使用相同的BorderLayout的問題與JFrame的,如的JPanel容器)

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JToggleButton; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 

public class MainClass { 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(
          "org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel"); 
        //UIManager.getDefaults(); 
       } catch (Exception e) { 
       } 
      } 
     }); 

     /*try { 
      //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(laf.getName())) { 
        UIManager.setLookAndFeel(laf.getClassName()); 
       } 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }*/ 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame f = new JFrame("JToggleButton Sample"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       JPanel panel = new JPanel(); 
       panel.setLayout(new BorderLayout()); 
       panel.add(new JToggleButton("North"), BorderLayout.NORTH); 
       panel.add(new JToggleButton("East"), BorderLayout.EAST); 
       panel.add(new JToggleButton("West"), BorderLayout.WEST); 
       panel.add(new JToggleButton("Center"), BorderLayout.CENTER); 
       panel.add(new JToggleButton("South"), BorderLayout.SOUTH); 
       f.add(panel); 
       f.pack(); 
       //f.setSize(300, 200); 
       f.setVisible(true); 
      } 
     }); 
    } 
} 
+0

與Win8相同,因爲我看到OPs截圖 – mKorbel 2013-04-03 21:39:34

+0

看起來像是它在MetalLookAndFeel中的錯誤。但是它對於oracle來說可能太小了。不要在代碼中使用任何以w開頭的單詞。 – SuperStar 2013-04-03 21:41:47

+0

@SuperStar然後報告,[你會成爲官方BugParade中的自己的錯誤的真正的超級明星], – mKorbel 2013-04-03 21:44:20

2

似乎只是正常工作對我來說...

enter image description hereenter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JToggleButton; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestLayout23 { 

    public static void main(String[] args) { 
     new TestLayout23(); 
    } 

    public TestLayout23() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JFrame f = new JFrame("JToggleButton Sample"); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.add(new JToggleButton("North"), BorderLayout.NORTH); 
       f.add(new JToggleButton("East"), BorderLayout.EAST); 
       f.add(new JToggleButton("West"), BorderLayout.WEST); 
       f.add(new JToggleButton("Center"), BorderLayout.CENTER); 
       f.add(new JToggleButton("South"), BorderLayout.SOUTH); 
       f.pack(); 
       f.setLocationRelativeTo(null); 
       f.setVisible(true); 
      } 

     }); 
    } 

} 
+0

看起來像Mac OS UI。我在贏得DODO-ws。嘗試刪除設置的外觀和感覺代碼。 – SuperStar 2013-04-03 21:26:32

+0

在Win7上不會發出MetalLookAndFeel Java6/7 – mKorbel 2013-04-03 21:28:55

+0

@mKorbel - 不知道我是否理解你。你的意思是 - 不是問題。它只發生在Win7 Java6/7上的MetalLookAndFeel中。對 ? – SuperStar 2013-04-03 21:30:31