This red box should only be 1/6 of the screen height. I can't get the height of the red box to decrease at all. How do I fix the height on all of these? https://i.sstatic.net/pTGxd.png
package urAPackage;
import urAPackage.GUIcompos;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Sweg extends GUIcompos {
public void setGUI(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
f = new JFrame("Wow sweg");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Da calculatorrrrr");
image = new ImageIcon(f.getClass().getResource("/UrAPackage/sweg.png")).getImage();
f.setIconImage(image);
f.setContentPane(new JLabel(new ImageIcon(f.getClass().getResource("/UrAPackage/bg.jpg"))));
f.setSize(399, 599);
f.setSize(400, 600);
f.setResizable(false);
f.setLayout(new BorderLayout());
f.setLocationRelativeTo(null);
pMain = new JPanel();
pMain.setLayout(new GridBagLayout());
gbc = new GridBagConstraints();
pMain.setPreferredSize(new Dimension(400,600));
pMain.setOpaque(false);
f.add(pMain, BorderLayout.CENTER);
pCent = new JPanel();
pCent.setLayout(pCentMGR = new GridLayout(4, 3, 10, 5));
pCent.setOpaque(false);
pCent.add(cBtn7 = new JButton("7"));
pCent.add(cBtn8 = new JButton("8"));
pCent.add(cBtn9 = new JButton("9"));
pCent.add(cBtn4 = new JButton("4"));
pCent.add(cBtn5 = new JButton("5"));
pCent.add(cBtn6 = new JButton("6"));
pCent.add(cBtn1 = new JButton("1"));
pCent.add(cBtn2 = new JButton("2"));
pCent.add(cBtn3 = new JButton("3"));
pCent.add(cBtn0 = new JButton("0"));
pCent.add(cBtnP = new JButton("."));
pCent.add(cBtnN = new JButton("(-)"));
pTop = new JPanel();
pTop.setBackground(new Color(255, 0, 0));
pLeft = new JPanel();
pLeft.setBackground(Color.blue);
pRight = new JPanel();
pRight.setBackground(Color.yellow);
pOutput = new JPanel();
pOutput.setBackground(Color.green);
//P MAIN GRID SET UP
gbc.weightx = gbc.weighty = 1.0;
gbc.gridx = 0;
gbc.gridy= 0;
gbc.gridheight = 1;
gbc.gridwidth = 6;
gbc.fill = GridBagConstraints.BOTH;
pMain.add(pTop, gbc);
gbc.gridx = 0;
gbc.gridy= 1;
gbc.gridheight = 5;
gbc.gridwidth = 1;
pMain.add(pLeft, gbc);
gbc.gridx = 1;
gbc.gridwidth = 4;
gbc.insets = new Insets(6,6,6,6);
pMain.add(pCent, gbc);
gbc.gridx = 5;
gbc.gridwidth = 1;
gbc.insets = new Insets(0,0,0,0);
pMain.add(pRight, gbc);
gbc.gridx = 0;
gbc.gridy= 6;
gbc.gridheight = 1;
gbc.gridwidth = 6;
pMain.add(pOutput, gbc);
f.setVisible(true);
}
}
Thanks
Ehem, ignore this it just asked me to give more context about the code but the question is straight forwards... So if you're like lol that package name, that was the name of the first package I ever made. A friend in the class saw it and died laughing so it's been that way ever since.
Your JFrame
should use a layout to prescribe the size of the associated panels. You can then adjust the weights of the rows and columns by adjusting the public fields of the GridBagLayout
: columnWidths
, rowHeights
, columnWeights
, and rowWeights
.
For example:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class GridBagLayoutExample extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayoutExample frame = new GridBagLayoutExample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GridBagLayoutExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(450, 450));
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0 };
gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0 };
gbl_contentPane.columnWeights = new double[] { 1.0, 6.0, 1.0, Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 1.0, 6.0, 1.0, Double.MIN_VALUE };
contentPane.setLayout(gbl_contentPane);
JPanel top = new JPanel();
top.setBackground(Color.RED);
GridBagConstraints gbc_top = new GridBagConstraints();
gbc_top.gridwidth = 3;
gbc_top.insets = new Insets(0, 0, 5, 5);
gbc_top.fill = GridBagConstraints.BOTH;
gbc_top.gridx = 0;
gbc_top.gridy = 0;
contentPane.add(top, gbc_top);
JPanel left = new JPanel();
left.setBackground(Color.BLUE);
GridBagConstraints gbc_left = new GridBagConstraints();
gbc_left.insets = new Insets(0, 0, 5, 5);
gbc_left.fill = GridBagConstraints.BOTH;
gbc_left.gridx = 0;
gbc_left.gridy = 1;
contentPane.add(left, gbc_left);
JPanel middle = new JPanel();
middle.setBackground(Color.BLACK);
GridBagConstraints gbc_middle = new GridBagConstraints();
gbc_middle.insets = new Insets(0, 0, 5, 5);
gbc_middle.fill = GridBagConstraints.BOTH;
gbc_middle.gridx = 1;
gbc_middle.gridy = 1;
contentPane.add(middle, gbc_middle);
JPanel right = new JPanel();
right.setBackground(Color.YELLOW);
GridBagConstraints gbc_right = new GridBagConstraints();
gbc_right.insets = new Insets(0, 0, 5, 0);
gbc_right.fill = GridBagConstraints.BOTH;
gbc_right.gridx = 2;
gbc_right.gridy = 1;
contentPane.add(right, gbc_right);
JPanel bottom = new JPanel();
bottom.setBackground(Color.GREEN);
GridBagConstraints gbc_bottom = new GridBagConstraints();
gbc_bottom.insets = new Insets(0, 0, 0, 5);
gbc_bottom.gridwidth = 3;
gbc_bottom.fill = GridBagConstraints.BOTH;
gbc_bottom.gridx = 0;
gbc_bottom.gridy = 2;
contentPane.add(bottom, gbc_bottom);
pack();
}
}