Removing the three dots “…” from a JButton?

后端 未结 5 1890
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 01:44

Hey, I am creating a calculator program, with some small buttons, I want one of the buttons to have \"Ans\" on them, but whenever I make the JButton smaller then 50, 50, it

5条回答
  •  一生所求
    2020-12-16 02:04

    This code attempts to explain why layouts and preferred sizes are so important. The important part lies in the input/output.

    TestGuiSize.java

    import java.awt.*;
    import javax.swing.*;
    
    class TestGuiSize {
    
        public static void addButtonToPanel(JPanel panel, String label) {
            JButton button = new JButton(label);
            button.setMargin(new Insets(1,1,1,1));
            panel.add(button);
        }
    
        public static void main(String[] args) {
            JPanel p = new JPanel(new GridLayout(4,3,3,3));
            addButtonToPanel(p, "7");
            addButtonToPanel(p, "8");
            addButtonToPanel(p, "9");
            addButtonToPanel(p, "/");
    
            addButtonToPanel(p, "4");
            addButtonToPanel(p, "5");
            addButtonToPanel(p, "6");
            addButtonToPanel(p, "*");
    
            addButtonToPanel(p, "1");
            addButtonToPanel(p, "2");
            addButtonToPanel(p, "3");
            addButtonToPanel(p, "-");
    
            addButtonToPanel(p, "0");
            p.add(new JLabel(""));
            addButtonToPanel(p, "Del");
            addButtonToPanel(p, "+");
    
            Dimension d = p.getPreferredSize();
            System.out.println(
                "Preferred Size: " +
                d.getWidth() +
                "x" +
                d.getHeight());
    
            JOptionPane.showMessageDialog(null, p);
        }
    }
    

    Input/output

    prompt> java TestGuiSize
    Preferred Size: 113.0x105.0
    
    prompt>java -Dswing.plaf.metal.controlFont=Dialog-22 TestGuiSize
    Preferred Size: 169.0x157.0
    
    prompt>java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel TestGuiSize
    Preferred Size: 93.0x93.0
    
    prompt>java -Dswing.defaultlaf=com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel TestGuiSize
    Preferred Size: 205.0x129.0
    
    prompt> 
    

    Run-time parameters are just the tip of the iceberg of the differences between runs that might sink an application's GUI code. Layouts are designed to handle such differences.

提交回复
热议问题