2014-03-03 80 views
1

我試圖將JLabel對齊到JPanel中。我添加了一個JTabbedPane,一個包含我的JLabel和JTextArea的JPanel到一個主JPanel。JPanel中的JLabel對齊

我已經搜索過,並嘗試了一些方法,如setAlignmentX,setHorizontalAlignment(SwingConstants.LEFT)和嵌套容器無濟於事。

這裏是我的代碼:

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

class LabelProblem 

{ 

public static void main(String[] args) 

{ 

    JFrame frame = new JFrame("Label Problem"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

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

    JPanel ComponentPanel = new JPanel(); 
    JLabel label = new JLabel("Sample Text"); 
    label.setHorizontalAlignment(SwingConstants.LEFT); 
    label.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    label.setAlignmentX(Component.RIGHT_ALIGNMENT); 

    ComponentPanel.add(label); 

    JTabbedPane Tab = new JTabbedPane(); 
    Tab.add("Document 1", new JPanel()); 

    Main.add(Tab); 
    Main.add(ComponentPanel); 

    JTextArea Area = new JTextArea(10,10); 
    JScrollPane Scroll = new JScrollPane(Area); 

    frame.add(Main); 
    frame.add(Scroll, BorderLayout.SOUTH); 
    frame.setSize(450,450); 
    frame.setVisible(true); 

} 

} 

我該如何調整我的JLabel右邊?

謝謝!

+0

無關,但你是不是在此時,相應的線程中執行代碼,請使用'SwingUtilities.invokeLater(...)'爲主。請參閱Swing的[hello world](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/start/HelloWorldSwingProject/src /start/HelloWorldSwing.java) – DSquare

+0

@DSquare謝謝你指出。 – mundomug

回答

1

因此,該標籤的位置由ComponentPanel的佈局決定。由於您沒有指定任何佈局,因此它使用默認的FlowLayoutCENTER對齊。假設你沒有問題FlowLayout這是一個簡單的問題設置LEFT的對齊,因爲這是可能的這種佈局。

下面是修復的代碼,但我懷疑當你把更多的元素添加到ComponentPanel時,你會想要使用另一個佈局,因爲FlowLayout對於菜單等更適合,而不是顯示主要內容。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.FlowLayout; 

import javax.swing.BorderFactory; 
import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTabbedPane; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 

class LabelProblem 
{ 

    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       initGUI(); 
      } 
     }); 
    } 

    public static void initGUI() 
    { 

     JFrame frame = new JFrame("Label Problem"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

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

     JPanel componentPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 
     JLabel label = new JLabel("Sample Text"); 
     label.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 

     componentPanel.add(label); 

     JTabbedPane Tab = new JTabbedPane(); 
     Tab.add("Document 1", new JPanel()); 

     main.add(Tab); 
     main.add(componentPanel); 

     JTextArea area = new JTextArea(10, 10); 
     JScrollPane scroll = new JScrollPane(area); 

     frame.add(main); 
     frame.add(scroll, BorderLayout.SOUTH); 
     frame.setSize(450, 450); 
     frame.setVisible(true); 
    } 
} 

結果:

Result

注:我也改變了變量名稱遵循java style convention:變量名稱應該以小寫開始從clases名differenciate他們,在大寫開始。

+0

+1用於利用默認佈局對齊;這個問題令人困惑,左右對齊。 – trashgod

+0

@DSquare感謝您解決我的代碼!我試圖改變對齊setAlignmentX(Component.RIGHT_ALIGNMENT),但它沒有奏效。你能告訴我爲什麼它不起作用嗎? – mundomug

+0

@mundomug我的理解是,容器的LayoutManager負責確定其中所有組件的大小和位置。 AlignmentX/Y是組件的一個屬性,取決於使用該屬性的佈局管理器。 [BoxLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html)確實使用它,但現在我能想到的所有其他人都忽略它並使用它們自己的規則和屬性以確定對齊。另外,當不使用絕對不推薦的佈局管理器(空佈局)時可能會很有用。 – DSquare

1

我認爲這可能是你的問題,而不是實際設置你認爲你正在設置佈局的佈局。

你有一個JPanel,它帶有一個垂直方向的BoxLayout(Main),它封閉了另一個默認佈局(ComponentPanel)的JPanel,最後封閉你的標籤。您的標籤無法推到正確位置的原因是因爲被推到正確的內的封閉容器。如果你在ComponentPanel周圍設置了彩色邊框,你會明白我的意思 - 它只佔用與JLabel相同的空間量,使JLabel無處可移動。

您需要爲您的中間ComponentPanel設置佈局和約束,允許它水平地填充其父容器,以便標籤有一定的位置。

你還沒有真正指定你的佈局應該如何看,但是如果你把Main上的佈局改成X_AXIS,你的標籤會彈出到左邊(就像它的父容器一樣)。不知道你真的想做什麼,我不能說更多。

但是,我會建議您將BoxLayout完全扔掉,並使用GridBagLayout進行調查,從而爲您提供對用戶界面的高級控制。 GridBagLayout不是最簡潔的構造,但這是控制的價格。

+0

既不膠水也不Box.createRigidArea()的作品。 – mundomug

+0

優秀的解釋! – mundomug

1

一個簡單的方法是在構造函數中將標籤的horizontalAlignment設置爲JLabel.RIGHT

image

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

class LabelProblem { 

    public static void main(String[] args) { 
     JFrame frame = new JFrame("Label Problem"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new GridLayout(0, 1)); 

     JTabbedPane tab = new JTabbedPane(); 
     tab.add("Document 1", new JPanel()); 
     frame.add(tab); 

     JLabel label = new JLabel("Sample Text", JLabel.RIGHT); 
     frame.add(label); 

     JTextArea area = new JTextArea(10, 10); 
     JScrollPane scroll = new JScrollPane(area); 
     frame.add(scroll); 

     frame.pack(); 
     frame.setSize(450, 450); 
     frame.setVisible(true); 
    } 
} 
+0

謝謝你的幫助! – mundomug