简体   繁体   中英

I'm attempting to create a small GUI application, but the contents of the JFrame are not showing on screen. How can I fix my code?

As of late I've been developing a (very) small GUI application in Java. I'm extremely new to Swing and Java in general, but up until now I have been able to get everything to work the way I want it to. However, after cleaning up my code, when I run the program nothing but the border of the window appears. What am I doing wrong and how can I fix my code? Thanks ahead of time!

For the sake of saving space I've made Pastebin links to all of my classes (besides Main).

Main Class

package me.n3rdfall.ezserver.main;

public class Main {

    public static GUI g = new GUI();

    public static void main(String[] args) {

        g.showWindow(800, 500);

    }

}

GUI Class

http://pastebin.com/gDMipdp1

ButtonListener Class

http://pastebin.com/4XXm70AD

EDIT: It appears that calling removeAll() directly on 'frame' actually removed essential things other than what I had added. By calling removeAll() on getContentPane(), the issue was resolved.

Quick hack: Remove the removeAll() functions.

public void homePage() {
//      frame.removeAll();
//      mainpanel.removeAll();
//      topbar.removeAll();

I'm not sure what you're trying to achieve, but that will at least show some items. If I were you I would rebuild this GUI by extending JFrame. It will make your code a little easier to read.

I also think what you are trying to achieve with the buttons is to switch layouts, you can do this in an easier way by using CardLayout

Example (has nothing to do with your code, but to demonstrate):

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Example extends JFrame implements ActionListener {
    private JButton leftButton;
    private JButton rightButton;

    private CardLayout cardLayout = new CardLayout();
    JPanel cards = new JPanel(cardLayout);
    final static String LEFTPANEL = "LEFTPANEL";
    final static String RIGHTPANEL = "RIGHTPANEL";
    JPanel card1;
    JPanel card2;

    public Example()    {
        JPanel topPanel = new JPanel();
        addButtons(topPanel);

        add(topPanel, BorderLayout.NORTH);
        add(cards, BorderLayout.CENTER);

     //Initiates the card panels

        initCards();

        setTitle("My Window");
        setSize(300, 300);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void initCards() {
        card1 = new JPanel();
        card2 = new JPanel();
        card1.setBackground(Color.black);
        card2.setBackground(Color.red);

        cards.add(card1, LEFTPANEL);
        cards.add(card2, RIGHTPANEL);
    }

    private void addButtons(Container con)  {
        leftButton = new JButton("Left Button");
        leftButton.addActionListener(this);
        rightButton = new JButton("Right Button");
        rightButton.addActionListener(this);

        con.add(leftButton, BorderLayout.WEST);
        con.add(rightButton, BorderLayout.EAST);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource().equals(leftButton))    {
            //Change cardlayout
            cardLayout.show(cards, LEFTPANEL);      
        } else if(e.getSource().equals(rightButton))    {
            //Change cardlayout
            cardLayout.show(cards, RIGHTPANEL);     
        }
    }

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

}

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