没有报错,但是图片无法显示,这是怎么回事啊
package ui;
import javax.swing.*;
//游戏主界面
public class GameJFrame extends JFrame{
//空参构造
public GameJFrame() {
//初始化界面
initJFrame();
//初始化菜单
initJMenu();
//初始化图片
initImage();
//让界面显示
this.setVisible(true);
}
//初始化图片
private void initImage() {
//创建一个图片ImageIcon对象
ImageIcon icon = new ImageIcon("/puzzlegame/image/animal/animal3/3.jpg");
//创建一个JLable的对象(管理容器)
JLabel jLabel = new JLabel(icon);
//把管理容器添加到界面中
this.add(jLabel);
}
//初始化界面
public void initJFrame() {
//设置界面的宽高
this.setSize(603, 680);
//设置界面的标题
this.setTitle("拼图小游戏");
//让界面置顶
this.setAlwaysOnTop(true);
//让界面默认居中
this.setLocationRelativeTo(null);
//设置关闭模式
this.setDefaultCloseOperation(3);
}
//初始化菜单
public void initJMenu() {
//创建整个的菜单对象
JMenuBar jMenuBar = new JMenuBar();
//创建菜单上面的两个选项的对象 (功能 关于我们)
JMenu functionJMenu = new JMenu("功能");
JMenu aboutJMenu = new JMenu("关于我们");
//创建选项下面的条目对象
JMenuItem replayItem = new JMenuItem("重新游戏");
JMenuItem reLoginItem = new JMenuItem("重新登录");
JMenuItem closeItem = new JMenuItem("关闭游戏");
JMenuItem accountItem = new JMenuItem("公众号");
//将每一个选项下面的条目添加到选项当中
functionJMenu.add(replayItem);
functionJMenu.add(reLoginItem);
functionJMenu.add(closeItem);
aboutJMenu.add(accountItem);
//将菜单里面的两个选项添加到菜单当中
jMenuBar.add(functionJMenu);
jMenuBar.add(aboutJMenu);
//给整个界面设置菜单
this.setJMenuBar(jMenuBar);
}
}
import ui.GameJFrame;
import ui.LoginJFrame;
import ui.RegisterJFrame;
//程序的启动入口
public class App {
public static void main(String[] args) {
//new LoginJFrame();
new GameJFrame();
//new RegisterJFrame();
}
}
你图片的相对路径对嘛? 项目目录发出来看看
package com.yk.jdk.thread;
import co.paralleluniverse.fibers.Fiber;
import java.util.concurrent.ExecutionException;
public class FiberTest {
static int LENGTH=100000;
public static void main(String[] args) throws InterruptedException, ExecutionException {
Long start=System.currentTimeMillis();
Fiber[] fibers=new Fiber[LENGTH];
for(int i=0;i<LENGTH;i++){
fibers[i]=new Fiber(()->{
cale();
});
}
for(int i=0;i<LENGTH;i++){
fibers[i].start();
}
for(int i=0;i<LENGTH;i++){
fibers[i].join();
}
Long end=System.currentTimeMillis();
System.out.println("FiberTest cost======================="+(end-start));
}
private static void cale() {
int k=0;
for(int i=0;i<1000;i++){
for (int j=0;j<200;j++){
k+=i;
}
}
}
}
需要配置 java 参数 javaagent ,-javaagent:path-quasar.jar (需要改成本地的位置)
协程10000,计算200万次,耗时 604毫秒
协程 100000 ,计算200万次 耗时 975毫秒
针对Java无法显示插入的图片问题,可能的原因和解决方式如下:
可能的原因: 1.图片路径设置不正确。 2.图片读取时发生异常。 3.图片显示的组件或容器大小不够。
解决方式: 1.确保图片路径设置正确。可以通过在代码中直接输出图片路径的方式确认路径是否正确,或者使用ClassLoader加载图片资源。 2.使用异常捕获机制,在读取图片时捕获可能抛出的异常,查看异常信息进行调试。 3.根据实际情况,调整组件或容器大小,确保图片能够完整显示在界面中。
参考代码(以Swing中的JLabel显示图片为例):
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageDisplayDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Image Display");
JLabel label = new JLabel();
// 设置图片路径
String path = "your image path";
// 加载图片资源
try {
label.setIcon(new ImageIcon(ImageDisplayDemo.class.getClassLoader().getResource(path)));
} catch (Exception e) {
e.printStackTrace();
}
frame.add(label);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
注:确保图片资源已经嵌入到Java项目中。