5

NetBeansでJavaアプリケーション(プロジェクト)を作成しました。このアプリケーションではJFrame、メニューバーを使用してを設計しましたJPanels。これらJPanelsをさまざまなメニュー項目のonアクション内に表示してJFrame、メニュー項目がクリックされるたびに異なるメニュー項目がJPanels内に表示されるようにしJFrameます。私は両方JFrameJPanel別々に設計しましたが、それらをリンクすることができませんでした。

友達を助けてください。

4

7 に答える 7

5

これにはカードレイアウトを使用できます。カード レイアウトには多くのコンポーネント (この場合は JPanel) を含めることができ、それらを切り替えることができます。netbeans パレットにカード レイアウトを追加するのは簡単です。

ドキュメント:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/CardLayout.html

于 2009-01-30T12:00:29.317 に答える
3

ボタンがクリックされたときにJPanelsを動的に作成したいというコメントの1つから見えます。この場合、CardLayout は理想的ではありません。同じ効果を自分で達成するのは比較的簡単です。コードは次のようになります。

public void actionPerformed(ActionEvent event) {
    Container contentPane = frame.getContentPane();
    contentPane.removeAll();
    contentPane.add(new YourPanel());
    contentPane.invalidate();
    contentPane.repaint();
}

これは、変化するパネルがフレーム内の唯一のコンポーネントであると想定しています。そうでない場合は、BorderLayout を持つ JPanel を Matisse のコンテンツ ペインに追加し、新しいパネルをコンテンツ ペインではなくコンテンツ ペインに追加します。

于 2009-05-10T11:54:01.517 に答える
1

まず、.javaファイルとして保存します。これはメインの jframe クラスです。最初に実行します。次に、外部パネルがコンストラクターによって追加されていることがわかります。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on Apr 20, 2010, 5:20:26 PM
 */

package game;

import javax.swing.SwingUtilities;

/**
 *
 * @author S.M. Mahmudul hasan
 */
public class MainFrame extends javax.swing.JFrame {

    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
        addPanel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        firstPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout firstPanelLayout = new javax.swing.GroupLayout(firstPanel);
        firstPanel.setLayout(firstPanelLayout);
        firstPanelLayout.setHorizontalGroup(
            firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 457, Short.MAX_VALUE)
        );
        firstPanelLayout.setVerticalGroup(
            firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 398, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(firstPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(firstPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    private void addPanel() {
        MainPanel m=new MainPanel();
        this.getContentPane().remove(0);
        firstPanel.removeAll();
        javax.swing.GroupLayout firstPanelLayout = new javax.swing.GroupLayout(firstPanel);
        firstPanel.setLayout(firstPanelLayout);
        firstPanelLayout.setHorizontalGroup(
            firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(firstPanelLayout.createSequentialGroup()
                .addGap(78, 78, 78)
                .addComponent(m, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(79, Short.MAX_VALUE))

        );
        firstPanelLayout.setVerticalGroup(
            firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(firstPanelLayout.createSequentialGroup()
                .addGap(25, 25, 25)

                .addComponent(m, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(23, 23, 23))
        );
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(firstPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(firstPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        SwingUtilities.updateComponentTreeUI(this.getContentPane());
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel firstPanel;
    // End of variables declaration

}

パネル クラス:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainPanel.java
 *
 * Created on Apr 20, 2010, 5:21:03 PM
 */

package game;

import java.awt.Graphics;

/**
 *
 * @author S.M. Mahmudul hasan
 */
public class MainPanel extends javax.swing.JPanel {

    /** Creates new form MainPanel */
    public MainPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("paintable area"));

        jLabel1.setText("Paintable area");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(jLabel1)
                .addContainerGap(128, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(103, 103, 103)
                .addComponent(jLabel1)
                .addContainerGap(119, Short.MAX_VALUE))
        );
    }// </editor-fold>

  @Override public void paintComponent(Graphics g) {
         super.paintComponent(g);    // paints background
         g.drawString("shohan", 50, 100);
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration

}
于 2010-04-20T11:52:43.940 に答える
1

Matisse GUI ビルダーは、「JTabbedPane」コントロールを探しているのであれば、かなり適切にサポートしています。TabbedPane コンテナをフォームにドラッグしてから、他のコンテナをドラッグして新しいタブを作成できます。

ユーザーが別のボタンを押したときに別のコンテナーを非表示/表示するなど、より高度な動作を探している場合は、いくつかのコードを記述する必要があります。GUI ビルダーには、これを処理する機能がありません。

于 2009-02-08T00:28:08.410 に答える
0

GUIをもっと魅力的にしたい場合は、 JDesktopPaneを使用して結果を確認してください。

于 2012-06-23T10:33:46.357 に答える
0

メニューをクリックに反応させたい場合は、メニュー項目にリスナーを追加する必要があります。これにより、正しいパネルが表示されます。

于 2009-02-07T23:18:33.483 に答える