Java 小程序显示不正确
Java applet not showing correctly
我做了一个Java小程序。
<applet code=gui.clientGUI.MyApplet.class
archive="QTminer.jar"
width=400 height=200>
</applet>
我的罐子看起来像这样:
这是我的简单代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class MyApplet extends JApplet {
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
initUI();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void initUI() {
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JButton btnStartApplication = new JButton("Start Application");
btnStartApplication.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SS oi = new SS();
oi.setVisible(true);
}
});
getContentPane().add(btnStartApplication);
}
}
class SS extends JWindow {
private JLabel lblNewLabel;
private JLabel label;
public SS() {
setBounds(new Rectangle(0, 0, 883, 590));
setLocationRelativeTo(null);
getContentPane().setLayout(null);
lblNewLabel = new JLabel("Welcome", SwingConstants.CENTER);
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setFont(new Font("Segoe UI", Font.BOLD | Font.ITALIC, 24));
lblNewLabel.setBounds(0, 313, 883, 41);
getContentPane().add(lblNewLabel);
label = new JLabel(new ImageIcon(getClass().getResource("/gui/resources/Qtminer_background.jpg")));
label.setBounds(0, 0, 883, 592);
getContentPane().add(label);
setVisible(true);
}
}
我的问题是,当 运行 在 eclipse 中一切正常时,但是当 运行 在浏览器中时,我在加载图像图标时得到 NullPointerException:
label.setIcon(new ImageIcon(getClass().getResource("/gui/resources/Qtminer_background.jpg")));
我的 OS 拒绝访问本地文件 jar。
在真实的网络服务器上测试小程序使其工作。
已解决!
我做了一个Java小程序。
<applet code=gui.clientGUI.MyApplet.class
archive="QTminer.jar"
width=400 height=200>
</applet>
我的罐子看起来像这样:
这是我的简单代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class MyApplet extends JApplet {
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
initUI();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void initUI() {
getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JButton btnStartApplication = new JButton("Start Application");
btnStartApplication.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SS oi = new SS();
oi.setVisible(true);
}
});
getContentPane().add(btnStartApplication);
}
}
class SS extends JWindow {
private JLabel lblNewLabel;
private JLabel label;
public SS() {
setBounds(new Rectangle(0, 0, 883, 590));
setLocationRelativeTo(null);
getContentPane().setLayout(null);
lblNewLabel = new JLabel("Welcome", SwingConstants.CENTER);
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setFont(new Font("Segoe UI", Font.BOLD | Font.ITALIC, 24));
lblNewLabel.setBounds(0, 313, 883, 41);
getContentPane().add(lblNewLabel);
label = new JLabel(new ImageIcon(getClass().getResource("/gui/resources/Qtminer_background.jpg")));
label.setBounds(0, 0, 883, 592);
getContentPane().add(label);
setVisible(true);
}
}
我的问题是,当 运行 在 eclipse 中一切正常时,但是当 运行 在浏览器中时,我在加载图像图标时得到 NullPointerException:
label.setIcon(new ImageIcon(getClass().getResource("/gui/resources/Qtminer_background.jpg")));
我的 OS 拒绝访问本地文件 jar。
在真实的网络服务器上测试小程序使其工作。
已解决!