2013-05-16 65 views
0

好的我有一個尺寸爲1280x720的框架。我需要將它分開,並在左側創建一個1000x720和280x720的右側。面板現在是1000x720我需要再次將其分割爲1000x520頂部和1000x200底部。我已經嘗試了相當一段時間。如果您有任何鏈接可以幫助或分享您的體驗,這將非常有幫助。 所以它的3個領域:#1 1000x520。 #3 280x720 #2 1000x200我需要拆分我的面板2次,我不知道如何去做

import javax.swing.JFrame; 
import javax.swing.JSplitPane; 
import javax.swing.UIManager; 

public class GamePanel extends JFrame{ 


    private static final long serialVersionUID = 1L; 
    public JSplitPane secondSplit; 
    SplitTableHand splitTableHand = new SplitTableHand(); 
    ChatPanel chatPanel = new ChatPanel(); 


    public GamePanel() { 

     secondSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitTableHand, chatPanel); 

     secondSplit.setOneTouchExpandable(false); 
     secondSplit.setDividerLocation(1000); 
     this.setSize(1280, 720); 
     this.setResizable(false); 
     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     //this.pack(); 
     this.setVisible(true); 
     getContentPane().add(secondSplit); 



    } 
    public static void main(String args[]){ 
     try { 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     } catch (Exception evt) {} 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       GamePanel mainFrame = new GamePanel(); 

      } 
     }); 


    } 


} 

    import java.awt.Dimension; 

import javax.swing.JPanel; 
import javax.swing.JSplitPane; 


public class SplitTableHand extends JPanel{ 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    JSplitPane splitPane; 
    TablePanel tablePanel = new TablePanel(); 
    HandPanel handPanel = new HandPanel(); 

    public SplitTableHand() { 


     splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tablePanel, handPanel); 

     splitPane.setOneTouchExpandable(false); 
     splitPane.setDividerLocation(550); 
     splitPane.setPreferredSize(new Dimension(1000, 720)); 
     add(splitPane); 
     splitPane.setVisible(true); 
    } 


} 

    import java.awt.BorderLayout; 
import java.awt.Color; 

import javax.swing.JLabel; 
import javax.swing.JPanel; 


public class TablePanel extends JPanel{ 

    private static final long serialVersionUID = 1L; 
    private JLabel label = new JLabel("LABEL"); 

    public TablePanel() { 
     setLayout(new BorderLayout());// we shall use absolute positioning for this 
     this.setSize(1000, 520); 
     this.setPreferredSize(getSize()); 
     this.setBackground(new Color(100,100,100)) ; 
     this.add(label); 
     this.setVisible(true); 
    } 
} 

    import java.awt.Color; 

import javax.swing.JButton; 
import javax.swing.JPanel; 


public class HandPanel extends JPanel{ 

    private static final long serialVersionUID = 1L; 
    JButton button; 
    public HandPanel() { 
     //default layout is flowlayout 
     this.setSize(1000, 200); 
     this.setPreferredSize(getSize()); 
     this.setBackground(new Color(150,150,50)) ; 

     for(int i = 0 ; i < 20;i++){ 
      String name = "Button"+ i; 
      button = new JButton(name); 
      this.add(button); 
      this.setVisible(true); 
     } 
    } 

} 

    import java.awt.Color; 
import java.awt.FlowLayout; 



import javax.swing.JButton; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.JTextField; 


public class ChatPanel extends JPanel{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private JTextArea chatArea = new JTextArea(10, 30); 
    private JTextField chatField = new JTextField(50); 
    JScrollPane scrollPane = new JScrollPane(chatArea); 
    JButton button ; 

    public ChatPanel() { 
     setLayout(new FlowLayout()); 
     this.setSize(720, 280); 
     this.setPreferredSize(getSize()); 
     this.setBackground(new Color(50,50,50)) ; 
     chatArea.setEditable(false); 
     chatArea.setLineWrap(true); 
     chatArea.setVisible(true); 
     chatField.setVisible(true); 
     button = new JButton("Button"); 
     button.setEnabled(true); 
     button.setVisible(true); 

     this.add(scrollPane); 
     this.add(chatField); 
     this.setVisible(true); 
     this.add(button); 
    } 

} 
+2

做_not_集邊界上不可調整大小的容器中,[例如](http://stackoverflow.com/a/12532237/230513);而不是'pack()'並且最大化。 – trashgod

回答

1

首先你的程序是給編譯錯誤。修復它們。您必須延長SplitTableHandJPanel。有了這個,你的錯誤在GamePanel得到解決。

public class SplitTableHand extends JPanel 

第二點是在GamePanel類的構造函數,創建topPanel並將其添加到該幀,而不是添加secondSplit的。

GamePanel構造改爲

public GamePanel() { 
    secondSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitTableHand, chatPanel); 

    secondSplit.setOneTouchExpandable(true); 
    secondSplit.setDividerLocation(150); 
    this.setSize(1280, 720); 
    this.setResizable(false); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    //this.pack(); 
    this.setVisible(true); 
    getContentPane().add(secondSplit); 
} 

SplitTableHand類也,你必須添加splitPane

添加的聲明,它的構造add(splitPane);

+0

thnx我會嘗試一下吧:D – cotsios

+0

謝謝Ravindra,這對我有很大的幫助,我設置了正確的dividerLocation,並且它像我希望的那樣工作。雖然我無法看到面板上的文本區域或字段,但您是否知道爲什麼? – cotsios

+0

在'ChatPanel'類中,不是直接添加'chatArea',而是添加'scrollPane' –

相關問題