3

I am working on this program, using swing. Every time I export the program, and run it, the GUI I atempt to set up doesn't appear. The JFrame does, but not the inner components. Thanks in advance ~Airis

Code:

import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Login {
    public static void login_Interface(){

        //Start GUI style//
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            e1.printStackTrace();
        }
        //      End     //

        JFrame login_Frame = new JFrame("Login - LetsMeet");
        login_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        login_Frame.setSize(750, 650);
        login_Frame.setResizable(true);

        JPanel panel_Title = new JPanel();                            //PANEL Title
        panel_Title.setBounds(0, 0, 750, 150);
        panel_Title.setLayout(null);
        Image logo = null;
        try {
            logo = ImageIO.read(new File("Data/images/logo_letsmeet.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Graphics2D logo_out = ((BufferedImage) logo).createGraphics();
        panel_Title.paint(logo_out);

        JPanel login_Panel = new JPanel();                            //LOGIN Panel
        login_Panel.setBounds(0, 150, 350, 150);
        login_Panel.setLayout(null);
        JTextField username_login = new JTextField("Username");
        username_login.setBounds(100, 50, 100, 25);
        JPasswordField password_login = new JPasswordField();
        password_login.setBounds(200, 50, 100, 25);
        JButton login_go = new JButton("Login");
        login_go.setBounds(200, 50, 100, 25);
        login_Panel.add(password_login);
        login_Panel.add(username_login);


        JPanel panel_Divider = new JPanel();                          //PANEL Divider
        login_Panel.setBounds(350, 150, 50, 150);
        panel_Divider.setSize(50, 100);
        panel_Divider.setLayout(null);
        Image sep = null;
        try {
            sep = ImageIO.read(new File("Data/images/sep.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Graphics2D div = ((BufferedImage) sep).createGraphics();
        panel_Title.paint(div);

        JPanel register_Panel = new JPanel();                         //REGISTER Panel
        register_Panel.setBounds(400, 150, 350, 150);
        register_Panel.setLayout(null);

        login_Frame.add(panel_Title);
        login_Frame.add(login_Panel);
        login_Frame.add(panel_Divider);
        login_Frame.add(register_Panel);
        login_Frame.setVisible(true);
    }
}

Errors: None

4

3 回答 3

8

首先...

panel_Title.paint(logo_out);这不是图形在 Swing 中的工作方式……也不是图像在 Swing 中的绘制方式

第二...

您应该使用布局管理器,它们将大大减少潜在问题并降低代码的复杂性。

第三

您的应用程序应该在事件调度线程的上下文中启动...

public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            // Construct your UI here...
        }
    });
}

第四

正如安德鲁还指出的那样(+1)......您的图像“似乎”是内部资源,无法通过File参考访问。您需要以不同的方式加载这些(嵌入式)资源......

logo = ImageIO.read(Login.class.getResource("/Data/images/logo_letsmeet.png"));

您还忽略了这些资源的潜力,null这是一种非常危险的做法。

我建议你通读

于 2012-12-11T03:13:42.713 回答
4

除了@MadProgammer 提出的所有建议外,您还需要将控件添加到JFrame内容窗格,如下所示:

login_Frame.getContentPane().add(panel_Title);
login_Frame.getContentPane().add(login_Panel);
...

然后你的控件应该出现

更新:

运行您的实际代码,并向容器 ( JPanels) 添加彩色边框,我得到以下信息:

panel_Title.setBorder(BorderFactory.createLineBorder(Color.BLUE));
login_Panel.setBorder(BorderFactory.createLineBorder(Color.RED));
panel_Divider.setBorder(BorderFactory.createLineBorder(Color.GREEN));
register_Panel.setBorder(BorderFactory.createLineBorder(Color.YELLOW));

在此处输入图像描述

基本上你的代码有布局配置问题。再次,遵循@MadProgammer 的建议。您将来可以使用此边框技巧来调试您的布局

于 2012-12-11T03:17:23.297 回答
1
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Login {
    public static void login_Interface(){

        //Start GUI style//
        try {
                 UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e1) {
             e1.printStackTrace();
        } catch (InstantiationException e1) {
             e1.printStackTrace();
        } catch (IllegalAccessException e1) {
             e1.printStackTrace();
         } catch (UnsupportedLookAndFeelException e1) {
              e1.printStackTrace();
         }
          //      End     //

          JFrame login_Frame = new JFrame("Login - LetsMeet");
          login_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         login_Frame.setSize(750, 650);
          login_Frame.setResizable(true);

         JPanel panel_Title = new JPanel();                            //PANEL Title
         panel_Title.setBounds(0, 0, 750, 150);
         panel_Title.setLayout(null);
         Image logo = null;
         try {
              logo = ImageIO.read(new File("Data/images/logo_letsmeet.png"));
          } catch (IOException e) {
              e.printStackTrace();
         }
         Graphics2D logo_out = ((BufferedImage) logo).createGraphics();
         panel_Title.paint(logo_out);

           JPanel login_Panel = new JPanel();                            //LOGIN Panel
          login_Panel.setBounds(0, 150, 350, 150);
          login_Panel.setLayout(null);
          JTextField username_login = new JTextField("Username");
    username_login.setBounds(100, 50, 100, 25);
    JPasswordField password_login = new JPasswordField();
    password_login.setBounds(200, 50, 100, 25);
    JButton login_go = new JButton("Login");
    login_go.setBounds(200, 50, 100, 25);
    login_Panel.add(password_login);
    login_Panel.add(username_login);


    JPanel panel_Divider = new JPanel();                          //PANEL Divider
    login_Panel.setBounds(350, 150, 50, 150);
    panel_Divider.setSize(50, 100);
    panel_Divider.setLayout(null);
    Image sep = null;
    try {
        sep = ImageIO.read(new File("Data/images/sep.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    Graphics2D div = ((BufferedImage) sep).createGraphics();
    panel_Title.paint(div);

    JPanel register_Panel = new JPanel();                         //REGISTER Panel
    register_Panel.setBounds(400, 150, 350, 150);
    register_Panel.setLayout(null);

    login_Frame.add(panel_Title);
    login_Frame.add(login_Panel);
    login_Frame.add(panel_Divider);
    login_Frame.add(register_Panel);
    login_Frame.setVisible(true);
}

}

您没有在此程序中设置任何布局管理器。这就是为什么你不能显示任何东西

于 2012-12-11T12:35:40.573 回答