
swingでアプリケーションを作成しています。
1つのフレーム上に複数のパネルを描画する(画面遷移)アプリです。
たとえば、ログイン用のパネル、メニュー用のパネルと言ったように用意しています。
ここで問題が発生しました。
フレームの最大化ボタンを押しても、パネルが最大化されず、パネル内に配置しているボタンなども位置の調整が行われません。
フレームにレイアウトマネージャーを指定しても、パネルは最大化されないのでしょうか?
フレーム自体にボタンを配置すると調整はされるのですが・・・
今のところクラス構成としては、
フレームクラス
ログインパネルクラス
メニューパネルクラス
データ表示パネルクラス
となっています。
ログインパネルのボタンを押されたら、ログインパネルを破棄し、メニューパネルを表示するといった作りにしています。
どのパネルを表示しているときにも、フレームの最大化ボタンが押された場合にパネルの最大化も行いたいのです。
どなたかご教授願えませんでしょうか?
No.2ベストアンサー
- 回答日時:
試しに、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);
}
}
No.1
- 回答日時:
> どのパネルを表示しているときにも、フレームの最大化ボタンが押された場合にパネルの最大化も行いたいのです。
特別な事はしなくても、フレームを最大化したら、パネルも最大化される見たいですよ。
以下、例です。
----------------------------------------------------------------------
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を使用しております。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- その他(ソフトウェア) Figma 1 2023/06/23 14:22
- YouTube youtubeは画面と操作パネルが別々に表示されていたのに画面上にかぶるように表示されるようになった 1 2022/05/08 10:04
- Illustrator(イラストレーター) 至急。イラストレーターでこの画面で、整列パネルが表示されてないのですがどこを押せば表示されますか? 1 2022/07/25 19:21
- DIY・エクステリア ルミナスのラックで、ストーブガードを自作したいが、熱くはならない? 1 2023/02/18 18:18
- ノートパソコン 【IGZOパネル】IGZOディスプレイを採用したノートパソコンを教えてください。 I 1 2023/07/11 19:58
- 一戸建て 家の太陽光発電をLIXIL TEPCOの『建て得』採用してる人いませんか? 現在の発電量などをリアル 1 2023/04/03 06:39
- その他(法律) イラストの著作権についてです。 フラワースタンドのパネル用のイラストを私がとある絵師様に依頼しました 1 2023/05/28 00:26
- ガスコンロ・IHクッキングヒーター・給湯器 IHクッキングヒーターについて 写真の青いお鍋から吹きこぼれてしまい、青矢印の方向に滝のように水が流 5 2023/05/17 15:32
- 戦争・テロ・デモ これって本当ですか? 明日の8時23分に核を使うらしいです 見えない部分の内容。【今頃プーチンの戦術 10 2022/10/10 18:11
- モニター・ディスプレイ 液晶モニター選択について 7 2022/08/15 12:56
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
フレームを閉じれない
-
画面を切り替えたい
-
VectorクラスとRunnableインタ...
-
GUIで作成したアプリで、最大化...
-
Javaについて
-
Javaのレイアウトマネージャに...
-
JFrameとJPanelの最大化について
-
テキストボックスの値が変数に...
-
Swingについての質問です
-
ボールが勝手に動き困ってます。
-
はじめまして開発初心者です。
-
javaのpanelが表示されません
-
javaのフレームにサイズがセッ...
-
java で checkBox に関...
-
Javaで
-
スクロールバーの高さが設定で...
-
cadで作った図面をフレーム上で...
-
初心者です。フレームの閉じ方...
-
Java JPanelを使ったコードにつ...
-
p.add(myPanel());にエラーが出...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
java junit list同士の比較方法
-
javaで文字数制限するには?
-
最後の行に移動するには?
-
setPrefferedSizeとsetSize
-
Buttonを押すと画像が表示され...
-
JPanelの切り替えと再描画
-
4択クイズでのランダム出題の...
-
【Swing】JPanel クラス外から...
-
Java-Swing TextAreaにスクロー...
-
KeyListenerの使い方について
-
ArrayListの使い方について(初...
-
連番をオブジェクト名の最後に...
-
TextAreaでの文字の色
-
JPanelの重ね方/OverlayLayout他
-
エラーで式の開始が不正ですと...
-
VB6.0でのTextboxの高さ変更
-
EXECLの行を削除
-
JDialogを閉じた時の処理
-
ラムダ式とかデリゲートっぽい...
-
ボタンクリック後パネルを再描...
おすすめ情報