为什么这个Swing登录表单显示错误?

时间:2013-11-07 16:54:16

标签: java swing user-interface

我是Java Swing 的新手,我遇到了问题。

我必须创建一个登录窗口,从这样的immage(类似这样的东西,scilicet,windows必须显示2个文本字段,用户插入其用户名和密码以及执行登录操作的按钮)中获取灵感:< / p>

enter image description here

好的,我认为这很简单,我已经意识到以下课程:

package com.techub.crystalice.gui.login;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdesktop.application.SingleFrameApplication;

import com.techub.crystalice.gui.Constants;
import com.techub.crystalice.gui.GUI;

public class LoginFrame extends SingleFrameApplication {
    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        System.out.println("DENTRO: LoginFrame() ---> startup()");


        this.getMainFrame().setTitle("MyApplication Login");
        this.getMainFrame().setSize(600, 350);          // Setta le dimensioni del JFrame che rappresenta la finestra principale
        Container mainContainer = this.getMainFrame().getContentPane();     // Recupera l'oggetto Container del JFrame

        // Imposta un layput manager di tipo GridLayout per il contenitore principale: 3 righe ed una singola colonna:
        mainContainer.setLayout(new GridLayout(3,1));   

        // Contenitore rappresentato da 6 righe a singola colonna contenente i campi testuali e di input del login: 
        JPanel body = new JPanel();
        body.setLayout(new GridLayout(6, 1));
        body.add(new JLabel("Username"));

        JTextField userName = new JTextField();
        body.add(userName);

        body.add(new JLabel("Password"));
        JTextField password = new JTextField();
        body.add(password);

        this.getMainFrame().add(body);      // Aggiunge al JFrame principale il JPanel contenente il form di login


        show(this.getMainFrame());

        JPanel footer = new JPanel();
        footer.setLayout(new FlowLayout(FlowLayout.CENTER)); 

        JButton loginButton = new JButton("Login");
        footer.add(loginButton);

        this.getMainFrame().add(footer);    // Aggiunge al JFrame principale il JPanel contenente il bottone di login

    }

    public static void main(String[] args) {
        System.out.println("DENTRO: LoginFrame() ---> main()");
        launch(LoginFrame.class, args);
    }

}

此类使用名为 JDesktop 的litle框架,该框架涉及 startup()方法的定义,但这是纯Swing代码。唯一要说的是我通过这个代码行获得了主** JFrame **:

this.getMainFrame()

这个例子似乎有效,但我在登录表单可视化中遇到了一些美学问题,因为我得到了以下结果:

enter image description here

正如你所看到的,这是非常令人讨厌的,如果结构与示例相同,则会出现以下问题:

  1. JTextField 的高度太小
  2. 我的元素之间没有上边距,左边距,右边距
  3. 字体太小
  4. 我能以某种方式更正此错误吗?你能帮我提一些建议吗?我的窗户结构还可以吗?

    TNX

    安德烈

2 个答案:

答案 0 :(得分:1)

  1. this.getMainFrame().pack()应该用于将JFrame的大小设置为显示所有组件所需的最小大小。

  2. 我建议使用GridBagLayout,因为GridBagConstraints允许您指定Insets,这是组件之间的间距,也允许您正确定位组件。

  3. 使用java.awt.Font对象设置适当大小的所需字体。保留对您的两个JLabel的引用,然后使用setFont()设置正确的字体。

答案 1 :(得分:0)

GridLayout组件不支持托管组件首选大小(这是您希望拥有的)。在这种情况下的技巧是添加一个JPanel(它尊重首选大小)里面有一个JTextField,所以基本上就在你有

的时候

的JLabel
JTextField的
JLabel的
的JTextField

之后

JPanel(与JLabel合作)
JPanel(带JTextField)
JPanel(与JLabel合作)
JPanel(使用JTextField)