2014-12-05 81 views
1

我是Swing的新手,我試圖用一些文本框和按鈕創建一個帶日誌的屏幕。當我運行它,我得到一個空的屏幕,但一旦我調整窗口,它開始工作......有誰知道爲什麼?Swing登錄面板

代碼:

package ui_view; 
import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JButton; 
import javax.swing.JTextField; 

public class InlogView extends JFrame{ 

    GridBagConstraints constraints; 
    JPanel p = new JPanel(); 
    JPanel bp = new JPanel(); 
    JPanel img = new JPanel(); 
    JButton inlogButton; 
    JButton cancelButton; 
    JLabel gebruikersNaam; 
    JTextField gebruikersInvoer; 
    JLabel wachtwoord; 
    JTextField wachtwoordInvoer; 
    BufferedImage image; 
    JLabel picLabel; 

    public InlogView(){ 
     super("inlog"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(370,300); 
     add(img, BorderLayout.PAGE_START); 
     add(p, BorderLayout.CENTER); 
     add(bp, BorderLayout.PAGE_END); 
     setVisible(true); 
     setResizable(true); 
    } 

    public void maakInlogView() throws IOException{ 
     p.setLayout(new GridBagLayout()); 
     constraints = new GridBagConstraints(); 
     constraints.fill = GridBagConstraints.HORIZONTAL; 

     gebruikersNaam = new JLabel("gebruikersnaam: "); 
     changeConstraints(1,1,0,0); 
     p.add(gebruikersNaam, getConstraints()); 

     gebruikersInvoer = new JTextField(10); 
     changeConstraints(1,1,1,0); 
     p.add(gebruikersInvoer, getConstraints()); 

     wachtwoord = new JLabel("wachtwoord: "); 
     changeConstraints(1,1,0,1); 
     p.add(wachtwoord, getConstraints()); 

     wachtwoordInvoer = new JTextField(10); 
     changeConstraints(1,1,1,1); 
     p.add(wachtwoordInvoer, getConstraints()); 

     inlogButton = new JButton("Aanmelden"); 
     cancelButton = new JButton("Cancel"); 

     bp.add(inlogButton); 
     bp.add(cancelButton); 

     image = ImageIO.read(new File("doc/14_1.png")); 
     picLabel = new JLabel(new ImageIcon(image)); 
     img.add(picLabel); 

    } 

    protected void changeConstraints(int height, int width, int gridx, int gridy) { 
     constraints.gridheight = height; 
     constraints.gridwidth = width; 
     constraints.gridx = gridx; 
     constraints.gridy = gridy; 
    } 

    protected GridBagConstraints getConstraints() { 
     return constraints; 
    } 


    public static void main(String args[]) throws IOException{ 
     InlogView inlog = new InlogView(); 
     inlog.maakInlogView(); 

    } 
} 

回答

2

呼叫setVisible(true)maakInlogView()後添加的validate()而不是在構造函數中。

0

,你必須在JFrame中調用的validate(),或在你的情況的JPanel

剛剛的setResizable(真)

+0

謝謝,它的工作原理! – 2014-12-05 11:33:13

+0

很高興我可以幫助:) – 2014-12-05 11:33:40

+2

@陳琛:不,這不是正確的解決方案。解決方案就像伊恩說的那樣,在將所有組件添加到窗口之後調用'setVisible(true)'**。最好是在InlogView類構造函數內調用'maakInlogView()',然後調用'setVisible(true)'。 – 2014-12-05 12:21:39