2013-07-08 134 views
-1

是否有可能將JButton中的JPanel添加到Split JPanel中?現在,我已經將JPanel與JButton添加到JFrame中,但我希望它與其他JPanel一起使用JPanel。當我嘗試這樣做時,我會得到一個完全空白的帶分隔符的JPanel。另一個JPanel中的JPanel中的JButton

______________________________________________________________________________ 
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

你問:

是否有可能加入一個JPanel與Jbutton將要拆分的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 JPanels。當我嘗試這樣做時,我會得到一個完全空白的帶分隔符的JPanel。

然後你的代碼有錯誤,但不幸的是,根據你發佈的代碼,我懷疑我們中的任何人都可以做更多的事情,而不僅僅是猜測。如果您需要更具體的幫助,那麼您需要發佈一個可運行的小例子來演示您的問題,sscce