プロが教えるわが家の防犯対策術!

GridLayoutレイアウトで ボタンを追加し
後で プログラムで レイアウトの( 2, 1 )に 
別のボタンを配置したり

( 2, 2 )の位置に 何のコンポーネントのオブジェクトがあるか?
と知ることが出来る 方法はありますか?

教えてください。



JPanel p = new JPanel();
p.setLayout( new GridLayout( 2, 2 ) );
JButton b1 = new JButton( "Button1" );
JButton b2 = new JButton( "Button2" );
JButton b3 = new JButton( "Button3" );
JButton b4 = new JButton( "Button3" );
p.add( b1 );
p.add( b2 );
p.add( b3 );
p.add( b4 );

A 回答 (2件)

サンプルコードを示します。



>( 2, 2 )の位置に 何のコンポーネントのオブジェクトがあるか?
GridLayoutedPanel#getComponent( int row, int col )

>後で プログラムで レイアウトの( 2, 1 )に 別のボタンを配置
GridLayoutedPanel#setComponent( Component c, int row, int col )

で、実装してあります。
なお、インデックスは0から始まりますので、
GridLayoutedPanel#setLayout( new GridLayout( 2, 2 ) ) の場合、
指定可能なインデックス値は(0,0)~(1,1)であることにご注意ください。



----------- GridLayoutedPanel.java -----------


import java.awt.Component;
import java.awt.GridLayout;

import javax.swing.JPanel;

/**
* グリッドレイアウト専用のJPanelのサブクラスです。
*/
public class GridLayoutedPanel extends JPanel {

/**
* ダブルバッファーおよびフローレイアウトで新しい JPanel を作成します。
* 後程、setLayoutメソッドで、GridLayoutマネジャーを設定してください。
*/
public GridLayoutedPanel() {
}
/**
* ダブルバッファーおよびグリッドレイアウトで新しい JPanel を作成します。
* @param rows 行数
* @param cols 列数
*/
public GridLayoutedPanel( int rows, int cols ) {
setLayout( new GridLayout(rows, cols) );
}
/**
* ダブルバッファーおよびグリッドレイアウトで新しい JPanel を作成します。
* @param rows 行数
* @param cols 列数
* @param hgap 水平方向の間隔
* @param vgap 垂直方向の間隔
*/
public GridLayoutedPanel( int rows, int cols, int hgap, int vgap ) {
setLayout( new GridLayout(rows, cols, hgap, vgap) );
}

/**
* 指定の行、列の位置にあるコンポーネントを返します。
* インデックスは0から始まります。
* @param row 行位置
* @param col 列位置
* @return 指定位置のコンポーネント
*/
public Component getComponent( int row, int col ) {
return getComponent( convertIndex(row, col) );
}

/**
* 指定位置のコンポーネントを差し替えます。
* インデックスは0から始まります。
* @param c 差し替えるコンポーネント
* @param row 行位置
* @param col 列位置
* @return 差し替えられる前にあったコンポーネント
*/
public Component setComponent( Component c, int row, int col ) {
int index = convertIndex(row, col);
Component old = getComponent( index );
remove( index );
add( c, index );
return old;
}

/**
* グリッドレイアウトのインデックスを1次元インデックスに変換します。
* @param row 行位置
* @param col 列位置
* @return 1次元インデックス
*/
private int convertIndex( int row, int col ) {
GridLayout gl = (GridLayout)getLayout();
return row * gl.getColumns() + col;
}

}



----------- GridLayoutedPanelSample.java -----------


import javax.swing.JButton;
import javax.swing.JFrame;

/**
* GridLayoutedPanelのサンプルクラス
*/
public class GridLayoutedPanelSample {

public GridLayoutedPanelSample() {

GridLayoutedPanel p = new GridLayoutedPanel( 2, 2 );
JButton b1 = new JButton( "Button1" );
JButton b2 = new JButton( "Button2" );
JButton b3 = new JButton( "Button3" );
JButton b4 = new JButton( "Button3" );
p.add( b1 );
p.add( b2 );
p.add( b3 );
p.add( b4 );


JFrame frame = new JFrame();
frame.add( p );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setBounds( 100, 100, 300, 200 );
frame.setVisible( true );


//指定位置のコンポーネントを差し替える
p.setComponent( new JButton("Button5"), 1, 0);

//指定位置のコンポーネントを調べる
System.out.println( ((JButton)p.getComponent(1, 1)).getText() );

}

public static void main(String[] args) {
new GridLayoutedPanelSample();
}

}
    • good
    • 0
この回答へのお礼

どうもありがとうございます!
ちょっとサンプルが自分には難しかったのですが
どうもです!

お礼日時:2008/08/11 22:41

java.awt.Containerには、indexを指定するremove()やadd()、およびgetComponent()メソッドがあります。

    • good
    • 0
この回答へのお礼

どうも!
大変参考になりました。
ありがとうございます!

お礼日時:2008/08/11 22:42

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!