繁体   English   中英

JFrame没有显示按钮或文本

[英]JFrame isn't showing buttons or text

当前,我的JFrame没有显示任何按钮,文本等。我​​查看了其他主题,并且发生错误的原因之一是因为在创建所有内容之前已调用setVisable方法。 如您所见,这不是问题,因为在创建所有内容之后便会调用它。 我的目标是在底部面板上具有启动按钮,并在单击JLabel时取代它。 (这是我的第一个秋千应用程序。)

我的问题是启动JButton和bottomPanel没有显示。

这是客户端类中发生问题的地方

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);
 }

这就是系统的其余部分,其工作原理如下:启动启动。

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();
        }
    }
}

运行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();
        }
    }
}

然后,此类使用设置来制作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);
            }
        });

    }

}

编辑:

setContentPane(new JLabel(Settings.background));

替换为

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

解决了由于将JLabel放在所有内容上而导致的问题。

这是问题

setContentPane(new JLabel(Settings.background));

您正在使用标签设置内容窗格。 如果您摆脱了这一点,那就行得通。

如果需要背景图像,可以选择将其绘制在JPanel ,如下所示

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

    }
}

ButtonPanel将是您当前正在使用的buttonPanel

@MadProgrammer建议的另一种解决方案是将背景标签的布局管理器设置为BorderLayout ,就像这样

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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