目前我的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被置于所有内容之上。
答案 0 :(得分:4)
这是问题
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());