SetVisibleの問題(TRUE)
-
18-09-2019 - |
質問
以下に示す2つの例は同じです。どちらも同じ結果を生成することになっています。たとえば、JPanelに表示される画像の座標を生成します。例1は、完全に機能します(画像の座標を印刷します)が、例2は座標の0を返します。
両方の例で、パネルを追加した後、SetVisible(True)を置いたのはなぜだろうと思っていました。唯一の違いは、使用された例1です extends JPanel
および例2 extends 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();
}
}
解決
問題は、2番目の例では、コンポーネントがコンテナに追加される前にコンポーネントの境界を印刷しようとしていることです(呼び出すことにより add()
)そして、フレームの内容がレイアウトされる前に(呼び出すことによって pack()
).
これが例1を再現しようとする私の試みです。
これが例2を再現しようとする私の試みです。 SwingUtilities
正しいスレッドに物を置くために電話をかけると、私は GetCoordiates
あなたのコメントの助けを借りたコンストラクター:
class GetCoordinate extends JLabel {
public GetCoordinate() {
setText("Foo!");
System.out.println("Coordinate: " + this.getBounds());
}
}
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
final 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) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Grid();
}
});
}
}
あなたが説明したように、それはゼロのサイズを印刷します:
座標:java.awt.rectangle [x = 0、y = 0、width = 0、height = 0]
ただし、コンポーネントが追加されてフレームが詰め込まれた後にサイズを印刷すると、機能するはずです。これが私の例2の変更されたバージョンです。ここではメソッドを追加しました GetCoordinate.printBounds()
そして、その方法を呼び出して、すべてが追加され、レイアウトされています。
class GetCoordinate extends JLabel {
public GetCoordinate() {
setText("Foo!");
// Let's not try to do this here anymore...
// System.out.println("Coordinate: " + this.getBounds());
}
public void printBounds() // <-- Added this method
{
System.out.println("Coordinate: " + this.getBounds());
}
}
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
final JPanel pDraw = new JPanel();
m.setConstraints(pDraw, con);
final GetCoordinate content = new GetCoordinate();
pDraw.add(content);
c.add(pDraw);
pack();
setVisible(true);
content.printBounds(); // <-- Added this
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Grid();
}
});
}
}
これらの変更を使用して、コンテンツのゼロ以外のサイズを含む次のコンソール出力を取得します。
座標:java.awt.rectangle [x = 5、y = 5、width = 23、height = 16
他のヒント
そのような異常の一般的な原因は、 EDT. 。この場合、私はあなたのコードから何が違うかを知ることはできません。特に、2番目の例がどこに印刷されているかは明らかではありません。
Contoh
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author LENOVO G40
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new FrmMenuUTama().setVisible(true);
}
}