2013-05-21 51 views
3

我正在嘗試創建一個類似Facebook的登錄頁面,但是我寫的代碼並未在左上方顯示它,而是顯示在頁面中心,我正在使用GridBagLayout並在FIRST_LINE_END上設置文本。將網頁右上角的組件對齊到GridBagLayout管理器

final JFrame f2=new JFrame("Admin Main"); 
        f2.setSize(1350,730); 
        f2.setVisible(true); 
        f1.setVisible(false); 
        f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);![enter image description here][2] 
        GridBagLayout gbl=new GridBagLayout(); 
        final JPanel p2=new JPanel(gbl){ 
     private Image img = ImageIO.read(new File("F:\\Untitled Folder\\Rohan\\football2.jpg")); 
     @Override 
          protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 

       g.drawImage(img, 0,0,1366,730, null); 
       } 
     };; 
        GridBagConstraints g2=new GridBagConstraints(); 
        g2.insets=new Insets(3,3,3,3); 
        JLabel l2=new JLabel("Admin ID",JLabel.LEFT); 
        JLabel l3=new JLabel("Password",JLabel.LEFT); 
        l2.setFont(new Font("TimesRoman",Font.BOLD,16)); 
        l2.setForeground(Color.BLUE); 
        l2.setBackground(Color.WHITE); 
        l3.setFont(new Font("TimesRoman",Font.BOLD,16)); 
        l3.setForeground(Color.BLUE); 
        l3.setBackground(Color.WHITE); 
        final JTextField t1=new JTextField(15); 
        final JPasswordField pw1=new JPasswordField(15); 
        JButton b3=new JButton("Back"); 
        JButton b4=new JButton("Sign in"); 
        f2.add(p2); 
        g2.anchor=GridBagConstraints.FIRST_LINE_END; 
        g2.gridx=1; 
        g2.gridy=1; 
        p2.add(l2,g2); 
        g2.gridx=2; 
        g2.gridy=1; 
        p2.add(t1,g2); 
        g2.gridx=1; 
        g2.gridy=2; 
        p2.add(l3,g2); 
        g2.gridx=2; 
        g2.gridy=2; 
        p2.add(pw1,g2); 
        g2.gridx=1; 
        g2.gridy=3; 
        p2.add(b3,g2); 
        g2.gridx=2; 
        g2.gridy=3; 
        p2.add(b4,g2); 

enter image description here

+0

請點擊到GridBagLayout的標籤,然後通過最新的順序,我敢肯定,第一,第二頁包含similair代碼,並在[SSCCE(http://sscce.org/)形式 – mKorbel

回答

7

我看到你當前佈局的兩個問題:

  1. 你把你的登錄面板,在父BorderLayout中心(這樣你的面板被拉伸到其容器的大小)
  2. 您的GridBagLayout中沒有任何東西可以將您的組件「推」到頂部和左側。

有幾個選擇這裏:(使用BorderLayout的,一旦與約束北部和第二次與約束WEST例如)

  1. 您巢登錄面板分成幾個小組。
  2. 將您的loginPanel添加到WEST,並在GridBagLayout的底部添加「填充」組件,以便將其他組件添加到頂部。

這裏是第二個解決方案的演示:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.FlowLayout; 
import java.awt.Font; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.swing.Box; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class TestLoginGridBagLayout { 

    protected void initUI() throws MalformedURLException { 
     JFrame frame = new JFrame("Admin Main"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JLabel background = new JLabel(new ImageIcon(new URL(
       "http://media1.santabanta.com/full1/Football/Football%20Abstract/football-abstract-6a.jpg"))) { 
      @Override 
      public Dimension getPreferredSize() { 
       Dimension preferredSize = super.getPreferredSize(); 
       Dimension layoutPreferredSize = super.preferredSize(); 
       preferredSize.width = Math.max(preferredSize.width, layoutPreferredSize.width); 
       preferredSize.height = Math.max(preferredSize.height, layoutPreferredSize.height); 
       return preferredSize; 
      } 
     }; 
     background.setLayout(new BorderLayout()); 
     frame.add(background); 
     GridBagLayout gbl = new GridBagLayout(); 
     final JPanel loginPanel = new JPanel(gbl); 
     loginPanel.setOpaque(false); 
     background.add(loginPanel, BorderLayout.WEST); 
     JLabel adminIDLabel = new JLabel("Admin ID", JLabel.LEFT); 
     JLabel passwordLabel = new JLabel("Password", JLabel.LEFT); 
     adminIDLabel.setFont(new Font("TimesRoman", Font.BOLD, 16)); 
     adminIDLabel.setForeground(Color.BLUE); 
     adminIDLabel.setBackground(Color.WHITE); 
     passwordLabel.setFont(new Font("TimesRoman", Font.BOLD, 16)); 
     passwordLabel.setForeground(Color.BLUE); 
     passwordLabel.setBackground(Color.WHITE); 
     final JTextField adminID = new JTextField(15); 
     final JPasswordField password = new JPasswordField(15); 
     JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING)); 
     buttonPanel.setOpaque(false); 
     JButton back = new JButton("Back"); 
     JButton signIn = new JButton("Sign in"); 
     buttonPanel.add(back); 
     buttonPanel.add(signIn); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.insets = new Insets(3, 3, 3, 3); 
     gbc.anchor = GridBagConstraints.FIRST_LINE_END; 
     loginPanel.add(adminIDLabel, gbc); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 
     loginPanel.add(adminID, gbc); 
     gbc.gridwidth = 1; 
     loginPanel.add(passwordLabel, gbc); 
     gbc.gridwidth = GridBagConstraints.REMAINDER; 
     loginPanel.add(password, gbc); 
     loginPanel.add(buttonPanel, gbc); 
     GridBagConstraints gbcFiller = new GridBagConstraints(); 
     gbcFiller.weightx = 1.0; 
     gbcFiller.weighty = 1.0; 
     gbcFiller.fill = GridBagConstraints.BOTH; 
     loginPanel.add(Box.createGlue(), gbcFiller); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        new TestLoginGridBagLayout().initUI(); 
       } catch (MalformedURLException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

} 

我也參加了的自由:

  • 重命名你的變量爲有意義的名稱(它使你的代碼更易於閱讀其他)
  • 將您的自定義背景圖片面板替換爲JLabel
  • 將您的按鈕移動到與另一個嵌套面板LayoutManager
  • 拍另一個背景圖像,因爲我沒有你的。
+0

thanxs很多先生.. –

+0

謝謝,基本上增加Box.createGlue()weighty = 1的伎倆 – Tires

0

如果你希望它明確地在左上角我寧願使用西北錨。 FIRST_LINE_END從左向右流動的文本也會將文本置於右側,其他方向則從右向左。

此外,開關gridx和網格開始於而不是。並根據需要從那裏增加。

因此,例如:

g2.anchor=GridBagConstraints.NORTHWEST; 
g2.gridx=0; 
g2.gridy=0; 
p2.add(l2,g2); 
g2.gridx=1; 
g2.gridy=0; 
p2.add(t1,g2); 
/*And so on for the rest of the block*/ 
+0

使用'爲gridx /實際上不建議使用gridy。使用'gridwidth/gridheight'是一種更好的方法(使用絕對值(1,2,3,...)或相對值)(例如'GridBagConstraints。REMAINDER')。無論如何,將anchord設置爲'NORTHWEST'並不能解決問題。 –

+0

沒有效果先生,它仍然在屏幕中間..我使用Netbeans我不認爲這是因爲.. –

+0

然後它將如何解決先生? @Guillaume Polet –

0

使用FIRST_LINE_START錨值。

g2.anchor=GridBagConstraints.FIRST_LINE_START; 
+0

這不會解決他的問題 –

+0

沒有效果,使用FIRST_LINE_START –