简体   繁体   中英

JFrame isn't showing buttons or text

Currently my JFrame isn't showing any buttons, text, etc. I've looked at other topics, and one of the reasons for the error was because the setVisable method was called before everything was created. As you can see this isn't the issue here as it's called after everything is created. My goal for this is for the bottom panel to have a launch button, and when clicked a JLabel takes its place. (This is my first swing application..)

My issue is that the launch JButton, and bottomPanel aren't showing.

This is where the problem occurs, in the client class

private void createPanel() {
     bottomPanel = new JPanel(new BorderLayout());
     Launch = new JButton(Settings.launch);
     Loading = new JLabel(Settings.loaderIcon);
     Launch.addActionListener(this);
 }

 private void addPanel() {
     setTitle("RuneRebellionV2 Launcher");
     setContentPane(new JLabel(Settings.background));
     getContentPane().add(bottomPanel, BorderLayout.SOUTH);
     bottomPanel.add(new JLabel("Launcher, release " + Settings.version), BorderLayout.WEST);
     bottomPanel.setBackground(Color.black);
     bottomPanel.add(Launch, BorderLayout.EAST);
     Launch.setPreferredSize(new Dimension(120, 40));
     setSize(Settings.width, Settings.height);
     add(bottomPanel);
 }

And this is the rest of the system it works like this.. You launch initiate.

package com.RBV2;

import java.io.IOException;

import com.RBV2.engine.Client;
/*
 * Author David
 * 
 */
public class Initiate {
    public static void main(String[] args) {
        try {
            Settings.init();
            Client.main(args);
            System.out.println("Client started. -Alpha");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

That runs Settings.java

package com.RBV2;

import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;

/*
 * Author David
 * 
 */

public class Settings {
    public static String version = "1.0.0";
    public static String saveDirectory = System.getProperty("user.home")+"/";

    public static ImageIcon launch;
    public static ImageIcon icon1;
    public static ImageIcon icon2;
    public static Icon loaderIcon;
    public static ImageIcon slideshow;
    public static ImageIcon background;

    public static int width = 800, height = 480;

    public static String downloadUrl = "http://www.runerebellion.com/RuneRebellion.jar";
    public static String fileName = "RuneRebellion.jar";
    public static String serverName = "RuneRebellion";

    public static void init() {//Have to host files online
        try {
            URL background = new URL("http://www.runerebellion.com/clientImages/background.png");
            Settings.background = new ImageIcon(background);
            URL slideshow = new URL("http://www.runerebellion.com/clientImages/launch.png");//temp
            Settings.slideshow = new ImageIcon(slideshow);
            URL loaderIcon = new URL("http://www.runerebellion.com/clientImages/loader.gif");
            Settings.loaderIcon = new ImageIcon(loaderIcon);
            URL icon2 = new URL("http://www.runerebellion.com/clientImages/testcommunity.png");
            Settings.icon2 = new ImageIcon(icon2);
            URL icon1 = new URL("http://www.runerebellion.com/clientImages/community.png");
            Settings.icon1 = new ImageIcon(icon1);
            URL launch = new URL("http://www.runerebellion.com/clientImages/launch.png");
            Settings.launch = new ImageIcon(launch);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

Which then this class uses settings to make the JFrame.

package com.RBV2.engine;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;

import com.RBV2.Settings;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import java.io.*;
/*
 * Author Jon & David
 * 
 */
@SuppressWarnings("serial")
public class Client extends JFrame implements ActionListener {
    private JPanel bottomPanel;
    private JButton Launch;
    private JLabel Loading;
    //private JLabel Loading = new JLabel(Settings.loaderIcon);

    protected boolean updated = false;

    public void refresh() {
        setSize(Settings.width - 1, Settings.height - 1);
        setSize(Settings.width, Settings.height);
    }

    private void createLayout() {
        createPanel();
        addPanel();
        setVisible(true);
    }

     private void createPanel() {
         bottomPanel = new JPanel(new BorderLayout());
         Launch = new JButton(Settings.launch);
         Loading = new JLabel(Settings.loaderIcon);
         Launch.addActionListener(this);
     }

     private void addPanel() {
         setTitle("RuneRebellionV2 Launcher");
         setContentPane(new JLabel(Settings.background));
         getContentPane().add(bottomPanel, BorderLayout.SOUTH);
         bottomPanel.add(new JLabel("Launcher, release " + Settings.version), BorderLayout.WEST);
         bottomPanel.setBackground(Color.black);
         bottomPanel.add(Launch, BorderLayout.EAST);
         Launch.setPreferredSize(new Dimension(120, 40));
         setSize(Settings.width, Settings.height);
         add(bottomPanel);
     }

    private void checkUpdates() {
        try {
        final URL url = new URL(Settings.downloadUrl);
        URLConnection connection = url.openConnection();
        connection.connect();
        downloadUpdates(url);
        } catch(IOException e) {//failed to connect therefore lets just run the client if it exists all will run smooth
            try {
                startApplication();
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }
    }

    private void downloadUpdates(final URL url) {//Downloads the client off the website & removes older client
        try {
            final File file = new File(Settings.saveDirectory + url.getFile());
            if (file.exists())
                file.delete();

            Thread t = new Thread() {
                public void run() {
                    try {
                        OutputStream dest = new BufferedOutputStream(
                                new FileOutputStream(file));
                        URLConnection download = url.openConnection();
                        InputStream readFileToDownload = download
                                .getInputStream();
                        byte[] data = new byte[1024];
                        int numRead;
                        download.getContentLength();
                        while ((numRead = readFileToDownload.read(data)) != -1) {
                            dest.write(data, 0, numRead);
                        }
                        if (readFileToDownload != null)
                            readFileToDownload.close();
                        if (dest != null)
                            dest.close();
                        Thread.sleep(1000L);
                        startApplication();
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Client() throws IOException {
        createLayout();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == Launch) {
            checkUpdates();
        }
    }

    public static void startApplication() throws InterruptedException {
        try {
            Runtime.getRuntime().exec(
                    "java -jar " + (Settings.saveDirectory + Settings.fileName)
                            + "");
            Thread.sleep(1000L);
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) throws IOException {
        final Client l = new Client();
        l.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                l.setVisible(true);
            }
        });

    }

}

EDIT:

setContentPane(new JLabel(Settings.background));

replaced with

JLabel label = new JLabel(Settings.background);
setContentPane(label);
label.setLayout(new BorderLayout());

Fixed the issue, due to the JLabel being placed over everything.

Here's the problem

setContentPane(new JLabel(Settings.background));

You're setting the content pane with the label. If you get rid of that, it works.

An option, if you want a background image, is to paint it on a JPanel something like this

public class ButtonPanel extends JPanel {

    Image img;

    public ButtonPanel() {
        img = (Settings.background).getImage();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(Settings.width, Settings.height);

    }
}

The ButtonPanel will be the buttonPanel you are currently using

Another solution, as @MadProgrammer suggested is just to set the layout manager of the background label to BorderLayout , like this

private void addPanel() {
    setTitle("RuneRebellionV2 Launcher");
    JLabel label = new JLabel(Settings.background);
    setContentPane(label);
    label.setLayout(new BorderLayout());

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