为什么我无法从Main类显示此自定义JFrame?

时间:2013-11-14 15:12:48

标签: java swing user-interface jframe

我是Java Swing 开发的新手,我遇到以下问题

我有以下3个班级:

1)主要类只显示 LoginFrame 类:

package com.test.login4;

import javax.swing.JFrame;


public class Main {

    private static final LoginFrame loginFrame = new LoginFrame();

    //private static final GUI gui = new GUI();

    public static void main(String[] args) {
          System.out.println("Main ---> main()");   

          loginFrame.setVisible(true);


    }

}

2)然后我有 LoginFrame 类,它只是扩展了一个经典的Swing JFrame ,它显示了一个用户可以插入用户名和密码的登录表单:

package com.test.login4;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

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



public class LoginFrame extends JFrame implements ActionListener {

    private static final int FIXED_WIDTH = 550;
    private static final Dimension INITAL_SIZE = new Dimension(FIXED_WIDTH, 230);

    private boolean loginResult = true;

    public LoginFrame() {

        System.out.println("Inside LoginFrame ---> LoginFrame()");

        this.setTitle("XCloud Login");

        this.setPreferredSize(INITAL_SIZE);
        this.setResizable(false);

        Container mainContainer = this.getContentPane(); // main Container into
                                                            // the main JFrame

        // JPanel creation and settings of the MigLayout on it:
        // JPanel externalPanel = new JPanel();
        JPanelWithBackground externalPanel = null;

        try {
            // externalPanel = new JPanelWithBackground("resources/logo.png");
            externalPanel = new JPanelWithBackground("src/com/test/resources/logo.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        externalPanel.setLayout(new net.miginfocom.swing.MigLayout("fill"));

        externalPanel.add(new JLabel("Username"), "w 50%, wrap");
        JTextField userNameTextField = new JTextField(20);

        externalPanel.add(userNameTextField, "w 90%, wrap");

        externalPanel.add(new JLabel("Password"), "w 50%, wrap");
        JTextField pswdTextField = new JTextField(20);
        externalPanel.add(pswdTextField, "w 90%, wrap");

        JButton loginButton = new JButton("Login");
        loginButton.setActionCommand("loginAction");
        loginButton.addActionListener(this);

        externalPanel.add(loginButton, "w 25%, wrap");

        mainContainer.add(externalPanel);
        // mainFrame.add(mainContainer);
        // loginFrame.setVisible(true);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

}

3)此类使用 JPanelWithBackground 对象来获得背景图像

package com.test.login4;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class JPanelWithBackground extends JPanel {

      private Image backgroundImage;

      // Some code to initialize the background image.
      // Here, we use the constructor to load the image. This
      // can vary depending on the use case of the panel.
      public JPanelWithBackground(String fileName) throws IOException {
        backgroundImage = ImageIO.read(new File(fileName));
      }

      public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Draw the background image.
        //g.drawImage(backgroundImage, 0, 0, this);
        g.drawImage(backgroundImage, 0, 0, 550, 230, this);
      }
    }

我的问题是,当我执行 Main 类时,它在我看来是我的Java应用程序的图标(在Ubuntu栏上),但我无法显示 LoginFrame < / strong>窗口。

为什么呢?我错过了什么?

我尝试创建并显示一个经典的JFrame而不是我的LoginFrame(在Main类中),我没有问题。

3 个答案:

答案 0 :(得分:2)

你应该改变一些事情。

在调用loginFrame.setVisible()之前调用loginFrame.pack()应该允许它正确调整大小并显示。

无需使loginFrame成为静态。使它成为main方法中的变量 - 虽然它应该从使用登录框架的程序代码中调用。

LoginFrame应该扩展JDialog而不是JFrame。除非您使用不同大小的多个显示器,否则您应该只在程序中使用JFrame。任何应该放在主程序之上的东西都可以成为JDialog。

答案 1 :(得分:1)

方法this.setPreferredSize(INITAL_SIZE);没有达到预期效果。在setSize构造函数上调用LoginFrame方法。

  setSize(INITAL_SIZE);

答案 2 :(得分:0)

并且setBounds方法使你拥有setSize,另外还有你的Frame的x,y。