2013-02-17 49 views
5

我完全新的在Java中使用的圖形用戶界面,所以我有一點麻煩搞清楚如何使我需要的一切。我要在我的JFrame面板,我需要對齊(其中一個向左,一個向右)和我需要在面板爲中心的面板之一幾個按鈕。這是我的代碼。我該如何調整在JPanels/JFrames元素?

package application; 

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import java.io.*; 
import java.nio.*; 
import java.util.*; 

public class Main extends JPanel 
{ 
    public static void main(String[] args) 
    { 
     //set the ui to the native OS 
     try 
     { 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
     }catch(ClassNotFoundException | InstantiationException | IllegalAccessException 
     | UnsupportedLookAndFeelException e)         
     { 
     } 

     JFrame frame = new JFrame("Application Name"); 
     Menu menu = new Menu(); 
     JPanel iconPanel = new JPanel(); 
     final JPanel grid = new JPanel(new FlowLayout()); 
     JButton firewallButton = new JButton("Firewall"); 
     JButton networkButton = new JButton("Network"); 
     JButton printerButton = new JButton("Printer"); 

     int iconPanelSizeX; 
     int iconPanelSizeY; 
     int gridSizeX; 
     int gridSizeY; 
     int gridPosition; 

     //frame setting 
     frame.setSize(800, 600); 
     frame.setMinimumSize(new Dimension(800, 600)); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 

     //add grid and iconPanel JPanels to the frame 
     frame.add(iconPanel); 
     iconPanel.add(firewallButton); 
     iconPanel.add(networkButton); 
     iconPanel.add(printerButton); 
     frame.add(grid);   

     //iconPanel settings 
     iconPanel.setBorder(BorderFactory.createLoweredSoftBevelBorder()); 
     iconPanel.setBackground(Color.gray); 
     iconPanel.setLayout(new FlowLayout()); 
     iconPanel.setSize(new Dimension(100, 600)); 
     iconPanel.setVisible(true); 

     //grid setting 
     grid.setBackground(Color.red); 
     grid.setSize(new Dimension(700, 600)); 
     grid.setVisible(true); 

     //this is for resizing components when the user resizes the window 
     int counter = 0; 
     while(counter == 0) 
     { 
      firewallButton.setSize(new Dimension(iconPanel.getWidth(), 50)); 
      networkButton.setSize(new Dimension(iconPanel.getWidth(), 50)); 
      printerButton.setSize(new Dimension(iconPanel.getWidth(), 50)); 
      iconPanelSizeX = frame.getWidth()/10; 
      iconPanelSizeY = frame.getHeight(); 
      gridSizeX = (frame.getWidth()/10) * 9; 
      gridSizeY = frame.getHeight(); 
      iconPanel.setSize(new Dimension(iconPanelSizeX, iconPanelSizeY)); 
      grid.setSize(new Dimension(gridSizeX, gridSizeY)); 
     } 
    } 
} 

正如你可以看到,第二的JPanel(網格)不與幀的右側排隊,和內部iconTray的按鈕不能任一中心。我意識到這些都可能是簡單的佈局修復,但我不知道從哪裏開始。

回答

7

對於JFrame簡單的拆分可以使用GridLayout一個1行2個colums。

frame.setLayout(new GridLayout(1,2,3,3)); //3,3 are gaps 
frame.add(grid); 
frame.add(iconPanel); 

對於面板爲中心的組件,你可以使用FlowLayout這是默認設置上JPanels

做它manualy:

grid.setLayout(new FlowLayout()); //Centered components 

grid.setLayout(new FlowLayout(FlowLayout.LEFT,3,3)); //Components aligned to left 

grid.setLayout(new FlowLayout(FlowLayout.RIGHT,3,3)); //Components aligned to right 

這是它的外觀:

enter image description here

另外,很少觀察:

  • 永遠不要爲您的組件調用setXXXSize()方法;

  • 儘量避免撥打setSize();代替JFrame,改爲撥打pack();;

  • 在代碼結束時調用setVisible(true);;

你所有的龐大的代碼可以被「剝離」這樣的:

import javax.swing.*; 
import java.awt.*; 


public class Main extends JPanel 
{ 
    public static void main(String[] args) 
    { 
     JFrame frame = new JFrame("Application Name"); 
     JPanel iconPanel = new JPanel(); 
     JPanel grid = new JPanel(new FlowLayout()); 
     JButton firewallButton = new JButton("Firewall"); 
     JButton networkButton = new JButton("Network"); 
     JButton printerButton = new JButton("Printer"); 


     frame.add(iconPanel); 
     iconPanel.add(firewallButton); 
     iconPanel.add(networkButton); 
     iconPanel.add(printerButton); 
     grid.setBackground(Color.GREEN); 

     frame.setLayout(new GridLayout(1,2,3,3)); 
     frame.add(grid); 
     frame.add(iconPanel); 


     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+0

謝謝,這幫助了很多。實際上有一件事我忘了問,那就是如何垂直對齊按鈕 - 我必須稍後添加一些按鈕。 – user2067364 2013-02-17 23:07:33

+1

用於'pack()'的+1;更多[這裏](http://stackoverflow.com/a/14927280/230513)。 – trashgod 2013-02-17 23:21:01

+0

您將不得不使用其他佈局管理器。對於非常基本的對齊,你可以使用'GridLayout'或'BoxLayout',但對於更復雜的東西,我建議使用'GridBadLayout'或'MiGLayout'。只是谷歌。 – 2013-02-17 23:21:07

1

我要在我的JFrame面板,我需要對齊(其中一個向左, 一到右),我需要 在面板中心的面板之一幾個按鈕。這是我的代碼。

我知道這些都可能是簡單的佈局解決方法,但我沒有 線索從哪裏開始。

使用比您實際使用的簡單FlowLayout更復雜的佈局。我建議你使用

  • GridBagLayout
  • BoxLayout

檢查references here

2

我建議你花些時間去A Visual Guide to Layout Managers。這將幫助您熟悉標準API提供的佈局管理器。需要一些經驗和辛勤的工作才能弄清楚哪一個是正確的工具來獲得你想要的確切外觀。一旦您對Standard API提供的內容感到滿意,您還應該查看提供其他選項的第三方Layout Manager API。

5

如何垂直對齊按鈕?

這個例子中WEST區域框架的默認BorderLayout的使用垂直Box

enter image description here

import java.awt.*; 
import javax.swing.*; 

/** @see http://stackoverflow.com/a/14927280/230513 */ 
public class Main { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       display(); 
      } 
     }); 
    } 

    private static void display() throws HeadlessException { 
     JFrame frame = new JFrame("Application Name"); 
     JButton firewallButton = new JButton("Firewall"); 
     JButton networkButton = new JButton("Network"); 
     JButton printerButton = new JButton("Printer"); 

     //iconPanel settings 
     Box iconPanel = new Box(BoxLayout.Y_AXIS); 
     iconPanel.add(firewallButton); 
     iconPanel.add(networkButton); 
     iconPanel.add(printerButton); 
     iconPanel.setBackground(Color.gray); 
     iconPanel.setVisible(true); 
     frame.add(iconPanel, BorderLayout.WEST); 

     //grid setting 
     JPanel grid = new JPanel() { 

      @Override 
      // arbitrary placeholder size 
      public Dimension getPreferredSize() { 
       return new Dimension(320, 230); 
      } 
     }; 
     grid.setBackground(Color.red); 
     frame.add(grid, BorderLayout.CENTER); 

     //frame setting 
     frame.pack(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
+0

另請參閱[*初始線程*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)和此[問與答](http://stackoverflow.com/q/7229226/ 230513)。 – trashgod 2013-02-17 23:22:48

相關問題