简体   繁体   中英

Java - Jframe not displaying Buttons Labels or textfields

im new to java but when I try to create a new frame all I get is a new window open with a white background on and none of the buttons or text boxes or labels get added. I don't know what im doing wrong?

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

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

    }
}

Your code doesn't compile because you have a lot of errors ( you have no main method , private static void MainGui(){ makes no sense etc.). Try this code instead which compiles and opens the GUI fine for me, showing everything as you want to (the buttons, the labels, etc.) :

GUI工作

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

    }
}

I fixed it with one line of code. Just switch the "setVisible(true)" method to being inside the MainInterface class. However, make sure you put it as the last line of code inside the constructor, or it won't work. Basically, what was happening is that while the frame itself was being set to be true, the components were being added afterwards and were consequently not being shown. Putting "setVisible(true)" last makes sure all the components are added when you display the frame.

As a side note, you can add all methods you typed in the MainGUI class inside of MainInterface.

Here's MainGUI:

public class MainGUI 
    {

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

    }

Here's what MainInterfaces looks likes:

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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