JavaのSwingのレイアウト

このQ&Aのポイント
  • JavaのSwingを学習中ですが、レイアウトの方法がわかりません。
  • 添付画像のようなレイアウトを作りたいです。
  • コードの一部を添付します。
回答を見る
  • ベストアンサー

JavaのSwingのレイアウト

Swingを学習中ですが、うまくレイアウトできません。 添付した画像のようなレイアウトにしたいです。 作ってみたものは以下です。 import java.awt.BorderLayout; import java.awt.Color; import javax.swing.*; public class LayoutTest { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); JTextField searchFiled = new JTextField("テキスト"); String[] comboboxString = {"C", "C++", "Java"}; JComboBox comboBox = new JComboBox(comboboxString); JButton button = new JButton("ボタン"); panel.add(comboBox, BorderLayout.EAST); panel.add(searchFiled, BorderLayout.CENTER); panel.add(button, BorderLayout.WEST); frame.add(panel, BorderLayout.PAGE_START); JPanel redPanel = new JPanel(); redPanel.setBackground(Color.RED); frame.add(redPanel); frame.setSize(700, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); } }

  • Java
  • 回答数2
  • ありがとう数2

質問者が選んだベストアンサー

  • ベストアンサー
  • maiko0318
  • ベストアンサー率21% (1483/6970)
回答No.2

import java.awt.*; import javax.swing.*; public class LayoutTest { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); JTextField searchFiled = new JTextField("テキスト"); String[] comboboxString = {"C", "C++", "Java"}; JComboBox comboBox = new JComboBox(comboboxString); JButton button = new JButton("ボタン"); //ここから4行 panel.setLayout(new BorderLayout()); panel.add(comboBox, BorderLayout.WEST); panel.add(searchFiled, BorderLayout.CENTER); panel.add(button, BorderLayout.EAST); frame.add(panel, BorderLayout.PAGE_START); JPanel redPanel = new JPanel(); redPanel.setBackground(Color.RED); frame.add(redPanel); frame.setSize(700, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); } } やりたい方向に近づいたでしょうか? WESTが左、EASTが右ですね。

newtgecko
質問者

お礼

ありがとうございます。 西と東も間違えていましたね。 panel.setLayout(new BorderLayout());をしないといけないのがわかりました。ありがとうございます。

その他の回答 (1)

  • Ogre7077
  • ベストアンサー率65% (170/258)
回答No.1

javax.swing.JPanel は標準で FlowLayout なので、ご質問の要件なら BorderLayout に変更する必要があります。 panel.setLayout(new BorderLayout()); または JPanel panel = new JPanel(new BorderLayout());

newtgecko
質問者

お礼

ありがとうございます。 >javax.swing.JPanel は標準で FlowLayout なので よくわかりました。 Swingのレイアウトに関しては色々と疑問が出ると思うので、何かありましたらまたよろしくお願いします。

関連するQ&A

  • Swing自作コンポーネントをadd出来るように、

    JTextFieldやJButtonなどを一つにまとめたコンポーネントを自作し、それをadd()出来るようにしたいです。frame.add(自作のクラスのインスタンス)を可能にしたいです。 調べて、paintComponent()を使えば良いということはわかったのですが、Graphicsのインスタンス(?)に線を描いたり、円を描いたりする方法はわかりましたが、JButtonやJTextFieldなどをGraphicsのインスタンス(?)に追加する方法はわからず、paintComponent()を使うやり方はできませんでした。 paintComponent()も使わずに、自分のできる方法で、組んだものを一応載せます。 import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class MyComponentTest{ public static void main(String[] args){ JFrame frame = new JFrame("タイトル"); frame.setSize(400, 300); frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS)); // 2つのテキストフィールドと一つのボタンを4つ追加する frame.add(new TwoTextFieldsAndOneButton().getContent()); frame.add(new TwoTextFieldsAndOneButton().getContent()); frame.add(new TwoTextFieldsAndOneButton().getContent()); frame.add(new TwoTextFieldsAndOneButton().getContent()); frame.setVisible(true); } } //2つのテキストフィールドと一つのボタンを一つの部品にしたい class TwoTextFieldsAndOneButton{ JTextField textField1 = new JTextField("テキストフィールド1"); JTextField textField2 = new JTextField("テキストフィールド2"); JButton button = new JButton("ボタン"); // こんなメソッドを使わずに、再現したい JPanel getContent(){ JPanel panel = new JPanel(); panel.add(textField1); panel.add(textField2); panel.add(button); return panel; } }

    • ベストアンサー
    • Java
  • javaのBoxlayoutについて

    下記はBoxLayout用ののコードです。 Q1)Buttonのサイズを設定する方法がありますか? Q2)Buttonの配置を設定する方法がありますか? //===================================== import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.LayoutManager; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class AlignmentX extends JFrame{ JButton button1; JButton button2; JButton button3; public static void main(String[] args){ AlignmentX frame = new AlignmentX(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("タイトル"); frame.setVisible(true); } AlignmentX(){ button1 = new JButton("AAAAA"); button1.setAlignmentY(0.5f); button1.setPreferredSize(new Dimension(80,20)); button2 = new JButton("BBB"); button2.setAlignmentY(0.5f); button2.setPreferredSize(new Dimension(80,20)); button3 = new JButton("CC"); button3.setAlignmentY(0.5f); button3.setPreferredSize(new Dimension(80,20)); JPanel p = new JPanel(); p.setLayout((LayoutManager) new BoxLayout(p, BoxLayout.Y_AXIS)); p.add(button1); p.add(button2); p.add(button3); getContentPane().add(p, BorderLayout.CENTER); setBounds(10, 10, 300, 200); } } //以上,宜しくお願いします。

    • ベストアンサー
    • Java
  • javaでストップウォッチが上手く作れません

    あるサイトを参考にして作ってみたんですが うまく動いてくれません、多分おかしいところだらけですが どこがダメか教えてもらえるとありがたいです package timeP; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class timeC extends JFrame implements Runnable { private JPanel contentPane; private JTextField textField; private Thread th = null; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { timeC frame = new timeC(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public timeC() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 85); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS)); JPanel panel_3 = new JPanel(); contentPane.add(panel_3); panel_3.setLayout(new BorderLayout(0, 0)); textField = new JTextField(); panel_3.add(textField, BorderLayout.CENTER); textField.setColumns(10); JPanel panel_2 = new JPanel(); contentPane.add(panel_2); panel_2.setLayout(new BorderLayout(0, 0)); JButton JButtonstart = new JButton(" 開始 "); JButtonstart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if ( th == null ) { th = new Thread ( ); th.start(); } } }); panel_2.add(JButtonstart, BorderLayout.CENTER); JPanel panel_1 = new JPanel(); contentPane.add(panel_1); panel_1.setLayout(new BorderLayout(0, 0)); JButton JButtonstop = new JButton(" 停止 "); JButtonstop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if ( th != null ){ th = null; } } }); panel_1.add(JButtonstop, BorderLayout.CENTER); JPanel panel = new JPanel(); contentPane.add(panel); panel.setLayout(new BorderLayout(0, 0)); JButton btnNewButton_2 = new JButton("New button"); panel.add(btnNewButton_2, BorderLayout.CENTER); } @Override public void run() { //TODO 自動生成されたメソッド・スタブ int i; while ( th != null ){ i = Integer.parseInt( textField.getText() ); try { Thread.sleep(10000); if ( th == null ) break; } catch ( InterruptedException e ){ break; } textField.setText( Integer.toString( i + 1 ) ); } } }

  • JavaでのSwingを使った簡易電卓作成

    Javaについて勉強中の学生です。 よろしくお願いします。 CGIによるプログラムは一通り勉強しました。 次にGUIによるプログラムを勉強中です。 そこで電卓を作ってみようと思いましたが、よくわからないところがあります。 ご教示いただければ幸いです。 一応外見だけは作ることができましたが、イベントを登録するときに電卓のボタンをbtn1,btn2,btn3…とすればイベントを登録することはできるのですが、それだとボタンの分だけ作成しなければならず修正を加えるのが大変になってしまいます。 例)btn1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ hyozi.setText(hyozi.getText() + "1"); } } そこで「btn」に統一しボタンに表示されている文字列を使い簡潔にプログラムをまとめたいのですがどのようにすればよいのかわかりません。以下のプログラムに追加できるようなものを教えていただけないでしょうか? import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Dentaku{ private JFrame frame; private JPanel panel_fun,panel_ten; private JTextField hyozi; private JButton btn; public Dentaku(){ //フレームの作成してレイアウトをセット frame = new JFrame("簡易電卓プログラム"); frame.setLayout(new BorderLayout()); //パネルを作成してレイアウトをセット panel_fun = new JPanel(); panel_fun.setLayout(new GridLayout(2,3)); panel_ten = new JPanel(); panel_ten.setLayout(new GridLayout(4,3)); //表示用テキストボックスを作成 hyozi = new JTextField(); //電卓のボタンを作成 panel_fun.add(btn = new JButton("CA")); panel_fun.add(btn = new JButton("+")); panel_fun.add(btn = new JButton("-")); panel_fun.add(btn = new JButton("*")); panel_fun.add(btn = new JButton("/")); panel_fun.add(btn = new JButton("=")); panel_ten.add(btn = new JButton("1")); panel_ten.add(btn = new JButton("2")); panel_ten.add(btn = new JButton("3")); panel_ten.add(btn = new JButton("4")); panel_ten.add(btn = new JButton("5")); panel_ten.add(btn = new JButton("6")); panel_ten.add(btn = new JButton("7")); panel_ten.add(btn = new JButton("8")); panel_ten.add(btn = new JButton("9")); panel_ten.add(btn = new JButton("0")); //フレームに表示用テキストボックスをセット(BorderLayoutの北側) frame.add(hyozi,BorderLayout.NORTH); //フレームにパネルをセット(BorderLayoutの中央,南側) frame.add(panel_fun,BorderLayout.CENTER); frame.add(panel_ten,BorderLayout.SOUTH); //フレームの詳細設定 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,250); frame.setVisible(true); } //mainメソッドの定義 public static void main(String[] args){ Dentaku application = new Dentaku(); } }

    • ベストアンサー
    • Java
  • JAVAでの背景画像表示

    現在javaでゲームのメニュー画面を作っているのですが、ボタンを配置する所まではできたんですが、背景に画像を表示することができなくて困っています! import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Container; public class Mati extends JFrame{ public static void main(String[] args){ Mati frame = new Mati(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10,650,650); frame.setTitle("街"); frame.setVisible(true); JPanel h = new JPanel(); h.setOpaque(false); ImageIcon icon1 = new ImageIcon("Mati.jpg"); JLabel label1 = new JLabel(icon1); JLabel label2 = new JLabel(); h.add(label1); } Mati(){ JButton button1 = new JButton("宿屋"); button1.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button2 = new JButton("道具屋"); button2.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button3 = new JButton("武器屋"); button3.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button4 = new JButton("街を出る"); button4.setFont(new Font("Mairyo", Font.PLAIN, 30)); JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); p.add(Box.createRigidArea(new Dimension(10,30))); p.add(button1); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button2); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button3); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button4); getContentPane().add(p, BorderLayout.CENTER); } } ソースはこのようになっています。これからどうすれば背景画像が表示されるか教えていただきたいです。 よろしくお願いします。

  • javaでスタート画面を作っていて困っています。

    現在javaでゲームのスタート画面を作っているのですが、パネルが透過されずに困っています。 プログラムソースは import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.*; import java.awt.Font; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Container; class Start最新版 extends JFrame{ public static void main(String args[]){ Start最新版 frame = new Start最新版("タイトル"); frame.setVisible(true); } Start最新版(String title){ setTitle(title); setBounds(10, 10, 1024, 768); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); p.setOpaque( false ); ImageIcon icon1 = new ImageIcon("Start.jpg"); JLabel label1 = new JLabel(icon1); JLabel label2 = new JLabel(); p.add(label1); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); JButton button1 = new JButton("ゲームを始める"); button1.setFont(new Font("Mairyo", Font.PLAIN, 30)); JPanel n = new JPanel(); n.setOpaque(false); n.setLayout(new BoxLayout(n, BoxLayout.Y_AXIS)); n.add(Box.createRigidArea(new Dimension(290,30))); n.add(button1); となっています。「ゲームを始める」ボタンだけを残して、周りのパネルを透過して画像を表示させたいのですが、うまくいきません!解決方法をご存知の方どうか教えていただきたいです!よろしくお願いします。

  • javaのJTextFieldにマウスフォーカス

    お世話になります。 Q1)下記のコードに於きまして、JTextFieldにマウスフォーカス が当りますと、その旨、System.out.println("JTextField_tt")と表示する方法をお教えください。 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.LineBorder; public class EObjectTest extends JFrame implements ActionListener { public static JButton b1; public static JButton b2; public static JTextField tt; static EObjectTest frame; public static void main(String args[]){ frame=new EObjectTest("AAAA"); frame.setVisible(true); } EObjectTest(String title) { setTitle(title); setBounds(100, 100, 300, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); // ボタン作成・追加 b1 = new JButton("One"); b2 = new JButton("Two"); tt = new JTextField("aaa"); LineBorder border = new LineBorder(Color.RED, 2, true); tt.setBorder(border); tt.setColumns(15); p.add(b1); p.add(b2); p.add(tt); Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); // リスナ登録 b1.addActionListener(this); b2.addActionListener(this); tt.addActionListener(this); } public void actionPerformed(ActionEvent e) { // getSource() でイベントソースのオブジェクトを獲得し // Button クラスにキャストする JButton b = (JButton)e.getSource(); // JTextField ttt = (JTextField)e.getSource(); if (b==b1) { System.out.println("Oneのボタン"); } if (b==b2) { System.out.println("Twoのボタン"); } /* if (ttt==tt) { System.out.println("JTextField_tt"); } */ } } 以上

    • ベストアンサー
    • Java
  • javaのcomboBoxでデーターの右寄表示

    javaのcomboBoxで表示データーの右寄せ表示について質問します。 Q1)下記のコードでどの様に変更すれば良いでしょうか? ====================== import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; public class comboBox_Right extends JFrame { public static JComboBox[] combo=new JComboBox[4]; static comboBox_Right frame; public static void main(String args[]){ frame=new comboBox_Right("AAAA"); frame.setVisible(true); } comboBox_Right(String title) { setTitle(title); setBounds(100, 100, 300, 420); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel p = new JPanel(); //======================================== //ComboBox data String[][] combodata = { {"A0", "B0", "C0", "D0"}, //No.1 Combo {"A1", "B1", "C1", "D1"}, //No.1 Combo {"A2", "B2", "C2", "D2"}, //No.1 Combo {"A3", "B3", "C3", "D3"}, //No.1 Combo }; for(int i=0; i<4; i++){ combo[i] = new JComboBox(combodata[i]); //make 4 comboBox combo[i].setPreferredSize(new Dimension(175, 25)); //ComboBox Size } //=============== for(int i=0; i<4; i++) p.add(combo[i]); //add 4 comboBox //=============== Container contentPane = getContentPane(); contentPane.add(p, BorderLayout.CENTER); } } =============================== 以上、宜しくお願いします。

    • ベストアンサー
    • Java
  • javaのpanelが表示されません

    下記のプログラムを作ったんですが、eclipseで実行するとフレームは表示されますが、空のフレームのような状態で、panelやボタンなどは表示されません。mainとは別のクラスで作成してるからなんでしょうか?よろしくお願いします。 import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class Table0 { public static void main(String[] args) { new Frame0(); } } class Frame0{ JFrame frame; JPanel panel; JScrollPane jsp; JTable table; DefaultTableModel dtm; public Frame0(){ frame=new JFrame("table"); frame.setBounds(30, 30, 300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); table=new JTable(8,5); jsp=new JScrollPane(table); jsp.setPreferredSize(new Dimension(250, 70)); panel=new JPanel(); panel.add(jsp); Container c=frame.getContentPane(); c.add(panel,BorderLayout.CENTER); } }

    • ベストアンサー
    • Java
  • GridLayoutのPanel上のButton

    GridLayoutのPanel上のButton横サイズを取得したいのですが、 button1.getWidth()==0 と出てしまいます。 フレームやパネルのサイズをボタン数で割れば出るのですが、 そうではなく、ボタンのサイズを取得したいです。 ご存知の方がおられましたら教えて下さい。 //----以下ソースです---- import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class ButtonSizeOnGridLayout extends JFrame { public ButtonSizeOnGridLayout() { JPanel panel1 = new JPanel(new GridLayout(0, 5)); JButton button1 = new JButton(); button1.setPreferredSize(new Dimension(150, 150)); System.out.println("button1.getWidth()==" + button1.getWidth());//出力:0 panel1.add(button1); this.getContentPane().add(panel1); this.pack(); this.setVisible(true); } public static void main(String a[]) { new ButtonSizeOnGridLayout(); } }

    • ベストアンサー
    • Java

専門家に質問してみよう