【问题标题】:Why I can't display this custom JFrame from a Main class?为什么我不能从 Main 类中显示这个自定义 JFrame?
【发布时间】:2014-12-25 17:18:19
【问题描述】:

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

我有以下 3 个课程:

1) 一个 Main 类,只显示一个 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 窗口。

为什么?我错过了什么?

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

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) 见The Use of Multiple JFrames, Good/Bad Practice? 在这种情况下,登录应该是模态的JDialogJOptionPane
  • 你不应该硬编码你的 JPanelWithBackground 的大小。使用您读入的图像的大小。

标签: java swing user-interface jframe


【解决方案1】:

有很多事情你应该改变。

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

无需将 loginFrame 设为静态。在你的 main 方法中让它成为一个变量——尽管它应该从你的使用登录框架的程序代码中调用。

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

【讨论】:

    【解决方案2】:

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

      setSize(INITAL_SIZE);
    

    【讨论】:

    • 设置框架的大小既没有必要也不是最佳选择。
    • @AndrewThompson,我知道,但是使用setSize OP 可以查看登录框架。
    • pack() 是那里需要的。如果有任何不建议尺寸的自定义组件(例如BGImagePanel),则该自定义组件应具有首选尺寸集(或覆盖)。
    • 如果您希望代码在进行更改时继续按预期工作,请不要调用 setSize。以后你会遇到问题。
    【解决方案3】:

    还有 setBounds 方法让你有 setSize 并且另外有 x,y 作为你的 Frame。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多