2013-03-28 25 views
14

Layout wantedLàm cách nào để bố trí nhiều bảng trên khung hình jFrame? (java)

Tôi đang trong quá trình tạo trò chơi java socket của riêng mình. Trò chơi của tôi được vẽ hoàn toàn với màn hình đầy đủ (nơi nó nói "vẽ đồ họa ở đây", nhưng im vẽ cho toàn bộ khung hình tại thời điểm này). Tôi muốn thêm một hộp văn bản có thanh cuộn để hiển thị văn bản chỉ, không nhập bất kỳ đầu vào và hộp văn bản nào khác để nhập văn bản từ người dùng và sau đó là nút để gửi văn bản, cho mục đích trò chuyện. Nhưng vào câu hỏi của tôi, làm thế nào để tôi thậm chí bắt đầu đặt nó ra? Tôi hiểu tôi cần bố cục, nhưng ai đó có thể giúp tôi về điều này? Đây là mã của tôi tại thời điểm này (mã này chỉ thiết lập bức tranh cho toàn bộ màn hình vào lúc này, cần phải chia màn hình ngay bây giờ như tôi có trong hình trên):

public class Setup extends JFrame implements Runnable{ 
    JPanel panel; 
    JFrame window; 
    public Setup(Starter start, JFrame window){ 
     window.setSize(600,500); 
     window.setLocationRelativeTo(null); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setResizable(false); 
     panel = new Display(start); 
     this.window = window; 
    } 
    public void run(){ 
     window.getContentPane().add(panel); 
     window.setBackground(Color.BLACK); 
     window.setVisible(true); 
    } 
} 

"new Display (start) "- điều này mở rộng jpanel, về cơ bản, nơi tôi vẽ mọi thứ đồ họa thông minh.

Ngoài ra, tôi đã thấy mọi người thêm vào các bảng khác nhau nhưng tôi không thể có chúng có cùng kích thước. Giống như trong hình, bảng điều khiển "vẽ đồ họa ở đây" là bảng lớn nhất, v.v.

Trả lời

23

JPanel thực sự chỉ là vùng chứa nơi bạn có thể đặt các thành phần khác nhau trong đó (thậm chí JPanels) khác. Vì vậy, trong trường hợp của bạn, tôi sẽ đề xuất một số lớn JPanel như một số loại vùng chứa chính cho cửa sổ của bạn. Điều đó bảng điều khiển chính bạn chỉ định một Layout phù hợp với nhu cầu của bạn (here is an introduction to the layouts).

Sau khi bạn thiết lập cách bố trí để bảng điều khiển chính của bạn bạn có thể thêm bảng sơn và JPanels khác mà bạn muốn (như những người có văn bản trong nó ..).

JPanel mainPanel = new JPanel(); 
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); 

    JPanel paintPanel = new JPanel(); 
    JPanel textPanel = new JPanel(); 

    mainPanel.add(paintPanel); 
    mainPanel.add(textPanel); 

Đây chỉ là một ví dụ mà sắp xếp tất cả phụ tấm theo chiều dọc (Y-Axis). Vì vậy, nếu bạn muốn một số nội dung khác ở dưới cùng của mainPanel (có thể một số biểu tượng hoặc nút) cần được sắp xếp với bố cục khác (như bố cục ngang), chỉ cần tạo lại một JPanel mới làm công cụ chứa tất cả các nội dung khác và đặt setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS).

Như bạn sẽ thấy, bố cục khá cứng nhắc và khó có thể tìm bố cục tốt nhất cho các bảng của bạn. Vì vậy, không bỏ cuộc, đọc phần giới thiệu (liên kết ở trên) và xem các hình ảnh - đây là cách tôi làm điều đó :)

Hoặc bạn chỉ có thể sử dụng NetBeans để viết chương trình của mình. Ở đó bạn có một trình soạn thảo trực quan khá dễ dàng (kéo và thả) để tạo tất cả các loại Windows và Khung. (Chỉ hiểu mã sau đó là ... phức tạp đôi khi.)

EDIT

Vì có một số nhiều người quan tâm đến câu hỏi này, tôi muốn cung cấp một ví dụ đầy đủ về cách bố trí một JFrame để làm cho nó trông giống như OP muốn nó.

Các lớp được gọi là MyFrame và kéo dài đu JFrame

public class MyFrame extends javax.swing.JFrame{ 

    // these are the components we need. 
    private final JSplitPane splitPane; // split the window in top and bottom 
    private final JPanel topPanel;  // container panel for the top 
    private final JPanel bottomPanel; // container panel for the bottom 
    private final JScrollPane scrollPane; // makes the text scrollable 
    private final JTextArea textArea;  // the text 
    private final JPanel inputPanel;  // under the text a container for all the input elements 
    private final JTextField textField; // a textField for the text the user inputs 
    private final JButton button;   // and a "send" button 

    public MyFrame(){ 

     // first, lets create the containers: 
     // the splitPane devides the window in two components (here: top and bottom) 
     // users can then move the devider and decide how much of the top component 
     // and how much of the bottom component they want to see. 
     splitPane = new JSplitPane(); 

     topPanel = new JPanel();   // our top component 
     bottomPanel = new JPanel();  // our bottom component 

     // in our bottom panel we want the text area and the input components 
     scrollPane = new JScrollPane(); // this scrollPane is used to make the text area scrollable 
     textArea = new JTextArea();  // this text area will be put inside the scrollPane 

     // the input components will be put in a separate panel 
     inputPanel = new JPanel(); 
     textField = new JTextField(); // first the input field where the user can type his text 
     button = new JButton("send"); // and a button at the right, to send the text 

     // now lets define the default size of our window and its layout: 
     setPreferredSize(new Dimension(400, 400));  // let's open the window with a default size of 400x400 pixels 
     // the contentPane is the container that holds all our components 
     getContentPane().setLayout(new GridLayout()); // the default GridLayout is like a grid with 1 column and 1 row, 
     // we only add one element to the window itself 
     getContentPane().add(splitPane);    // due to the GridLayout, our splitPane will now fill the whole window 

     // let's configure our splitPane: 
     splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT); // we want it to split the window verticaly 
     splitPane.setDividerLocation(200);     // the initial position of the divider is 200 (our window is 400 pixels high) 
     splitPane.setTopComponent(topPanel);     // at the top we want our "topPanel" 
     splitPane.setBottomComponent(bottomPanel);   // and at the bottom we want our "bottomPanel" 

     // our topPanel doesn't need anymore for this example. Whatever you want it to contain, you can add it here 
     bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS)); // BoxLayout.Y_AXIS will arrange the content vertically 

     bottomPanel.add(scrollPane);    // first we add the scrollPane to the bottomPanel, so it is at the top 
     scrollPane.setViewportView(textArea);  // the scrollPane should make the textArea scrollable, so we define the viewport 
     bottomPanel.add(inputPanel);    // then we add the inputPanel to the bottomPanel, so it under the scrollPane/textArea 

     // let's set the maximum size of the inputPanel, so it doesn't get too big when the user resizes the window 
     inputPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 75));  // we set the max height to 75 and the max width to (almost) unlimited 
     inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS)); // X_Axis will arrange the content horizontally 

     inputPanel.add(textField);  // left will be the textField 
     inputPanel.add(button);   // and right the "send" button 

     pack(); // calling pack() at the end, will ensure that every layout and size we just defined gets applied before the stuff becomes visible 
    } 

    public static void main(String args[]){ 
     EventQueue.invokeLater(new Runnable(){ 
      @Override 
      public void run(){ 
       new MyFrame().setVisible(true); 
      } 
     }); 
    } 
} 

Xin lưu ý rằng đây chỉ là một ví dụ và có nhiều cách tiếp cận để bố trí một cửa sổ. Tất cả phụ thuộc vào nhu cầu của bạn và nếu bạn muốn nội dung có thể thay đổi kích thước/đáp ứng. Một cách tiếp cận thực sự tốt sẽ là GridBagLayout có thể xử lý bố cục khá phức tạp, nhưng cũng khá phức tạp để tìm hiểu.

1

Bạn sẽ muốn sử dụng một số trình quản lý bố cục để giúp bạn đạt được các kết quả cơ bản mà bạn muốn.

Khám phá A Visual Guide to Layout Managers để so sánh.

Bạn có thể sử dụng GridBagLayout nhưng đó là một trong những trình quản lý bố cục phức tạp nhất (và mạnh mẽ) có sẵn trong JDK.

Thay vào đó, bạn có thể sử dụng một loạt trình quản lý bố cục phức hợp.

Tôi muốn đặt các thành phần đồ họa và vùng văn bản trên một đơn JPanel, sử dụng một BorderLayout, với các thành phần đồ họa trong CENTER và vùng văn bản ở vị trí SOUTH.

Tôi muốn đặt trường văn bản và nút trên riêng JPanel sử dụng một GridBagLayout (vì đó là đơn giản nhất tôi có thể nghĩ ra để đạt được kết quả trên bạn muốn)

tôi muốn đặt hai tấm này lên một thứ ba, chính, bảng điều khiển, sử dụng một số BorderLayout, với bảng đầu tiên trong số CENTER và bảng thứ hai ở vị trí SOUTH.

Nhưng đó là tôi

+0

Bất kỳ ai muốn làm sáng tỏ lý do cho phiếu giảm giá, tôi rất muốn tìm hiểu cách người ta có thể cải thiện câu trả lời – MadProgrammer

Các vấn đề liên quan