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

时间:2015-03-10 18:45:32

标签: java swing user-interface

我是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

    }
}

2 个答案:

答案 0 :(得分:2)

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

GUI Working

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)Class 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

    }
}

答案 1 :(得分:1)

我用一行代码修复它。只需将“setVisible(true)”方法切换为MainInterface类。但是,请确保将其作为构造函数中的最后一行代码,否则将无法正常工作。基本上,正在发生的事情是,虽然框架本身被设置为真,但组件之后被添加,因此没有被显示。将“setVisible(true)”设置为last会确保在显示框架时添加所有组件。

作为旁注,您可以添加在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) 
    { 

    }

}