Java 从本地文件在JEditorPane中设置背景图像
我正在使用JEditorPane在JFrame上设置背景图像。我之所以使用这个组件,是因为我想设置小图像,并能够根据帧的大小重复它。类似于背景重复的CSS模拟。我遇到的问题是,我无法从本地文件夹加载图像Java 从本地文件在JEditorPane中设置背景图像,java,css,swing,Java,Css,Swing,我正在使用JEditorPane在JFrame上设置背景图像。我之所以使用这个组件,是因为我想设置小图像,并能够根据帧的大小重复它。类似于背景重复的CSS模拟。我遇到的问题是,我无法从本地文件夹加载图像 background.setContentType("text/html"); background.setText("<html><body style=\"background-image: url(http://hq-wallpapers.ru/wallpapers/8/
background.setContentType("text/html");
background.setText("<html><body style=\"background-image: url(http://hq-wallpapers.ru/wallpapers/8/hq-wallpapers_ru_abstraction3d_39318_1920x1200.jpg);\"></body></html>");
this.setContentPane(background);
background.setContentType(“text/html”);
背景。setText(“”);
此.setContentPane(背景);
我尝试使用的是:url(../image.jpg);但它不起作用
如果你能提供更好的方法来完成这项任务,我将不胜感激。
另外,由于我的项目的要求,我不允许使用JFrameForm。
。/image.jpg
不是有效的URL,因为JEditorPane
没有“代码库”,可以从中访问“引用”的加载位置
相反,您可以使用文件
生成URL
(或者如果它是嵌入式资源,类#getResource
),例如
File background = new File("../image.jpg");
URL url = background.toURI().toURL();
ep.setContentType("text/html");
ep.setText("<html><body style='color: #ffffff; background-image: url(" + url.toString() + ");'>Boo</body></html>");
File background=新文件(“../image.jpg”);
URL=background.toURI().toURL();
ep.setContentType(“文本/html”);
ep.setText(“Boo”);
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.io.File;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.JEditorPane;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
公共测试窗格(){
试一试{
setLayout(新的BorderLayout());
JEditorPane ep=新的JEditorPane();
文件背景=新文件(“/Volumes/Disk02/Dropbox/MegaTokyo/Aqua/aria_fanart_by_imdis疑-d5xbvgz.jpg”);
URL=background.toURI().toURL();
ep.setContentType(“文本/html”);
ep.setText(“Boo”);
添加(新的JScrollPane(ep));
}捕获(格式错误){
例如printStackTrace();
}
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200200);
}
}
}
现在如果
URL=background.toURI().toURL()代码>不起作用,您可能需要使用URL=background.getCanonicalFile().toURI().toul()
而不是加载本地文件的代码在哪里?@user1803551我遵循web开发逻辑,只需更改.setText()方法的背景样式。所以我有这样的smth:background.setText(“”);我想我们有个误会。本地文件夹通常指项目资源中的文件夹。您正在从internet加载一个文件,该文件不被视为“本地”。@user1803551我知道,我确实从internet加载图像,但我试图实现的是将其更改为本地存储。我还是想用HTML的方式来做。原因是,因为它重复图像,我可以用10像素乘10像素的小图片来填充我的背景。我试图在网络上找到解决方案,在那里我可以使用standart Swing元素用小图像填充背景,但我找不到任何合适的想法。MadProgammer当时没有解决这个问题吗?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
try {
setLayout(new BorderLayout());
JEditorPane ep = new JEditorPane();
File background = new File("/Volumes/Disk02/Dropbox/MegaTokyo/Aqua/aria_fanart_by_imskeptical-d5xbvgz.jpg");
URL url = background.toURI().toURL();
ep.setContentType("text/html");
ep.setText("<html><body style='color: #ffffff; background-image: url(" + url.toString() + ");'>Boo</body></html>");
add(new JScrollPane(ep));
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}