Question

I'm a second year student and I'm working on my OOP project (Calculator). I'm done with the functions of the numeric buttons and operators. Now I'm in the stage of re-arranging my buttons. At first, I just set my button size to (50,50), it works fine and its label is still visible, but when I decided to make it smaller (30, 30), its label turned into "..." instead.

Here's the pic:

GUI btn shows ... instead of MC, MR, MS, and M+

And heres my code:

  lblEdit.setBounds(-138,-5,180,50);    
  lblView.setBounds(-90,-5,180,50); 
  lblHelp.setBounds(-40,-5,180,50); 
  txt.setBounds(15,35,250,30);      // text pane
  txt2.setBounds(0,330,100,20); 
  blank.setBounds(15,80,30,30);     // this is just an extra button, no use at all, OK? :D 
  btnMC.setBounds(15,115,30,30);
  btnMR.setBounds(15,150,30,30);
  btnMS.setBounds(15,185,30,30);
  btnMp.setBounds(15,220,30,30);
Was it helpful?

Solution

Your problem is that you set the button sizes to begin with. If you instead leave the JButtons and the GUI to size itself using proper layout managers and calling pack() on the JFrame, you will get a decent looking GUI that shows all the text in any OS. Solution: don't use null layouts, don't call setBounds(...), read up on and use appropriate layout managers held in nested JPanels, and let these layout managers do all the heavy layout lifting for you.

For example, you could create a grid of buttons using a GridLayout, and alter the size of the grid and the buttons by simply changing the font size of the button. For example run the code below twice, changing the button font size (in the code below, the float constant BTN_FONT_SIZE) and seeing how the GUI automatically accommodates the button font by resizing the button to its optimal size.

import java.awt.GridLayout;
import javax.swing.*;

public class CalcEg {
   private static final float BTN_FONT_SIZE = 20f;  // **** try using 40f here ****
   private static final String[][] BTN_LABELS = {
      {"7", "8", "9", "-"},
      {"4", "5", "6", "+"},      
      {"1", "2", "3", "/"},
      {"0", ".", " ", "="}
   };
   private JPanel mainPanel = new JPanel();

   public CalcEg() {
      int rows = BTN_LABELS.length;
      int cols = BTN_LABELS[0].length;
      int gap = 4;
      mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
      mainPanel.setLayout(new GridLayout(rows, cols, gap, gap));
      for (String[] btnLabelRow : BTN_LABELS) {
         for (String btnLabel : btnLabelRow) {
            JButton btn = createButton(btnLabel);
            // add ActionListener to btn here
            mainPanel.add(btn);
         }
      }
   }

   private JButton createButton(String btnLabel) {
      JButton button = new JButton(btnLabel);
      button.setFont(button.getFont().deriveFont(BTN_FONT_SIZE));
      return button;
   }

   public JComponent getMainComponent() {
      return mainPanel;
   }

   private static void createAndShowGui() {
      CalcEg mainPanel = new CalcEg();

      JFrame frame = new JFrame("CalcEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel.getMainComponent());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

If you nest the button JPanel into a BorderLayout using JPanel and add a JTextField to its PAGE_START or the NORTH end, and you play with different font sizes, you'll see something like this:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top