javaのswingを使ったGUIプログラムについてです。

Writer: admin Type: ballsarmy Date: 2018-12-26 00:00
javaのswingを使ったGUIプログラムについてです。下記のプログラムの実行結果のCenterボタンの位置に、新たなJPanel「centerPanel」を配置し、centerPanelの中にボタン「north」「south」「west」「east」「center」を描画するプログラムの作り方がわかりません。package vol1.ch01;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class Layout01 extends JFrame {public static void main(String[] args) {Layout01 w = new Layout01( "Layout01" );w.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );w.setSize( 350, 200 );w.setVisible( true );}public Layout01( String title ){super( title );JPanel pane = (JPanel)getContentPane();JButton buttonNorth = new JButton( "North" );pane.add( buttonNorth, BorderLayout.NORTH );JButton buttonCenter = new JButton( "Center" );pane.add( buttonCenter, BorderLayout.CENTER );JButton buttonSouth = new JButton( "South" );pane.add( buttonSouth, BorderLayout.SOUTH );JButton buttonWest = new JButton( "West" );pane.add( buttonWest, BorderLayout.WEST );JButton buttonEast = new JButton( "East" );pane.add( buttonEast, BorderLayout.EAST );}}###他にやり方があるかもしれないですが、参考になれば public Layout01( String title ) { super( title ); Container cp = getContentPane(); JPanel p1 = new JPanel(); p1.setLayout(new BorderLayout()); JButton buttonNorth = new JButton("North"); p1.add(buttonNorth); cp.add(p1, BorderLayout.NORTH); JPanel p2 = new JPanel(); p2.setLayout(new BorderLayout()); JButton bt1 = new JButton("North-C"); JButton bt2 = new JButton("Center-C"); JButton bt3 = new JButton("West-C"); JButton bt4 = new JButton("East-C"); JButton bt5 = new JButton("South-C"); p2.add(bt1, BorderLayout.NORTH); p2.add(bt2, BorderLayout.CENTER); p2.add(bt3, BorderLayout.WEST); p2.add(bt4, BorderLayout.EAST); p2.add(bt5, BorderLayout.SOUTH); cp.add(p2, BorderLayout.CENTER); JPanel p3 = new JPanel(); p3.setLayout(new BorderLayout()); JButton buttonSouth = new JButton("South"); p3.add(buttonSouth); cp.add(p3, BorderLayout.SOUTH); JPanel p4 = new JPanel(); p4.setLayout(new BorderLayout()); JButton buttonWest = new JButton("West"); p4.add(buttonWest); cp.add(p4, BorderLayout.WEST); JPanel p5 = new JPanel(); p5.setLayout(new BorderLayout()); JButton buttonEast = new JButton("East"); p5.add(buttonEast); cp.add(p5, BorderLayout.EAST); }w.setSize( 450, 200 );くらいにして幅を広げて確認してみてください。ナイス0
###この質問は投票によってベストアンサーに選ばれました!

 

TAG