1

我从java2s获得了 JToggleButton 的示例。在代码中,如果文本为“West”,我看到切换按钮无法正确显示文本。西部显示为我们... 其他一切都很好。但是text="west"的时候没有问题,即w小。这是一个错误吗?

在此处输入图像描述

代码:

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);
 }
}
4

2 回答 2

2

似乎对我来说工作得很好......

在此处输入图像描述在此处输入图像描述

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);
            }

        });
    }

}
于 2013-04-03T21:24:14.303 回答
2

MetalLookAndFeel - 未正确计算 getPreferredSize

在此处输入图像描述

系统观感

在此处输入图像描述

雨云

在此处输入图像描述

物质

在此处输入图像描述

来自代码(使用 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);
            }
        });
    }
}
于 2013-04-03T21:36:53.507 回答