我有一个JPanel带有彩绘背景图像和一个布局管理器,其中包含其他较小的图像,所有这些都在一个内部JFrame.背景图像非常大,我希望能够保持其纵横比,无论是在大型还是小型显示器上.
最终,我希望能够将我LayoutManager和它的细胞中的较小图像"粘合"到背景图片上.
我四处寻找资源,似乎很多例子都使用了,BufferedImage但我不是; 这会造成问题吗?我将在下面发布我的代码来绘制图像,如果我缺少任何信息,请告诉我.
public class MonitorPanel extends JPanel {
Image img;
public MonitorPanel() throws MalformedURLException {
//add components
try {
img = ImageIO.read(new File("src/customer_vlans.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void paintComponent(Graphics g)
{
//paint background image
super.paintComponent(g);
//g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
g.drawImage(img, 0, 0, this);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我应该提到我知道宽高比公式: 原始高度/原始宽度x新宽度=新高度 然而,我不知道如何正确使用它对我有利.
我有一个小问题。我正在学习 Java 表单,我想在调整表单大小的同时调整图像大小。但不幸的是我只调整表格大小。图像仍然具有相同的尺寸。如何重新缩放图像?我应该改变什么?我做错了什么?我附上代码:
package przegladarka;
import java.io.*;
import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;
public class view extends JPanel {
Image imag;
Dimension size;
public static int framew=300;
public static int frameh=300;
static String[] children;
static ArrayList<String> sciezki = new ArrayList<String>();
public static void main(String[] args) {
File folder = new File("./zdjecia/");
visitAllFiles(folder);
JFrame frame = new JFrame("Przegladarka");
frame.setSize(300, 300);
frame.setVisible(true);
frame.getContentPane().add(new MyCanvas());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void visitAllFiles(File dir) {
if (dir.isDirectory()) {
children = dir.list();
for (int i = …Run Code Online (Sandbox Code Playgroud) 我正在编写一个gui,我想使用嵌入一些图片,但在将它嵌入我的主程序之前,我编写了该代码来测试它:
public class guikopie extends javax.swing.JFrame{
public guikopie() {
a = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
a.setIcon(new javax.swing.ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulpen.jpg"));
add(a);//here i add it to the jlabel
pack();
}
public static void main(String args[]){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new guikopie().setVisible(true);
}
});
}
private javax.swing.JLabel a;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么这段代码不显示图片?
我意识到这个问题已经被问过很多次了,但我觉得它没有得到正确的回答。
我想在 java gui 中使用图像作为背景
我尝试了以下方法:
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
public class worldGUI extends JFrame implements ActionListener
{
private ImageIcon world = new ImageIcon("C://Users/Hans/Documents/world/map.png");
private JLabel map = new JLabel(world);
private JButton borders = new JButton("Borders");
public worldGUI()
{
setTitle("Welcome to the World");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,500);
setLocation(100,100);
setVisible(true);
borders.addActionListener(this);
map.add(borders);
setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
我是 Java 新手,请耐心等待