Removing the three dots “…” from a JButton?

后端 未结 5 1882
伪装坚强ぢ
伪装坚强ぢ 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 01:58

    Don't set the preferred size of the button. Use the preferred size of the button and let the layout manager layout the components. The preferred size ensures all the text will be displayed properly on different Look and Feels.

    0 讨论(0)
  • 2020-12-16 02:00

    You could change the size of the font on the button. See these links:

    Setting font size

    Increasing font size (Can be easily changed to decrease font size)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-16 02:05

    Probably this because margin of your button is too big.

    Try this:

    myButton.setMargin(new Insets(0, 0, 0, 0));
    

    You can also turn off border:

    button.setBorder(null);
    
    0 讨论(0)
  • 2020-12-16 02:13

    Use setMargin(Insets m) to adjust the space between the JButton border and the label. The default is (2, 14, 2, 14). To maximize the space available for the label (and to remove the dots completely) you can use something like

    myButton.setFont(new Font("Tahoma", Font.BOLD, 11));
    myButton.setMargin(new Insets(0, -1, 0, -20));
    myButton.setHorizontalAlignment(SwingConstants.LEFT);
    
    0 讨论(0)
提交回复
热议问题