-1

是否可以将带有 JButtons 的 JPanel 添加到拆分 JPanel?现在,我将带有 JButton 的 JPanel 添加到 JFrame,但我希望它与其他 JPanel 一起放在 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
    }
}
4

1 回答 1

5

你问:

是否可以将带有 JButtons 的 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,但我希望它与其他 JPanel 一起放在 JPanel 上。当我尝试这样做时,我得到一个带有分隔符的完全空白的 JPanel。

然后您的代码中有错误,但不幸的是,根据您发布的代码,我怀疑我们中的任何人都可以做的不仅仅是猜测。如果您需要更具体的帮助,那么您需要发布一个可运行的小型示例来演示您的问题,即sscce

于 2013-07-08T22:59:28.783 回答