我正在尝试在JLabel中加载动画GIF.
虽然这有效:
URL urlsd;
try {
urlsd = new URL("http://pscode.org/media/starzoom-thumb.gif");
ImageIcon imageIcon = new ImageIcon(urlsd);
JLabel progress = new JLabel(imageIcon);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
另一方面,这不,并且我不想从URL获取GIF,因为我已经拥有GIF.加载结果只显示GIF的第一帧:
try {
ImageIcon imageIcon = new ImageIcon(ImageIO.read(ClassLoader.getSystemResourceAsStream("res/images/progress_indicator.gif")));
JLabel progress = new JLabel(imageIcon);
imageIcon.setImageObserver(progress);
progress.setBounds(5, 20, 66, 66);
contentPane.add(progress);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我想这肯定是有原因的,但我找不到它.
谢谢!亚历克斯
每当从JComboBox中选择一个项目时,我都试图从图像文件夹中将图标设置为JLabel.JComboBox中的项目名称和文件夹中图像的名称相同.因此,无论何时从JComboBox中选择一个项目,都应将具有相同名称的相应图像设置为JLabel的图标.我想做这样的事情.
private void cmb_movieselectPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt){
updateLabel(cmb_moviename.getSelectedItem().toString());
}
protected void updateLabel(String name) {
ImageIcon icon = createImageIcon("C:\\Users\\xerof_000\\Pictures\\tmspictures\\" + name + ".jpg");
if(icon != null){
Image img = icon.getImage();
Image newimg = img.getScaledInstance(lbl_pic.getWidth(), lbl_pic.getHeight(), java.awt.Image.SCALE_SMOOTH);
icon = new ImageIcon(newimg);
lbl_pic.setIcon(icon);
lbl_pic.setText(null);
}
else{
lbl_pic.setText("Image not found");
lbl_pic.setIcon(null);
}
}
protected static ImageIcon createImageIcon(String path) {
URL imgURL;
imgURL = NowShowing.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为问题出现在"C:\ Users\xerof_000\Pictures\tmspictures \"中我尝试使用"C:/ Users/xerof_000/Pictures/tmspictures /",但即使这样也行不通.无论我做什么,它只在JLabel上显示"Image not found".
我是一名初学者,正在构建一个带按钮和滚动条的简单窗口.当我编译我的代码时,我的按钮上的文字被删除了一个省略号,图像图标没有显示.我试过在eclipse和NetBeans中编译它.为了解决我试过的问题
.setMargin(new Insets(0, 0, 0, 0));
.setPreferedSize
adding padding (I forgot the code for this)
.setBounds
Run Code Online (Sandbox Code Playgroud)
以及我在互联网上偶然发现的其他一切.这些都没有解决我的问题,我无法查看按钮中的文本和图像.
我的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FeedBar2 extends JFrame {
public FeedBar2() {
super("FeedBar 2");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create icons
ImageIcon loadIcon = new ImageIcon("load.gif");
ImageIcon saveIcon = new ImageIcon("save.gif");
ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
// create buttons
JButton load = new JButton("Load", loadIcon);
JButton save = new JButton("Save", saveIcon);
JButton subscribe = new JButton("Subscribe", subscribeIcon); …Run Code Online (Sandbox Code Playgroud) 我想要添加一个图像JButton.按钮的背景设置为黑色.我试图在它上面添加图像,但没有显示任何内容.背景颜色为黑色但图像缺失.
public class Test extends JFrame {
JButton b;
JPanel p;
Test() {
p = new JPanel(new BorderLayout());
b = new JButton();
b.setBackground(Color.black);
ImageIcon img = new ImageIcon("C:\\Users\\Aksi\\Documents\\NetBeansProjects\\test'\\src\\test\\Black_B.ico");
b.setIcon(img);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 400);
p.add(b);
add(p);
validate();
}
public static void main(String args[]) throws IOException {
Test ob = new Test();
ob.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将图像添加到一个帧但看起来它不起作用.ImageIcon从指定文件创建的图像.图像文件位于java文件存在的seam目录中.
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class image {
public static void main(String args[])
{
TimeFrame frame = new TimeFrame();
}
}
class TimeFrame extends JFrame
{
//Image icon = Toolkit.getDefaultToolkit().getImage("me.jpg");
ImageIcon icon = new ImageIcon("me.jpg");
JLabel label = new JLabel(icon);
public TimeFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("My Frame");
setSize(500,400);
//this.setIconImage(icon);
add(label,BorderLayout.CENTER);
setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud) 我不是很擅长创建Swing应用程序.所以我有问题如何设置图标JButton.
我的项目结构如下所示:

我JButton在MainWindow课堂上很简单:它看起来像这样:
tactButton = new JButton("next tact");
Run Code Online (Sandbox Code Playgroud)
我想使用方法将图像设置为此按钮setIcon.我的代码看起来像这样:
tactButton.setIcon(new ImageIcon(getClass().getResource("/images/button_next.jpg")));
Run Code Online (Sandbox Code Playgroud)
但是当我启动应用程序时,我有例外:
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at by.bulgak.conveyor.gui.MainWindow.<init>(MainWindow.java:117)
at by.bulgak.conveyor.gui.MainWindow$1.run(MainWindow.java:46)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Run Code Online (Sandbox Code Playgroud)
所以我尝试了不同的东西:
MainWindow的类tactButton.setIcon(new ImageIcon("/images/button_next.jpg")); 但我有这个例外,或者如果我使用tactButton.setIcon(new ImageIcon("/images/button_next.jpg"));我有简单的按钮没有图像.
最后,我写了我的图像的绝对路径,这很好(但绝对路径不是好主意).你能帮帮我吗?
我看了一个问题如何将图像添加到JButton并试图像那样做.
UPDATE
创建按钮和设置图标的完整代码:
tactButton = new JButton("next tact");
tactButton.setSize(100, …Run Code Online (Sandbox Code Playgroud) 嗨我已经创建了aj列表,我想在该文本中的任何文本之前添加图像我怎么能这样做我试过但我无法实现我的目标我想在列表元素"Barmer"之前添加图像.
public class ListDemo extends JPanel
implements ListSelectionListener {
private JList list;
private DefaultListModel listModel;
public ListDemo() {
super(new BorderLayout());
listModel = new DefaultListModel();
listModel.addElement("Barmer");
//Create the list and put it in a scroll pane.
list = new JList(listModel);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
list.setVisibleRowCount(5);
list.setBackground(new java.awt.Color(0,191,255));;
list.setFont(new Font("Arial",Font.BOLD,35));
list.setForeground( Color.white );
list.setFixedCellHeight(60);
list.setFixedCellWidth(50);
list.setBorder(new EmptyBorder(10,20, 20, 20));
JScrollPane listScrollPane = new JScrollPane(list);
add(listScrollPane, BorderLayout.CENTER);
}
public void valueChanged(ListSelectionEvent e) {
}
private static void createAndShowGUI() {
//Create and set up the …Run Code Online (Sandbox Code Playgroud) 我有一个使用CardLayout的类,它有两张卡片,上面有一个按钮.我希望能够做的是放置一个像背景一样的图像,例如Windows中的桌面背景.该程序最终将有几张不同的卡片,我希望能够在每张卡片上放置不同的背景.我已尝试过在本网站上提出的其他类似问题中提出的许多建议,以及通过Google搜索我能找到的任何建议,但我似乎无法让它发挥作用.我知道使用CardLayout我不能将面板放在面板上,因此将图像放在JPanel上是行不通的.所以我的问题是,根据下面发布的代码,我有更好的方法来设置我的布局,以便它更好地工作,而且,我应该如何处理显示图像,使其在按钮的背景中?任何帮助将不胜感激.
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Cards implements ActionListener {
private JPanel cards;
private JButton button1;
private JButton button2;
private Image backgroundImage;
public void addComponentToPane(Container pane) throws IOException {
backgroundImage = ImageIO.read(new File("background.jpg"));
//create cards
JPanel card1 = new JPanel()
{
@Override
public void paintComponent(Graphics g)
{
g.drawImage(backgroundImage, 0, 0, null);
}
};
JPanel card2 = new JPanel();
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
button1.addActionListener(this);
button2.addActionListener(this);
card1.add(button1); …Run Code Online (Sandbox Code Playgroud) 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".我以为它会在内存中返回图像的地址.
我整个上午一直在阅读论坛,如果不复制和使用其他人更复杂的代码,我无法找到任何可以帮助我的东西.我尽量保持这个尽可能简单,这样我就不会混淆自己.
在此先感谢您的帮助.
我正在写一个程序,要求我有一个按钮,上面有一个图像,但到目前为止,我还没有能够让它工作.我已经检查了这个网站上的其他几个帖子,包括如何将图像添加到JButton.
我的代码:
public class Tester extends JFrame
{
public Tester()
{
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
setTitle("Image Test");
setSize(300,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button = new JButton();
try
{
Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));
button.setIcon(new ImageIcon(img));
}
catch (IOException ex) {}
button.setBounds(100,100,100,100);
panel.add(button);
}
public static void main(String[] args)
{
Tester test = new Tester();
test.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
当此代码运行时,会出现错误:线程"main"中的异常java.lang.IllegalArgumentException:input == null!此错误发生在以下行:
Image img = ImageIO.read(getClass().getResource("Images\\BBishopB.gif"));
Run Code Online (Sandbox Code Playgroud)
我不认为这个错误是由于java代码找不到文件,因为我的Images文件夹在src文件夹中(我正在使用Eclipse),如上面链接所推荐的那样.
有没有人对问题可能有什么想法?
谢谢.
java ×10
swing ×10
imageicon ×5
image ×3
jbutton ×3
jlabel ×3
jframe ×2
animated-gif ×1
cardlayout ×1
eclipse ×1
jcombobox ×1
jlist ×1
jpanel ×1
jscrollpane ×1
packaging ×1