简体   繁体   中英

not displaying buttons and labels

I am a bit of stuck. I am supposed to display 2 buttons and 1 label but my program is no displaying them. If you have any ideas it would be great. I am using eclipse and the code is compiling and runnning. Here is the code.

/** This is the driver class of the program.
 * Here is the main method with the JFrame.
 * class name RunFurniture.class
 * @author Kiril Anastasov
 * @date 18/03/2012
 */
import java.awt.*;
import javax.swing.*;
public class RunRurniture 
{

    /**
     * @param args
     */
    public static void main(String[] args) 
    {

        JFrame application = new JFrame();
        PanelFurniture panel = new PanelFurniture();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(600,450);
        application.setLocationByPlatform(true);
        application.setVisible(true);

    }

}

/** Here is the GUI of the program.
 * class name PanelFurniture.class
 * @author Kiril Anastasov
 * @date 18/03/2012
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class PanelFurniture extends JPanel implements ActionListener
{

    JLabel label;
    JButton a,b;
    JPanel centerPanel, eastPanel;

    public void actionPerformed(ActionEvent ae)
    {
        this.setLayout(new BorderLayout());

        centerPanel = new JPanel();
        //centerPanel.setLayout();
        this.add(centerPanel, BorderLayout.CENTER);

        eastPanel = new JPanel(new GridLayout(1,3));
        label = new JLabel();
        b = new JButton("Move");
        eastPanel.add(b);
        eastPanel.add(label);
        a = new JButton("ee");
        eastPanel.add(a);

         //this.add(eastPanel);
        this.add(eastPanel, BorderLayout.EAST);
    }
}

You only add the components to the panel when in actionPerformed, and you never register it as a listener, so it will never be displayed. instead, add them in the constructor / init method:

public class PanelFurniture extends JPanel
{

    JLabel label;
    JButton a,b;
    JPanel centerPanel, eastPanel;

    public void init()
    {
        this.setLayout(new BorderLayout());

        centerPanel = new JPanel();
        //centerPanel.setLayout();

        this.add(centerPanel, BorderLayout.CENTER);


        eastPanel = new JPanel(new GridLayout(1,3));

        label = new JLabel();
        b = new JButton("Move");
        eastPanel.add(b);
        eastPanel.add(label);
        a = new JButton("ee");
        eastPanel.add(a);

        this.add(eastPanel, BorderLayout.EAST);
    }
}

and call init() after creating the panel:

PanelFurniture panel = new PanelFurniture();
panel.init();
application.add(panel);

Also, make sure the UI is constructor on the EDT:

SwingUtils.invokeLater(new Runnable() {
    @Override
    public void run() {
        JFrame application = new JFrame();
        PanelFurniture panel = new PanelFurniture();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(600,450);
        application.setLocationByPlatform(true);
        application.setVisible(true);
    }
});

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