何时是GUI组件的位置,宽度和高度设置?如果布局经理正在使用,这些领域是如何实现的?我有一个包含两个滑块的面板,我想在其中一个旁边画一个矩形。这里是我的代码初始化我的GUI组件和一个矩形画他们旁边的滑块之一:JSlider getX(),getY()JApplet
public PhotoSliders()
{
initComponents(); //from the netbeans GUI designer
}
public void setColorRect() //initialize a colored rectangle
{
colorRect = new ColorRect(Color.RED, (double)(emSlider.getX())/getWidth(), (double)(emSlider.getY())/getHeight());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
colorRect.paintColorRect(g, getWidth(), getHeight());
}
我发现,emSlider.getX(),emSlider.getY()的getWidth()和getHeight()最初都是0。当我调整我的小程序的大小,并因此导致它重新绘制时,字段不再为零。为什么他们最初都是零?这里是我的applet类代码:
public void init() //initialize the Applet Class
{
setLayout(new BorderLayout());
slidersPanel = new PhotoSliders();
JPanel north = new JPanel(new BorderLayout());
north.add(slidersPanel, BorderLayout.WEST);
add(north, BorderLayout.NORTH);
add(photoEffect, BorderLayout.CENTER);
slidersPanel.setColorRect();
}
public void paint(Graphics g) //paint the photoEffect
{
super.paint(g);
slidersPanel.setColorRect();
}