簡體   English   中英

另一個JPanel中的JPanel中的JButtons

[英]JButtons in JPanel in another JPanel

是否可以將具有JButton的JPanel添加到拆分JPanel? 現在,我將帶有JButton的JPanel添加到JFrame中,但是我希望將其與其他JPanels一起放在JPanel上。 當我嘗試執行此操作時,我得到一個帶有分隔符的完全空白的JPanel。

______________________________________________________________________________
public class Panel extends JPanel implements Runnable, ActionListener {

public Panel(){
    JFrame frame = new JFrame();
        ctsMenu = new JPanel(new GridLayout(2,2));
        ctsMenu.setPreferredSize(new Dimension(500,500));



                for (int iRows = 0; iRows < 2 ; iRows++){
                for (int iColumns = 0; iColumns < 2; iColumns++){
                    sGrid[iRows][iColumns] = new JButton ("("+iRows+","+iColumns+")");
                    ctsMenu.add(sGrid[iRows][iColumns]);
                    sGrid[iRows][iColumns].addActionListener(this);
                panel.add(ctsMenu);
                }
            }

            sGrid[0][0].setText("A");
            sGrid[0][1].setText("B");
            sGrid[1][0].setText("C");
            sGrid[1][1].setText("D");

            frame.setContentPane(panel);
            frame.pack();
            frame.setVisible(true);
}}
____________________________________________________________________
public MainFrame()
    {

            setTitle( "Split Pane Application" );
            setBackground( Color.GREEN );

            JPanel topPanel = new JPanel();
            topPanel.setLayout( new BorderLayout() );
            getContentPane().add( topPanel );


            createPanel1();
            createPanel2();
            createPanel3();
            createPanel4();


            splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
            topPanel.add( splitPaneV, BorderLayout.CENTER );
            splitPaneV.setDividerLocation(300);
            splitPaneV.setLeftComponent( gamePanel);
            //  gamePanel.addKeyListener(new KeyListener());
            gamePanel.setFocusable(true);
            gamePanel.requestFocusInWindow();
            splitPaneV.setRightComponent( panel3 );
        }
    }

    public void createPanel1(){
    // deleted to take up less space
    }

    public void createPanel2(){
    // deleted to take up less space
    }
    public void createPanel3(){
    panel3 = new Panel();   
    }

    public void createPanel4(){
        //deleted to take up less space
    }
}

你問:

是否可以將具有JButton的JPanel添加到拆分JPanel?

是的,絕對有可能這樣做。 這類似於我們一直在做的事情:

import javax.swing.*;

public class SplitPaneEg {
   private static void createAndShowGui() {
      JPanel panel1 = new JPanel();
      panel1.setBorder(BorderFactory.createTitledBorder("Panel 1"));
      panel1.add(new JButton("Button 1"));
      panel1.add(new JButton("Button 2"));

      JPanel panel2 = new JPanel();
      panel2.setBorder(BorderFactory.createTitledBorder("Panel 2"));
      panel2.add(new JButton("Button 1"));
      panel2.add(new JButton("Button 2"));

      JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel1,
            panel2);

      JFrame frame = new JFrame("Split Pane Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(splitPane);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

您聲明:

現在,我將帶有JButton的JPanel添加到JFrame中,但是我希望將其與其他JPanels一起放在JPanel上。 當我嘗試執行此操作時,我得到一個帶有分隔符的完全空白的JPanel。

然后,您的代碼有錯誤,但是不幸的是,根據您發布的代碼,我懷疑我們中的任何人都可以做的不只是猜測。 如果您需要更多具體的幫助,那么您將需要發布一個小的可運行示例來說明您的問題,即sscce

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM