setVisible (true) 的问题
下面显示的两个示例是相同的。两者应该产生相同的结果,例如生成在 JPanel 上显示的图像的坐标。 示例 1 工作正常(打印图像的坐标),但是示例 2 返回 0 作为坐标。
我想知道为什么,因为在两个示例中,我在添加面板后都设置了 setvisible (true)。唯一的区别是示例 1 使用扩展 JPanel
,示例 2 扩展 JFrame
示例 1:
public class Grid extends JPanel{
public static void main(String[] args){
JFrame jf=new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Grid grid = new Grid();
jf.add(grid);
jf.pack();
Component[] components = grid.getComponents();
for (Component component : components) {
System.out.println("Coordinate: "+ component.getBounds());
}
jf.setVisible(true);
}
}
示例 2:
public class Grid extends JFrame {
public Grid () {
setLayout(new GridBagLayout());
GridBagLayout m = new GridBagLayout();
Container c = getContentPane();
c.setLayout (m);
GridBagConstraints con = new GridBagConstraints();
//construct the JPanel
pDraw = new JPanel();
...
m.setConstraints(pDraw, con);
pDraw.add (new GetCoordinate ()); // call new class to generate the coordinate
c.add(pDraw);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Grid();
}
}
The two examples shown below are same. Both are supposed to produce same result e.g. generate the coordinates of images displayed on JPanel.
Example 1, works perfectly (print the coordinates of images), however example 2 returning 0 for the coordinate.
I was wondering why because, I have put the setvisible (true) after adding the panel, in both examples. The only difference is that example 1 used extends JPanel
and example 2 extends JFrame
EXAMPLE 1:
public class Grid extends JPanel{
public static void main(String[] args){
JFrame jf=new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Grid grid = new Grid();
jf.add(grid);
jf.pack();
Component[] components = grid.getComponents();
for (Component component : components) {
System.out.println("Coordinate: "+ component.getBounds());
}
jf.setVisible(true);
}
}
EXAMPLE 2:
public class Grid extends JFrame {
public Grid () {
setLayout(new GridBagLayout());
GridBagLayout m = new GridBagLayout();
Container c = getContentPane();
c.setLayout (m);
GridBagConstraints con = new GridBagConstraints();
//construct the JPanel
pDraw = new JPanel();
...
m.setConstraints(pDraw, con);
pDraw.add (new GetCoordinate ()); // call new class to generate the coordinate
c.add(pDraw);
pack();
setVisible(true);
}
public static void main(String[] args) {
new Grid();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是,在第二个示例中,您试图在将组件添加到其容器(通过调用
add()
)之前以及在框架的内容被添加之前打印出组件的边界。布局(通过调用pack()
)。这是我重现示例 1 的尝试。...这是我重现示例 2 的尝试。我添加了
SwingUtilities
调用以将内容放入正确的线程中,然后我在您的评论的帮助下填写了GetCoordiates
构造函数的内容:正如您所描述的,它打印出大小为零:
但是,如果在添加组件且框架已打包后打印出尺寸,则它应该可以工作。这是示例 2 的修改版本,其中我添加了一个方法
GetCooperative.printBounds()
并调用该方法,所有内容都已添加并布局:通过这些更改,我得到以下控制台输出,包括我的内容的非零大小:
The problem is that in the second example, you are trying to print out the bounds of a component before the component has been added to its container (by calling
add()
) and before the frame's contents have been laid out (by callingpack()
).Here is my attempt to reproduce Example 1. ...Here is my attempt to reproduce Example 2. I added the
SwingUtilities
call to put things in the right thread, and I filled in the contents of theGetCoordiates
constructor with help from your comments:Just as you described, it prints out a size of zero:
However, if you print out the size after the component has been added and the frame has been packed, it should work. Here is a modified version of my Example 2, where I added a method
GetCoordinate.printBounds()
and call that method everything has been added and laid out:With these changes, I get the following console output, including a nonzero size for my content:
此类异常的常见原因是无法在 EDT< 上启动/a>.在这种情况下,我无法从您的代码中看出有什么不同:特别是,不清楚第二个示例的打印位置。
A common cause of such anomalies is failing to start on the EDT. In this case, I can't tell from your code what is different: in particular, it's not clear where the second example prints.
康托
contoh