プロが教える店舗&オフィスのセキュリティ対策術

swingでアプリケーションを作成しています。

1つのフレーム上に複数のパネルを描画する(画面遷移)アプリです。
たとえば、ログイン用のパネル、メニュー用のパネルと言ったように用意しています。

ここで問題が発生しました。
フレームの最大化ボタンを押しても、パネルが最大化されず、パネル内に配置しているボタンなども位置の調整が行われません。
フレームにレイアウトマネージャーを指定しても、パネルは最大化されないのでしょうか?
フレーム自体にボタンを配置すると調整はされるのですが・・・

今のところクラス構成としては、
フレームクラス
ログインパネルクラス
メニューパネルクラス
データ表示パネルクラス
となっています。

ログインパネルのボタンを押されたら、ログインパネルを破棄し、メニューパネルを表示するといった作りにしています。

どのパネルを表示しているときにも、フレームの最大化ボタンが押された場合にパネルの最大化も行いたいのです。

どなたかご教授願えませんでしょうか?

A 回答 (2件)

> どのパネルを表示しているときにも、フレームの最大化ボタンが押された場合にパネルの最大化も行いたいのです。


特別な事はしなくても、フレームを最大化したら、パネルも最大化される見たいですよ。

以下、例です。
----------------------------------------------------------------------
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class SampleApplication {
  private JFrame frame = null;
  private JPanel panel = null;
  private BorderLayout layoutManager = null;
  
  public static void main(String[] args) {
    SampleApplication app = new SampleApplication();
  }
  
  SampleApplication() {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }
  
  private void createAndShowGUI() {
    // create window.
    frame = new JFrame("SampleApplication");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    // create panel.
    panel = new JPanel();
    frame.getContentPane().add(panel);
    
    // create LayoutManager.
    layoutManager = new BorderLayout();
    panel.setLayout(layoutManager);
    
    // create Button
    panel.add(new JButton("Button1"), BorderLayout.NORTH);
    panel.add(new JButton("Button2"), BorderLayout.WEST);
    panel.add(new JButton("Button3"), BorderLayout.CENTER);
    panel.add(new JButton("Button4"), BorderLayout.EAST);
    panel.add(new JButton("Button5"), BorderLayout.SOUTH);
    
    // Display the window.
    frame.pack();
    frame.setVisible(true);
  }
}
----------------------------------------------------------------------

この回答への補足

たとえば、ログインボタンを押した際には、以下のようにしてメニュー画面を表示しています。
getContentPane().remove(jPanel1);
NewJPanel panel = new NewJPanel();
getContentPane().add(panel);
repaint();
validate();

このときパネルにサイズを指定しないと次のパネルが表示されません。
サイズを指定して、次のパネルを表示させて後最大化ボタンを押してもパネルに設定されているボタン等の位置が調整されません。
こういった場合の対処方法はご存知でしょうか?

ちなみに、フレーム・パネルともにGritBagLayoutを使用しております。

補足日時:2010/04/14 17:17
    • good
    • 0

試しに、GridBagLayoutで、パネルを変更するサンプル作ってみました。



参考までにどうぞ。
----------------------------------------------------------------------
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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.SwingUtilities;

public class SampleApplication2 implements ActionListener {
  private JFrame frame = null;
  private JPanel panel1 = null;
  private JPanel panel2 = null;
  private GridBagLayout layoutManager1 = null;
  private GridBagLayout layoutManager2 = null;
  private JButton btnToScreen1 = null;
  private JButton btnToScreen2 = null;
  
  public static void main(String[] args) {
    SampleApplication2 app = new SampleApplication2();
  }
  
  SampleApplication2() {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }
  
  public void actionPerformed(ActionEvent event) {
    if (event.getSource().equals(btnToScreen1)) {
      frame.getContentPane().remove(panel2);
      frame.getContentPane().add(panel1);
      SwingUtilities.updateComponentTreeUI(frame);
    } else if (event.getSource().equals(btnToScreen2)) {
      frame.getContentPane().remove(panel1);
      frame.getContentPane().add(panel2);
      SwingUtilities.updateComponentTreeUI(frame);
    }
  }
  
  private void addComponent(JPanel panel, GridBagLayout gbl, Component comp, int x, int y
      , int w, int h) {
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.gridx = x;
    c.gridy = y;
    c.gridwidth = w;
    c.gridheight = h;
    c.weightx = 1.00; // panel のサイズに連動して、コンポーネントの配置を自動調整。
    c.weighty = 1.00; // panel のサイズに連動して、コンポーネントの配置を自動調整。
    gbl.setConstraints(comp, c);
    panel.add(comp);
  }
  
  private void createAndShowGUI() {
    // create window.
    frame = new JFrame("SampleApplication");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    // create panel.
    panel1 = new JPanel();
    panel2 = new JPanel();
    frame.getContentPane().add(panel1);
    
    // create LayoutManager.
    layoutManager1 = new GridBagLayout();
    panel1.setLayout(layoutManager1);
    layoutManager2 = new GridBagLayout();
    panel2.setLayout(layoutManager2);
    
    // create Button
    btnToScreen2 = new JButton("画面2へ");
    btnToScreen2.addActionListener(this);
    addComponent(panel1, layoutManager1, btnToScreen2, 0, 0, 1, 3);
    addComponent(panel1, layoutManager1, new JButton("Button2"), 1, 0, 1, 1);
    addComponent(panel1, layoutManager1, new JButton("Button3"), 1, 1, 1, 1);
    addComponent(panel1, layoutManager1, new JButton("Button4"), 1, 2, 1, 1);
    
    btnToScreen1 = new JButton("画面1へ");
    btnToScreen1.addActionListener(this);
    addComponent(panel2, layoutManager2, new JButton("Button5"), 0, 0, 3, 1);
    addComponent(panel2, layoutManager2, new JButton("Button6"), 0, 1, 1, 1);
    addComponent(panel2, layoutManager2, new JButton("Button7"), 1, 1, 1, 1);
    addComponent(panel2, layoutManager2, btnToScreen1, 2, 1, 1, 1);
    
    // Display the window.
    frame.pack();
    frame.setVisible(true);
  }
}
    • good
    • 1
この回答へのお礼

早速のご解答ありがとうございます。

上記を参考にして、いろいろと試みてみます。

お礼日時:2010/04/15 08:55

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