2013-02-17 87 views
5

Tôi hoàn toàn mới sử dụng GUI trong java, vì vậy tôi gặp một chút khó khăn trong việc tìm ra cách sắp xếp mọi thứ mà tôi cần. Tôi phải đặt trong JFrame của tôi mà tôi cần phải căn chỉnh (Một bên trái, một bên phải) và một vài nút trong một trong các bảng mà tôi cần phải được căn giữa trong bảng điều khiển. Đây là mã của tôi.Làm cách nào để căn chỉnh các phần tử trong 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)); 
     } 
    } 
} 

Như bạn có thể thấy, JPanel thứ hai (lưới) không thẳng hàng với cạnh bên phải của khung và các nút bên trong biểu tượngTray không tập trung vào. Tôi nhận ra đây là cả hai bản sửa lỗi bố cục đơn giản, nhưng tôi không biết phải bắt đầu từ đâu.

Trả lời

7

Đối với tách đơn giản của JFrame bạn có thể sử dụng GridLayout với 1 hàng và 2 colums.

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

Đối với trung các thành phần trong tấm bạn có thể sử dụng FlowLayout đó là bởi bộ mặc định trên JPanels:

Làm nó 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 

Đây là cách nó trông:

enter image description here

Ngoài ra, ít quan sát:

  • Không bao giờ gọi phương thức setXXXSize() cho các thành phần của bạn;

  • Cố gắng tránh gọi số setSize(); cho JFrame, gọi số pack(); thay thế;

  • Gọi setVisible(true); ở cuối mã;

Tất cả các mã lớn của bạn có thể bị "tước" này:

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

Cảm ơn, điều này đã giúp rất nhiều. Có một điều tôi quên hỏi, và đó là cách sắp xếp các nút theo chiều dọc - tôi phải thêm một số sau. – user2067364

+1

+1 cho 'gói()'; thêm [ở đây] (http://stackoverflow.com/a/14927280/230513). – trashgod

+0

Bạn sẽ phải sử dụng một số trình quản lý bố cục khác. Đối với căn chỉnh rất cơ bản, bạn có thể sử dụng 'GridLayout' hoặc' BoxLayout', nhưng đối với một cái gì đó phức tạp hơn, tôi đề nghị sử dụng 'GridBadLayout' hoặc' MiGLayout'. Chỉ cần google đó. –

1

tôi phải tấm trong JFrame của tôi rằng tôi cần phải sắp xếp (Một sang bên trái, một bên phải) và một vài nút tại một trong những tấm mà tôi cần để được tập trung trong bảng điều khiển. Đây là mã của tôi.

Tôi nhận thấy đây có thể là cả hai cách sửa lỗi bố cục đơn giản, nhưng tôi không có đầu mối bắt đầu từ đâu.

Sử dụng bố cục phức tạp hơn đơn giản FlowLayout mà bạn thực sự đang sử dụng. Tôi đề nghị để bạn sử dụng

  • GridBagLayout
  • BoxLayout

Kiểm tra references here

2

tôi đề nghị bạn dành chút thời gian đi qua A Visual Guide to Layout Managers. Điều này sẽ giúp bạn làm quen với các trình quản lý bố cục có sẵn với API chuẩn. Phải mất một số kinh nghiệm và công việc khó khăn để tìm ra cái nào trong số này là công cụ thích hợp để có được cái nhìn chính xác mà bạn muốn. Khi bạn cảm thấy thoải mái với những gì có sẵn từ API chuẩn, bạn cũng nên xem xét các API trình quản lý bố cục của bên thứ ba cung cấp các tùy chọn khác.

5

cách căn chỉnh các nút theo chiều dọc?

Ví dụ này sử dụng một dọc Box trong khu vực WEST của mặc định của khung BorderLayout:

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

Xem thêm [* Chủ đề ban đầu *] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) và [Hỏi đáp] này (http://stackoverflow.com/q/7229226/ 230513). – trashgod

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