目前我的JFrame没有显示任何按钮,文本等。我查看了其他主题,导致错误的原因之一是因为setVisable方法在所有内容之前被调用被创建。正如你所看到的,这不是问题,因为它是在创建完成之后调用的。我的目标是底部面板有一个启动按钮,点击时JLabel取代它。 (这是我的第一个swing应用程序..)JFrame没有显示按钮或文本
我的问题是,启动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。
你的代码有点复杂,或者我认为编程术语是它有很多“[复杂性](http://en.wikipedia.org/wiki/Cyclomatic_complexity)”。这会让你和其他人难以调试它。考虑重构,简化和避免许多副作用的方法。 –
@HovercraftFullOfEels真的只有一点需要看,我把这段代码分开了..我真的不明白你的意思,但我可以完美地阅读代码。 – RuneRebellion
你可以阅读代码,但你找不到错误,对不对?否则你不会在这里。但我的意思是说,你的许多方法改变了类字段的状态并且什么都不返回,这会增加复杂性,因为它不太明显,它们如何改变状态,从而可能导致问题。 –