0

I tried to use setSize() but didn't work. then I also used setPreferredSize(new Dimension(x,y)). Yes it does work but it changes the size of all the Components in that (oPanel) Panel. I just want to change the size of btnPlus.

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

public class MyCalc {

public static void main(String[] args) {

    JFrame main= new JFrame();
    JPanel bPanel= new JPanel();
    JPanel oPanel= new JPanel();
    JPanel Txt= new JPanel();
    JPanel Panel=new JPanel();
    JButton btn0= new JButton("0");
    JButton btn1= new JButton("1");
    JButton btn2= new JButton("2");
    JButton btn3= new JButton("3");
    JButton btn4= new JButton("4");
    JButton btn5= new JButton("5");
    JButton btn6= new JButton("6");
    JButton btn7= new JButton("7");
    JButton btn8= new JButton("8");
    JButton btn9= new JButton("9");
    JButton btnDot= new JButton(".");
    JButton btnPlus= new JButton("+");
    JButton btnMinus= new JButton("-");
    JButton btnEquals= new JButton("=");
    //JTextField tf= new JTextField();
    JTextArea ta= new JTextArea();

    ta.setColumns(20);
    ta.setRows(3);
    //ta.setViewPortView()

    main.setLayout(new BorderLayout(20,20));

    Txt.setLayout(new BorderLayout());
    //Txt.add(tf,BorderLayout.NORTH);
    Txt.add(ta,BorderLayout.SOUTH);

    bPanel.setLayout(new GridLayout(4,3,5,5));
    bPanel.add(btn0);
    bPanel.add(btn1);
    bPanel.add(btn2);
    bPanel.add(btn3);
    bPanel.add(btn4);
    bPanel.add(btn5);
    bPanel.add(btn6);
    bPanel.add(btn7);
    bPanel.add(btn8);
    bPanel.add(btn9);
    bPanel.add(btnDot);

    oPanel.setLayout(new GridLayout(3,1,5,5));
    //oPanel.setMaximumSize(new Dimension(400,400));
    oPanel.add(btnPlus);
    oPanel.add(btnMinus);
    oPanel.add(btnEquals);

    btnPlus.setPreferredSize(new Dimension(50,100));
    btnMinus.setPreferredSize(new Dimension(25,25));
    btnPlus.setPreferredSize(new Dimension(50,100));

    Panel.setLayout(new FlowLayout());
    Panel.add(bPanel);
    Panel.add(oPanel);

    //main.getContentPane().add(bPanel,BorderLayout.LINE_START);
    //main.getContentPane().add(oPanel, BorderLayout.LINE_END);
    main.getContentPane().add(Panel,BorderLayout.CENTER);
    main.getContentPane().add(Txt, BorderLayout.PAGE_START);

    main.setSize(300, 300);
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setVisible(true);
}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"How can I set the dimension of some particular JButton component (in this case btnPlus)?"* It is better not to. Instead give it an icon, a font size and a margin to suggest a size. – Andrew Thompson Aug 25 '15 at 08:00

1 Answers1

3

I think your main problem is in the use of GridLayout, which gives equal space to all the components in the container based on the configuration you give it and the size of the container.

Consider instead, using a GridBagLayout, which will give you much more control.

See How to Use GridLayout and How to Use GridBagLayout for more details.

You should also take a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? and yes, you should be avoiding it

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366