只使用绘画方法,我的图像最初不会显示.一旦我最小化了Java窗口并调整其大小,图像就会显示出来.有什么代码我不见了吗?
public class Lil extends JFrame {
Image image = Toolkit.getDefaultToolkit().getImage("images/Untitled.png");
public Lil(){
setTitle("flame");
setBackground(Color.WHITE);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void paint(Graphics g){
g.clearRect(0, 0, 400, 400);
g.drawImage(image, 60, 25, null);
//repaint();
}
public static void main(String [] args){
new lil();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Mac上的Eclipse设计GUI,我想让我JButton只显示我设置的Icon.它在Mac OS上看起来很好,但是当我将它导出到jar文件并在Windows操作系统上运行时,JButton看起来像这样:

边界都是EmptyBorder.
我做错了什么?怎么能让后面的广场消失?
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Picture extends JFrame{
public static void main(String[] args){
new Picture();
}
public Picture(){
this.setTitle("Picture");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
ImageIcon pic = new ImageIcon("TestImage.jpg");
p.add(new JLabel(pic));
this.add(p);
this.pack();
this.setVisible(true);
System.out.println(pic);
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码只是显示一个折叠的框架.我希望包(); 方法调用会调整我的.jpg图像周围的框架,对吗?
我已经可以听到第一个问题,是的,图像与我的项目包文件夹位于同一文件夹中.
我正在使用Eclipse IDE.
我还添加了println(); 声明查看Image引用返回的内容并返回"TestImage.jpg".我以为它会在内存中返回图像的地址.
我整个上午一直在阅读论坛,如果不复制和使用其他人更复杂的代码,我无法找到任何可以帮助我的东西.我尽量保持这个尽可能简单,这样我就不会混淆自己.
在此先感谢您的帮助.
我的第一个问题:
我一直试图解决这个问题几天,但我已经到了失去耐心的地步.以下是一些代码和我的项目结构.
问题:如何getResources()在eclipse中和导入jar后工作?
谢谢您的帮助.
public enum Icons {
XXX("src/resoruces/icons/xyz.png");
private ImageIcon icon;
Icons(String path) {
try {
// will fail miserably in eclipse and after exporting to jar
URL imageURL = getClass().getClassLoader().getResource(path);
icon = new ImageIcon(imageURL);
} catch (Exception e) {
// works like a char in eclipse and after creating the jar file
// with the files in the same directory
System.out.println("getResoruce() did not work");
icon = new ImageIcon(path);
}
}
Run Code Online (Sandbox Code Playgroud)

我想测试一个带有简单png图像的程序.我写了一个简短的程序来做到这一点,但我似乎无法让路径正确.我已经检查过,再次检查,重新检查,并检查了我的路径名称四倍,但没有正确,但无论我做什么,这个图像都不会显示.我使用了Oracle在ImageIcon文档(creaetImageIcon())中编写的一个简短的类来实现这一目标,但它似乎没有帮助.我将在下面发布整个程序,因为它很短.
package practiceImages;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ImageIconGUITest {
public static void main(String[] args) {
ImageIconGUITest gui = new ImageIconGUITest();
gui.display();
}
private ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
private void display() {
JFrame frame = new JFrame();
JLabel label = new JLabel(createImageIcon(
"Users/Evan/javaItems/Sprites_and_Other_Art/green.png", …Run Code Online (Sandbox Code Playgroud) 所以我想在每次按下按钮时替换JLabel中的ImageIcon.我做了它,因此图像,标签和GridBagConstraints是公开的.当我试图改变它虽然没有任何反应.
我是以错误的方式来做这件事还是?
谢谢!
package hi.low;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.Random;
import java.util.ArrayList;
public class Card_panel extends JPanel implements ActionListener
{
private final int WIDTH = 400, HEIGHT = 200;
private static String[] imageList = {
"images/2h.png", "images/3h.png", "images/4h.png", "images/5h.png", "images/6h.png",
"images/7h.png", "images/8h.png", "images/9h.png", "images/th.png", "images/jh.png",
"images/qh.png", "images/kh.png", "images/ah.png", "images/2d.png", "images/3d.png",
"images/4d.png", "images/5d.png", "images/6d.png", "images/7d.png", "images/8d.png",
"images/9d.png", "images/td.png", "images/jd.png", "images/qd.png", "images/kd.png",
"images/ad.png", "images/2c.png", "images/3c.png", "images/4c.png", "images/5c.png",
"images/6c.png", "images/7c.png", "images/8c.png", "images/9c.png", "images/tc.png",
"images/jc.png", "images/qc.png", "images/kc.png", "images/ac.png", "images/2s.png", …Run Code Online (Sandbox Code Playgroud) 我是一个新的程序员,我有一些问题,我已经浏览了一个图片到我的GUI(并在文本框中设置路径),它显示在标签上,但标签尺寸设置只有100,100而图片很多更大,所以当我打开/显示它到标签它被裁剪,无论如何,它是否自动调整大小到标签大小?下面是我在浏览按钮和打开对话框上的逻辑代码,请任何人告诉我我错在哪里..
public class ImagePath extends javax.swing.JFrame {
private JPanel contentPane;
JLabel jLabel1;
String s2;
File targetFile;
BufferedImage targetImg;
public ImagePath() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jFileChooser1 = new JFileChooser();
int state = jFileChooser1.showOpenDialog(new JFrame());
jTextField1.setText("");
if (state == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(new JFrame(), "hii");
File file = jFileChooser1.getSelectedFile();
s2 = file.toString();
jTextField1.setText(s2);
jLabel1 = new JLabel();
jLabel1.setName(s2);
jLabel1.setLocation(50, 50);
jLabel1.setSize(300, 300);
add(jLabel1);
BufferedImage bi1;
try {
bi1 = ImageIO.read(file);
ImageIcon icon1 = new ImageIcon(bi1);
jLabel1.setIcon(icon1);
Image img …Run Code Online (Sandbox Code Playgroud) 我正在制作一个依赖于网格中的瓷砖的java游戏.我在GridLayout中使用了ImageIcon和JLabel.我在创建GridLayout时将垂直和水平都设置为零,并且所有使用的图像都没有额外的空间.
解决这个问题的最佳方法是什么?
//Sets up the game canvas
private void setUpCanvasPanel(){
//Adjusts the panel settings
canvasPanel.setLayout(new GridLayout(Main.diameter, Main.diameter, 0 ,0));
//Adds panel to masterpanel
resetc();
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
masterPanel.add(canvasPanel, c);
}
//Renders the game canvas
public void renderCanvas(){
ImageIcon[] iconArray = new ImageIcon[Main.diameter * Main.diameter];
JLabel[] labelArray = new JLabel[Main.diameter * Main.diameter];
int count = 0;
for(int i = 0; i < Main.diameter; i ++){
for(int j = 0; …Run Code Online (Sandbox Code Playgroud) 我一直在寻找解决方案,并阅读一些与此问题相关的类似帖子但其中没有一个对我有用.
我正在尝试在JButton上显示图像"b.png",当我滚动按钮时图标会发生变化.
package GUI_JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Gui extends JFrame {
private JButton reg;
private JButton custom;
public Gui() {
super("Title goes here");
setLayout(new FlowLayout());
reg = new JButton("reg button"); // create reg button
add(reg); // add reg button to JFrame
// initialize images
Icon b = new ImageIcon(getClass().getResource("images/imageA.png"));
Icon x = new ImageIcon(getClass().getResource("images/imageB.png"));
custom = new JButton("custom button", b); // create custom …Run Code Online (Sandbox Code Playgroud) 你好我是编程的新手,我们有一个项目.我们创建了一个简单的赛车游戏,背景动人,但我坚持我的代码,我不知道该怎么做.游戏开始时我需要一个移动的背景请有人帮助我.我在求T_T

这是我的代码:
public class Game extends JFrame implements KeyListener, ActionListener {
Random s = new Random();
int x = 0, y = 50, a = 0, b = 250, f = 900, a1 = 0, b2 = 350, a2 = 0, b3 = 150;
int Move;
int value;
JLabel spriteLabel1 = new JLabel(new ImageIcon("ss.gif"));
JLabel spriteLabel2 = new JLabel(new ImageIcon("ss.gif"));
JLabel spriteLabel3 = new JLabel(new ImageIcon("ss.gif"));
JLabel spriteLabel4 = new JLabel(new ImageIcon("ss.gif"));
JLabel background = new JLabel(new ImageIcon("geren.png"));
Timer T1 …Run Code Online (Sandbox Code Playgroud) imageicon ×10
java ×10
swing ×9
jbutton ×2
jlabel ×2
paint ×2
eclipse ×1
graphics2d ×1
grid-layout ×1
jframe ×1
keylistener ×1
path ×1
png ×1