私はJPanelを持っています。たとえば、[挿入]ボタンをクリックすると、JButtonとJLabelを追加できます。私の問題は、JButtonの下にJLabelを挿入する必要があることです。JLabelテキストは、JButtonテキストを中心に配置する必要があります。その後、10ピクセル前後のスペースで「挿入」ボタンを再度使用し、同じ向きでJButtonとJLabelに新しいペアを水平方向に追加します。
ありがとう!
PD:質問を補足してみてください。
そんなものがあると思います
rootPane
+-----panelButton
| +------JButton
|
+-----panelPanels
+-----panel
+---JButton
+---JLabel
はあなたSpringLayout
を助けることができます
SpringUtilities.makeGrid(panel,
2, 1, //rows, cols
0, 0, //initialX, initialY
5, 5);//xPad, yPad
これは、未定義の数のパネルを挿入できるようにする動的な(これは私が望んでいたと思う)セットアップを示す簡単な例です。
public class AwesomeAnswer {
public static void main(String[] args) {
// please not that this is only an example and not a
// Swing thread safe way of starting a JFrame
JFrame frame = new JFrame();
JPanel content = (JPanel)frame.getContentPane();
// create our top panel that will hold all of the inserted panels
JPanel page = new JPanel();
page.setLayout( new BoxLayout( page, BoxLayout.Y_AXIS ) );
// add our page to the frame content pane
content.add( page );
// add two button/label panels
page.add( insert( "This is an awesome answer", "Accept" ) );
page.add( insert( "Say thank you", "Thank" ) );
frame.pack();
frame.setVisible( true );
}
public static final JPanel insert( String labelText, String buttonText ) {
// create the label and the button
JLabel lbl = new JLabel( labelText );
JButton btn = new JButton( buttonText );
// create the panel that will hold the label and the button
JPanel wrapPanel = new JPanel( new GridBagLayout() );
wrapPanel.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
// tell the grid bag how to behave
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 0;
gbc.gridheight = 2;
// make the button centered
JPanel buttonPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
buttonPanel.add( btn );
// make the label centered
JPanel labelPanel = new JPanel( new FlowLayout( 0, 0, FlowLayout.CENTER ) );
labelPanel.add( lbl );
// add our button and label to the grid bag with our constraints
wrapPanel.add( buttonPanel, gbc );
wrapPanel.add( labelPanel, gbc );
return wrapPanel;
}
}