繁体   English   中英

Java-Jframe不显示按钮标签或文本字段

[英]Java - Jframe not displaying Buttons Labels or textfields

我是Java的新用户,但是当我尝试创建新框架时,我得到的只是一个打开的带有白色背景的新窗口,并且未添加任何按钮,文本框或标签。 我不知道我在做什么错?

private static void MainGui(){

    MainInterfaces MainGui = new MainInterfaces();

    MainGui.setTitle(name+ "'s Inbox");
    MainGui.setSize(600,600);
    MainGui.setVisible(true);
    MainGui.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

MainInterfaces类

class MainInterfaces extends JFrame implements ActionListener
{

    private JButton send;
    private JTextField to, subject, message;
    private JLabel toLbl, subjectLbl, messageLbl;


    public MainInterfaces() { 

        setLayout(new FlowLayout());
        toLbl = new JLabel("Recipient: ");
        subjectLbl = new JLabel("Subject: ");
        messageLbl = new JLabel("Message: ");

        to = new JTextField(32);
        subject = new JTextField(32);
        message = new JTextField(32);

        send = new JButton("SEND");

        add(toLbl);
        add(to);
        add(subjectLbl);
        add(subject);
        add(messageLbl);
        add(message);
        add(send);

    }

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

    }
}

您的代码无法编译,因为存在很多错误( 您没有main方法private static void MainGui(){没有意义,等等)。 尝试使用以下代码,该代码可以为我编译并打开GUI,并根据需要显示所有内容(按钮,标签等)

GUI工作

1)类MainGui.java

class MainGui{
    public static void main(String[] args) {

        MainInterfaces MainGui = new MainInterfaces();

        MainGui.setTitle("'s Inbox");
        MainGui.setSize(600,600);
        MainGui.setVisible(true);
        MainGui.setDefaultCloseOperation(MainGui.EXIT_ON_CLOSE);
    }
}

2)类MainInterfaces.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MainInterfaces extends JFrame implements ActionListener
{

    private JButton send;
    private JTextField to, subject, message;
    private JLabel toLbl, subjectLbl, messageLbl;


    public MainInterfaces() { 



        setLayout(new FlowLayout());
        toLbl = new JLabel("Recipient: ");
        subjectLbl = new JLabel("Subject: ");
        messageLbl = new JLabel("Message: ");

        to = new JTextField(32);
        subject = new JTextField(32);
        message = new JTextField(32);

        send = new JButton("SEND");

        add(toLbl);
        add(to);
        add(subjectLbl);
        add(subject);
        add(messageLbl);
        add(message);
        add(send);




    }

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

    }
}

我用一行代码修复了它。 只需将“ setVisible(true)”方法切换到MainInterface类内部即可。 但是,请确保将其放在构造函数中的最后一行代码中,否则将无法正常工作。 基本上,发生的事情是,在将框架本身设置为true的同时,随后又添加了组件,因此未显示它们。 最后放置“ setVisible(true)”可确保在显示框架时添加了所有组件。

附带说明,您可以在MainInterface中添加在MainGUI类中键入的所有方法。

这是MainGUI:

public class MainGUI 
    {

        public static void main(String[] args) 
        {
           new MainInterfaces();
        }

    }

这是MainInterfaces的样子:

public class MainInterfaces extends JFrame implements ActionListener
{
    private JButton send; 
    private JTextField to, subject, message; 
    private JLabel toLbl, subjectLbl, messageLbl; 
    public MainInterfaces() 
    { 
        setTitle("(Name)'s Inbox");
        setSize(600,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout()); 
        toLbl = new JLabel("Recipient: "); 
        subjectLbl = new JLabel("Subject: "); 
        messageLbl = new JLabel("Message: "); 
        to = new JTextField(32); 
        subject = new JTextField(32); 
        message = new JTextField(32); 
        send = new JButton("SEND"); 
        add(toLbl); add(to); 
        add(subjectLbl); 
        add(subject); 
        add(messageLbl); 
        add(message); 
        add(send); 
        setVisible(true);
    } 
    public void actionPerformed(ActionEvent e) 
    { 

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM