2013-10-31 90 views
1

我在Java中並沒有真正與Swing合作。我正在試驗它。我想做一個尺寸不能改變的佈局佈局。我已經看到很多建議使用佈局管理器將多個Jpanel添加到Jframe中的事情。不過,我所看到的所有涉及佈局管理器的教程都表示,它允許用戶調整屏幕大小。我想要的佈局有一個沿着左側的矩形,沿着底部的一個薄矩形,以及佔據剩餘空間的第三個矩形。我嘗試使用絕對佈局,但它只是不想爲我工作。使用JPanels設置佈局

import java.awt.Color; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 


public class Test extends JFrame { 

    public Test() { 
     JPanel rect1 = new JPanel(); 
     rect1.setBounds(101, 650, 900, 50); 
     rect1.setBackground(Color.RED); 
     getContentPane().add(rect1); 

     JPanel rect2 = new JPanel(); 
     rect2.setBounds(0, 650, 100, 1000); 
     rect2.setBackground(Color.BLUE); 
     getContentPane().add(rect2); 

     JPanel rect3 = new JPanel(); 
     rect3.setBounds(101, 700, 900, 950); 
     rect3.setBackground(Color.GREEN); 
     getContentPane().add(rect3); 

     setTitle("TEST"); 
     setSize(1000, 700); 

     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE);   
    } 


    public static void main(String[] args) { 

     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       Test ex = new Test(); 
       ex.setVisible(true); 
      } 
     }); 
    } 
} 

有人可以幫助我在這種佈局(所有不同的顏色)在Jframe中正確地製造三個Jpanels嗎?

回答

2

您可以使用setResizable()。請參考下面的代碼片段。

setResizable(false); // this will not allow resizing 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
1

AbsoluteLayout是不行。不要這樣做。您可能正在尋找BorderLayout。查看教程:How to Use BorderLayout瞭解詳情。如果你不想讓你的JFrame能夠重新使用frame.setResizable(false);就可以了。

3

您可以使用BorderLayout作爲基準或甚至GridBagLayout來實現相同的目的。

你缺少的主要部件是佈局管理器使用(或可以根據佈局管理器使用)組件的首選/最小/最大尺寸

基本的事實,你會做什麼是定義一個自定義組件(從JPanel延伸)並覆蓋它的getPreferredSize方法並返回所需的值。根據佈局管理器的不同,您也可能需要覆蓋getMaximumSizegetMinimumSize方法。

例如...

基本上,這顯示了 「默認」 的大小,什麼時候調整屏幕大小會發生......

enter image description hereenter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class FixedSizeLayout { 

    public static void main(String[] args) { 
     new FixedSizeLayout(); 
    } 

    public FixedSizeLayout() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.gridx = 0; 
      gbc.gridy = 0; 

      add(new ContentPane(), gbc); 
      gbc.gridx++; 
      add(new LeftPane(), gbc); 
      gbc.gridwidth = 2; 
      gbc.gridx = 0; 
      gbc.gridy = 1; 
      add(new BottomPane(), gbc); 

     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

    } 

    public class ContentPane extends JPanel { 

     public ContentPane() { 
      setBackground(Color.GREEN); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(150, 150); 
     } 

     @Override 
     public Dimension getMinimumSize() { 
      return getPreferredSize(); 
     } 

     @Override 
     public Dimension getMaximumSize() { 
      return getPreferredSize(); 
     } 

    } 

    public class BottomPane extends JPanel { 

     public BottomPane() { 
      setBackground(Color.RED); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 50); 
     } 

     @Override 
     public Dimension getMinimumSize() { 
      return getPreferredSize(); 
     } 

     @Override 
     public Dimension getMaximumSize() { 
      return getPreferredSize(); 
     } 

    } 

    public class LeftPane extends JPanel { 

     public LeftPane() { 
      setBackground(Color.BLUE); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(50, 150); 
     } 

     @Override 
     public Dimension getMinimumSize() { 
      return getPreferredSize(); 
     } 

     @Override 
     public Dimension getMaximumSize() { 
      return getPreferredSize(); 
     } 

    } 
} 

現在,如果你更喜歡,你可以讓屏幕不可調整大小,但我一個人不會喜歡你。我更喜歡使用佈局管理器的強大功能,並允許用戶就他們想如何查看內容做出決定......我可以......但這只是我(我不喜歡除了一些對話框的情況)

+0

回答我1問題。你什麼時候睡覺 ? – Sage

+0

我是一個18個月的驕傲的父親,我不睡覺...永遠...... – MadProgrammer

+0

現在,我真的感到難過,因爲我不能發佈這樣的問題,我可以upvote並接受答案。 :P。無論如何挑戰接受。 :| :)) – Sage