デカルトを生成するのGridBagLayoutから(x、y)を調整する方法?
-
24-09-2019 - |
質問
以前の記事に関連した
私の質問 どのようにグリッドボックスに色を埋めるために、ランダムにする
はどのようにデカルトのGridBagLayoutの色によって充填ボックスのそれぞれの座標(x、y)を取得することができます。パネルの大きさは×300 300で、行と列をx 5に設定されている場合、我々は座標だけでなく、列と行を見ることで知ることができます方法はありますか?
http://www.freeimagehosting.net/image.php?a6ec309bd0.jpg の
EDITED:完全なコード
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Grid extends JPanel{
public static void main(String[] args){
JFrame jf=new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new Grid());
jf.pack();
jf.setVisible(true);
}
public Grid (){
setPreferredSize(new Dimension(300,300));
setBackground(Color.BLACK);
final Color BACKGROUND = Color.WHITE;
final Color[] colors = new Color[]
{Color.BLACK, Color.CYAN, Color.MAGENTA, Color.YELLOW,
Color.GREEN, Color.RED, Color.ORANGE, Color.BLUE, Color.PINK, Color.LIGHT_GRAY};
final int ROWS = 5;
final int COLS = 5;
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.weightx = 1d;
gc.weighty = 1d;
gc.insets = new Insets(0, 0, 1, 1);
gc.fill = GridBagConstraints.BOTH;
// fill the whole panel with labels
Label[][] label = new Label[ROWS][COLS];
for( int r=0 ; r<ROWS ; r++) {
for( int c=0 ; c<COLS ; c++) {
Label lbl = new Label();
lbl.setBackground(BACKGROUND);
gc.gridx = r;
gc.gridy = c;
add(lbl, gc); //add(component, constraintObj);
label[r][c] = lbl;
}
}
// now find random fields for the colors defined in BACKGROUND
for(Color col : colors) {
int r, c;
do { // make sure to find unique fields
r = (int)Math.floor(Math.random() * ROWS);
c = (int)Math.floor(Math.random() * COLS);
} while(!label[r][c].getBackground().equals(BACKGROUND));
label[r][c].setBackground(col);
}// end
int i=0;
Component[] components = getComponents();
for (Component component : components) {
i++;
Color color = component.getBackground();
Rectangle bounds = component.getBounds();
System.out.println("box "+i +",coordinate= "+bounds +", Color= "+color);
}
}
}
出力: 私は座標得ることができなかった、すべての座標は、同じように見えます java.awt.Rectangle [X = 0、Y = 0、幅= 0、高さ= 0]
解決
いかがます:
GridBagLayout lay = new GridBagLayout();
setLayout(lay);
// Add components
add(comp1, constraints);
add(comp2, constraints);
//
GridBagConstraints c = lay.getConstraints(comp1);
System.out.println(c.gridx + ", " + c.gridy);
あなたのケースでは、するSystem.out.printlnの代わりにこれを試してください:
GridBagConstraints gbc = ((GridBagLayout) getLayout()).getConstraints(component);
System.out.println("box " + i + ",coordinate=" + gbc.gridx + " , " + gbc.gridy);
私のためにその作品...
あなたは、画面上の部品の位置に必要がある場合は、、必ずフレームが最初に表示されます。私はあなたのmain()
の代わりにこれを使用する場合、例えば、それが適切な位置を印刷します:
public static void main(String[] args) {
final JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Grid grid = new Grid();
jf.add(grid);
jf.pack();
jf.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
int i = 0;
Component[] components = grid.getComponents();
for (Component component : components) {
i++;
Color color = component.getBackground();
Rectangle bounds = component.getBounds();
System.out.println("box " + i + ",coordinate= " + bounds
+ ", Color= " + color);
}
}
});
jf.setVisible(true);
}
それはWindowListener
だけでなく、すべてのケースで動作します。念のフレームですでに目に見える...
他のヒント
あなたがセルに追加されたコンポーネントから位置を取得することができます。
Rectangle bounds = component.getBounds();
それとも、このようなものを使用することができ、グリッド全体を経るます:
Component[] components = panel.getComponents();
for (Component component : components) {
Color color = component.getBackground();
Rectangle bounds = component.getBounds();
}
所属していません StackOverflow