此红色框应仅为屏幕高度的1/6。我根本无法让红色框的高度减少。如何修复所有这些高度? http://i.imgur.com/2lN2HxK.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);
}
}
由于
Ehem,忽略这一点,它只是让我提供更多关于代码的上下文,但问题是直截了当的......所以,如果你喜欢lol那个包名,那就是我做过的第一个包的名字。班上的一位朋友看到了它,笑死了,所以从那时起就一直如此。答案 0 :(得分:2)
您的JFrame
应使用布局来规定相关面板的大小。然后,您可以通过调整GridBagLayout
:columnWidths
,rowHeights
,columnWeights
和rowWeights
的公共字段来调整行和列的权重。< / p>
例如:
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();
}
}