2013-03-28 13 views
14

Layout wantedBir jFrame üzerinde çoklu paneller nasıl düzenlenir? (java)

Kendi java soket oyunumu oluşturma aşamasındayım. Oyunum, tam ekrana (burada "burada grafik çizimleri" yazıyor, fakat şu anda tüm jframe'e resim yapıyor) iyi resim yapıyor. Ben sadece metnini görüntülemek için bir kaydırma çubuğu ile bir metin kutusu eklemek, herhangi bir giriş ve kullanıcıdan metin girişleri almak için başka bir metin kutusu ve daha sonra sohbet amacıyla metni göndermek için bir düğme. Ama soruma dayanarak, bunu nasıl başlatabilirim ki? Anladığım kadarıyla bir plana ihtiyacım var, ama birisi bana yardım edebilir mi? (

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); 
    } 
} 

"Yeni Ekran başlangıç: İşte şu anda benim kodudur (bu kod yalnızca ben yukarıdaki resimde var gibi şimdi ekranı bölmek gerekir, şu anda bütün ekrana boyama kurar)) "- bu jüteli uzatır, temel olarak her şeyi grafikleri çizdiğim yer. Ayrıca, farklı panellere insanların eklendiğini gördüm ama aynı boyutta olmalarını sağlayamıyorum. Resimdeki gibi, "burada grafik boya" paneli en büyük olanıdır.

cevap

23

JPanel aslında yalnızca içine farklı öğeler koyabileceğiniz bir kaptır (diğer JPanels). Bu durumda, pencereniz için bir JPanel büyüklüğünü ana kapsayıcı olarak öneririm. Bu ana panel, gereksinimlerinize uygun bir Layout atarsınız (here is an introduction to the layouts).

Eğer boya panelini ve istediğiniz diğer JPanels ekleyebilir sizin ana panele düzenini ayarladıktan sonra (o metin ile olduğu gibi ..).

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); 

Bu sadece tüm alt paneller dikey (Y-ekseni) sıralar bir örnektir. Başka bir düzen (yatay düzen gibi) ile düzenlenmelidir mainPanel (belki bazı simgeler veya düğmeler) altındaki bazı diğer şeyler istiyorsanız, sadece yeniden tüm diğer şeyler için bir kap olarak yeni bir JPanel oluşturun ve setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS)'u ayarlayın.

Bulduğunuz gibi, yerleşimler oldukça sağlam ve panelleriniz için en iyi düzeni bulmak zor olabilir. Öyleyse pes etmeyin, girişi (yukarıdaki bağlantıyı) okuyun ve resimlere bakın - bunu nasıl yaparım :)

Ya da programınızı yazmak için NetBeans'i kullanabilirsiniz. Orada, her türlü Windows ve Çerçeveyi oluşturmak için oldukça kolay bir görsel editörünüz (sürükle ve bırak) var. (Sadece zor bazen ... sonradan is kodu anlayış.)

DÜZENLEME

bu soruya ilgilenen bazı birçok kişi olduğundan, bunu yapmak için bir JFrame düzeni nasıl tam bir örneğini sağlamak istiyordu OP'nin istediği gibi gözüküyor.

sınıf MyFramedenilen ve uzanır salıncaklar 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); 
      } 
     }); 
    } 
} 

bu sadece bir örnektir ve bir pencere düzeni için birden yaklaşımlar vardır lütfen unutmayın. Her şey sizin ihtiyaçlarınıza bağlıdır ve içeriğin yeniden boyutlandırılabilir/duyarlı olmasını istiyorsanız. Bir başka gerçekten iyi bir yaklaşım ise, oldukça karmaşık bir düzeni ele alabilen fakat öğrenmesi oldukça karmaşık olan GridBagLayout olacaktır.

1

İstediğiniz temel sonuçları elde etmenize yardımcı olacak bir dizi düzen yöneticisi kullanmak isteyeceksiniz.

Karşılaştırma için A Visual Guide to Layout Managers'a bakın.

Bir GridBagLayout kullanabilirsiniz, ancak bu JDK'da bulunan en karmaşık (ve güçlü) düzen yöneticilerinden biridir.

Bunun yerine bir dizi bileşik düzen yöneticisi kullanabilirsiniz.

ben CENTER grafik bileşeni ve SOUTH pozisyonunda metin alanına sahip bir BorderLayout kullanarak, tek JPanel üzerinde grafik bileşeni ve metin alanını yerleştirmek istiyorum.

Ben kullanarak ayrı JPanel üzerinde metin alanı ve düğme yerleştirmek istediğiniz bir GridBagLayout

Ben üzerine bu iki panelleri yer istiyorum (ben üzerinde istediğiniz sonucu elde etmek için aklınıza gelebilecek en basit çünkü) BorderLayout kullanarak, CENTER ilk panelde ve SOUTH konumunda ikinci, master, panel.

Ama bu downvote nedenine ışık tutacak beni

+0

Herhangi bir dilek, ben bir cevapta yükseltebilir öğrenmek isteriz – MadProgrammer

İlgili konular