0

メニュー項目など、プログラムの実行中に JPanel のサイズを変更したい。どうすれば到達できますか?または、コンテンツ ペインのライブ JPanel にアクセスするには、プログラムをどのように変更すればよいですか?

編集: デフォルトで最初に400x400のゲームフィールドのコンテンツペインを作成した場合。しかし、サイズを 500x500 に変更するオプションをメニューに表示したいが、プレイヤーが既に進行中のすべてのコンテンツを失わないようにしたい場合はどうすればよいでしょうか。

JFrame クラス:

package me.test;

import javax.swing.JFrame;


public class Main extends JFrame {
    private static final long serialVersionUID = 1L;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    new Main();
}

public Main() {

    setContentPane(new MyJPanel());

    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocation(100,50);
    setResizable(false);
    setVisible(true);


    }

}

私の変更された JPanel:

package me.test;

import java.awt.Dimension;

import javax.swing.JPanel;

public class MyJPanel extends JPanel {

    public MyJPanel() {
        setPreferredSize(new Dimension(400,400));
    }
}
4

2 に答える 2

2

いくつかのアイデアについては、このコードを見てください。コンポーネント自体のツールバーまたはフレームのメニュー項目に配置されたアクションに従ってサイズを変更できます。

コードの重要なポイントは次のとおりです。

  • 問題のコンポーネントに適切なサイズを設定します。
  • 最上位コンテナーへの参照を取得します。
  • 最後pack()に最上位のコンテナです。

ここに画像の説明を入力

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class GameSize {

    // the GUI as seen by the user (without frame)
    private JPanel gui = new JPanel(new GridBagLayout());
    Action small = new AbstractAction("Small") {

        @Override
        public void actionPerformed(ActionEvent e) {
            setSize(400, 100);
        }
    };
    Action large = new AbstractAction("Large") {

        @Override
        public void actionPerformed(ActionEvent e) {
            setSize(600, 200);
        }
    };

    private final void setSize(int w, int h) {
        gui.setPreferredSize(new Dimension(w, h));
        Container c = gui.getTopLevelAncestor();
        if (c instanceof JFrame) {
            JFrame f = (JFrame) c;
            f.pack();
        }
    }

    GameSize() {
        JToolBar tb = new JToolBar("Size");
        for (Action action : getActions()) {
            tb.add(action);
        }
        gui.add(tb);

        gui.setPreferredSize(new Dimension(200, 100));
        gui.setBackground(Color.RED);
    }

    /*
     * @return the Actions
     */
    public Action[] getActions() {
        Action[] actions = {small, large};

        return actions;
    }

    /**
     * @return the gui
     */
    public JPanel getGui() {
        return gui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                GameSize gs = new GameSize();

                JFrame f = new JFrame("Demo");
                f.add(gs.getGui());
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See http://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                JMenuBar menuBar = new JMenuBar();
                f.setJMenuBar(menuBar);
                JMenu size = new JMenu("Size");
                menuBar.add(size);
                for (Action action : gs.getActions()) {
                    size.add(action);
                }

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
于 2014-02-04T16:04:22.650 に答える