我正在尝试使用 JFrame
中的 GridBagLayout
添加三个面板。当设置默认布局时,添加面板没有任何问题,但是当布局更改为 GridBagLayout
并在 JFrame
中添加面板时,输出是一个空框架.
这是MainFrame
的代码:
public class MainFrame extends JFrame {
CirclePanel[] cp;
MainFrame() throws IOException{
cp = new CirclePanel[3];
for(int i = 0 ;i <3 ; i ++)
cp[i]= new CirclePanel(i+1);
this.setSize(700, 700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());//when this line is not set then
//add(cp[0]) adds the panel
GridBagConstraints gc = new GridBagConstraints();
gc.weightx=1;
gc.weighty=1;
gc.gridx=0;
gc.gridy=0;
add(cp[1],gc);
this.setVisible(true);
}
}
我尝试了不同的步骤,但无法使用 GridBagLayout
添加面板,或者至少无法显示面板。
我想通过增加 gc.gridx
来添加 3 个宽度相等的连续面板。
Main
类仅包含 main
方法,该方法调用 MainFrame
构造函数。
在我的自定义面板类 (CirclePanel
) 中,我使用 paintComponent
方法来绘制圆圈。
public class CirclePanel extends JPanel {
private int mainCirclex ;//= getWidth()/2;
private int mainCircley ;//= getHeight()/2;
private final int mainCircler =1200;
private final double ampRadius = 0.1;
// private MakeArrays ma;
private int[] firstCir;
public CirclePanel(int num) throws IOException{
firstCir = new int[175];
/* ma = new MakeArrays();
if(num == 1)
firstCir = ma.getFirstCir();
else if(num == 2)
firstCir = ma.getSecondCir();
else if(num == 3)
firstCir = ma.getThirdCir();
*/
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.BLACK);
g2d.fillRect(getX(), getY(), getWidth(), getHeight());
//////////main circle////////////////////
//amplifying radius and setting x & y
mainCircley = getHeight()/2;
mainCirclex = getWidth()/2;
int radius = (int) (mainCircler*ampRadius);
//drawing
g2d.setColor(Color.yellow);
g2d.drawOval(mainCirclex - radius, mainCircley - radius, 2 * radius, 2 * radius);
/////////end main circle///////////
//////////////sub circles//////////////
int min = Math.min(mainCirclex, mainCircley);
int r2 = Math.abs(min - radius) / 160;
int j =0;
for (int i = 0; i <360; i++) {
if(j>=175) break;
if(i%2==0){
double t = 2 * Math.PI * i / 360;
int r = (int) (firstCir[j]*ampRadius);
//System.out.println(firstCir[j]);
int x = (int) Math.round(mainCirclex + r * Math.cos(t));
int y = (int) Math.round(mainCircley - r * Math.sin(t));
g2d.setColor(Color.red);
g2d.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);
j++;
}
}
}
}
最佳答案
我通过增加 gridx
值添加了所有 3 个面板。为了使其工作,我做了以下工作:
添加了行
gc.fill = GridBagConstraints.BOTH
以允许面板调整大小并填充额外空间。在面板的构造函数中调用
setBackground
,而不是在paintComponent
中调用fillRect
。省去了您计算面板尺寸的麻烦。覆盖面板的
getPreferredSize
:@Override public Dimension getPreferredSize() { int size = (int) (2 * mainCircler * ampRadius); return new Dimension(size, size); }
(您可能想重做计算。)
在框架上调用
pack
而不是setSize
。
说明
从布局管理器的角度来看,您添加到框架中的面板是空的 - 它们没有任何子组件。这意味着面板将保留其默认首选尺寸,即 10x10 (System.out.println(cp[1].getPreferredSize())
),并且它们将添加到具有这些尺寸的框架中。
计算组件的首选大小时,不会自动考虑自定义绘制。如果首选尺寸对于绘画来说太小,则不会显示该绘画(或仅显示其中的一部分)。重写面板的 getPreferedSize
方法以提供正确的尺寸将告诉框架在调用 pack
时如何显示它们。
编辑:评论答案
When I call
setBackground
to color the panels in their constructor it works fine, but when I try to usefillRect
inpaintComponent
then the 1st panel is colored, but the next 2 panels aren't. Why is that?
因为一个非常常见的误解(这就是我推荐第2点的原因)。
getXXX()
(X
, Y
, 宽度
, 高度
)没有明确声明以下内容,但它们源自 getBounds()
,其内容如下:
Gets the bounds of this component in the form of a
Rectangle
object. The bounds specify this component's width, height, and location relative to its parent.
(强调我的。)所以边界位于父级坐标系中。但是,Graphics.fillRect
使用组件坐标系中的给定边界进行绘制(传递给 paintComponent
的 Graphics
对象)用于绘制组件,而不是其父组件)。
然后为了在面板自己的坐标系中绘制面板,我们需要
g2d.fillRect(0, 0, getWidth(), getHeight());
因为组件始终从其自己的坐标系中的 (0,0)
开始。使用
g2d.fillRect(getX(), getY(), getWidth(), getHeight());
获取面板在父级中的位置并将其传递到面板中的某个非特殊位置。此外,如果面板位于父级的 (300,300)
中,但面板的大小小于 (300,300)
,则面板中的绘图 理论上会在面板之外启动。所以,你看不到任何绘画。
第一个面板进行绘制是因为其 (0,0)
与其父面板的 (0,0)
一致。
关于java - GridBagLayout 没有添加我的面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30042318/