简体   繁体   中英

My JPanel won't show. Eclipse says I have no errors. Java

I am trying to display a JPanel in a JFrame. The Jrame works but I can't get the JPanel to display.

My whole class spent an hour on this today. Teacher included. Not luck. Eclipse says there are no errors Can someone please alert me to my mistake?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// make a JFrame and bits

public class MySystemGUI extends JFrame implements ActionListener
{
    private static final long serialVersionUID = 1L;
    private JFrame myFrame;
    private JTextField LLName, LLAddress, LLPhone, LLbankDeets;
    private JButton sub1;
    private JLabel LLNameT, LLAddressT, LLPhoneT, LLbankDeetsT;

    private JPanel LLJP()
    {
        JPanel JP1 = new JPanel();

        LLNameT = new JLabel ("Enter Landlord name");
        LLName = new JTextField(30);
        LLAddressT = new JLabel ("Enter Landlord Address ");
        LLAddress = new JTextField(40);
        LLPhoneT = new JLabel ("Enter Landlod Phone No.");
        LLPhone = new JTextField(10);
        LLbankDeetsT = new JLabel ("Enter Landlod Bank details");
        LLbankDeets = new JTextField(10);
        sub1 = new JButton("Submit"); 

        JP1.add(LLNameT);
        JP1.add(LLName); 
        JP1.add(LLAddressT);
        JP1.add(LLAddress); 
        JP1.add(LLPhoneT);
        JP1.add(LLPhone); 
        JP1.add(LLbankDeetsT );
        JP1.add(LLbankDeets); 
        JP1.add(sub1);

        //myFrame.add(JP1 );

        return JP1;
    }

    // Set up frame 

    public MySystemGUI()
    {   
        myFrame = new JFrame ();
        JPanel myPanel = LLJP(); 
        myFrame.add(myPanel,"South");
        this.setLayout(new GridBagLayout());
        this.setSize(700, 500);
        this.setTitle("My System GUI");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBackground(Color.blue);  
    }

    //run this bitch
    public static void main (String[] args)
    { 
        new MySystemGUI();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {

    }
}

You have an JFrame member

private JFrame myFrame;

which is what you are adding the components to.

myFrame.add(myPanel,"South");

But you setVisible to class frame

public class MySystemGUI extends JFrame
   ...
   this.setVisible(true);   // `this` is class frame, not `myFrame`

Take out all the this.setXxx and do myFrame.setXxx , and take out the extends JFrame

public class MySystemGUI implements ActionListener {
    ...
    public MySystemGUI() {
       ...
       myFrame.setLayout(new GridBagLayout());
       myFrame.setSize(700, 500);
       myFrame.setTitle("My System GUI");
       myFrame.setVisible(true);
       ...
    }
}

Other Notes

  • myFrame.add(myPanel,"South"); - "South" doesn't matter, only for BorderLayout. You set the layout to GridBaglayout.

  • this.setBackground(Color.blue); Wouldn't you want o set the background after the frame is visible?

  • Swing Programs should be run/started on the Event Dispatch Thread. See Initial Threads . Basically, you can wrap the instantiation in a SwingUtilities.invokeLater

     public static void main (String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { new MySystemGUI(); } }); } 

UPDATE

You're also going to want to make use of different layout managers for the panel holding the labels and field. With a FlowLayout (the default of JPanel), eveerytyhing is added consecutively in a row. Take some time to learn the layout managers at Laying out Components Withing a Container

Here is a simple fix using a GridLayout

import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class MySystemGUI implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JFrame myFrame;
    private JTextField LLName, LLAddress, LLPhone, LLbankDeets;
    private JButton sub1;
    private JLabel LLNameT, LLAddressT, LLPhoneT, LLbankDeetsT;

    private JPanel LLJP() {

        JPanel JP1 = new JPanel(new GridLayout(0, 2));

        LLNameT = new JLabel("Enter Landlord name");
        LLName = new JTextField(30);
        LLAddressT = new JLabel("Enter Landlord Address ");
        LLAddress = new JTextField(40);
        LLPhoneT = new JLabel("Enter Landlod Phone No.");
        LLPhone = new JTextField(10);
        LLbankDeetsT = new JLabel("Enter Landlod Bank details");
        LLbankDeets = new JTextField(10);
        sub1 = new JButton("Submit");

        JP1.add(LLNameT);
        JP1.add(LLName);
        JP1.add(LLAddressT);
        JP1.add(LLAddress);
        JP1.add(LLPhoneT);
        JP1.add(LLPhone);
        JP1.add(LLbankDeetsT);
        JP1.add(LLbankDeets);
        JP1.add(sub1);

        return JP1;

    }

    public MySystemGUI() {
        myFrame = new JFrame();
        JPanel myPanel = LLJP();
        myFrame.add(myPanel);
        myFrame.setTitle("My System GUI");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setBackground(Color.blue);
        myFrame.pack();
        myFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MySystemGUI();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e){}
}

You have a local frame hidding your class which is actually a JFrame :

public class MySystemGUI extends JFrame implements ActionListener {   // class signature
    ...
    public MySystemGUI() {
        ... 
        myFrame = new JFrame (); // local variable
        ...
        myFrame.add(myPanel,"South");
        ...
    }
    ...
}

Your panel is added to this local frame instead to your class so it won't be visible. You can either get rid of this local variable and add panel using this.add(...) or avoid extending JFrame and use local variables instead. This last apporach is preferred:

My whole class spent an hour on this today. Teacher included. Not luck.

Just find another teacher.

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