「java刷新图像」java刷新按钮

博主:adminadmin 2023-03-22 22:21:07 869

本篇文章给大家谈谈java刷新图像,以及java刷新按钮对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

请问java的JLabel上的图片如何刷新?

java jlabel 图片的更新及清空

Java jlabel 里显示衣服图片根据jcombobox的内容实现不了更新,

还有我想把jlabel的图片清空怎么实现!

jLabel.setIcon(null),编译出错。

麻烦大家指点一下

------解决方案--------------------

我运行label.setIcon(null)没有问题,给你一个例子,运行的时候,需要在工程目录下有images目录,该目录下加上1.jpg 2.jpg …… 8.jpg这样八个图片

Java codeimport java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.net.URL;

import javax.swing.BorderFactory;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.UIManager;

public class ImageRandomAccess2 implements ActionListener {

final static int NUM_IMAGES = 8;//显示的图片总数

final static int START_INDEX = 3;//初始化的时候默认显示的图片

ImageIcon[] images = new ImageIcon[NUM_IMAGES];//初始化数组

JPanel mainPanel, selectPanel, displayPanel;//三个面板

JLabel selectLabel=null;

JComboBox combobox=null;//控制显示有哪些图片,非随即滚动

JButton controlBtn=null;

JLabel imageIconLabel = null;

// Constructor

public ImageRandomAccess2() {

// Create the phase selection and display panels.

selectPanel = new JPanel();

displayPanel = new JPanel();

// Add various widgets to the sub panels.

addWidgets();

// Create the main panel to contain the two sub panels.

mainPanel = new JPanel();

mainPanel.setLayout(new GridLayout(2, 1, 5, 5));

mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

// Add the select and display panels to the main panel.

mainPanel.add(selectPanel);

mainPanel.add(displayPanel);

}

// Create and the widgets to select and display the images of peoples.

private void addWidgets() {

// Get the images and put them into an array of ImageIcon.

for (int i = 0; i NUM_IMAGES; i++) {

String imageName = "images/" + (i + 1) + ".jpg";

System.out.println("getting image: " + imageName);

// URL iconURL = ClassLoader.getSystemResource(imageName);

ImageIcon icon = new ImageIcon(imageName);

images[i] = icon;

}

// Create label for displaying images and put a border around

// it.

imageIconLabel = new JLabel();

imageIconLabel.setHorizontalAlignment(JLabel.CENTER);

imageIconLabel.setVerticalAlignment(JLabel.CENTER);

imageIconLabel.setVerticalTextPosition(JLabel.CENTER);

imageIconLabel.setHorizontalTextPosition(JLabel.CENTER);

imageIconLabel.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createLoweredBevelBorder(),

BorderFactory.createEmptyBorder(5, 5, 5, 5)));

imageIconLabel.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createEmptyBorder(0, 0, 10, 0),

imageIconLabel.getBorder()));

controlBtn=new JButton("无图片");

selectLabel=new JLabel("选择图片");

combobox=new JComboBox();

for(int i=0;iNUM_IMAGES;i++){

combobox.addItem(i);

}

selectLabel.setLabelFor(combobox);

// Display the first image.

imageIconLabel.setIcon(images[START_INDEX]);

imageIconLabel.setText("");

// Add border around the select panel.

selectPanel.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createTitledBorder("Select Phase"),

BorderFactory.createEmptyBorder(5, 5, 5, 5)));

// Add border around the display panel.

displayPanel.setBorder(BorderFactory.createCompoundBorder(

BorderFactory.createTitledBorder("Display Phase"),

BorderFactory.createEmptyBorder(5, 5, 5, 5)));

// Add control button to select panel and image label to

// displayPanel.

selectPanel.add(selectLabel);

selectPanel.add(combobox);

selectPanel.add(controlBtn);

displayPanel.add(imageIconLabel);

// Listen to events from control button.

// controlBtn.addActionListener(this);

combobox.addActionListener(this);

controlBtn.addActionListener(this);

}

boolean run = false;

// Implementation of ActionListener interface.

public void actionPerformed(ActionEvent event) {

if(event.getSource()==combobox){

int imageIndex=combobox.getSelectedIndex();

imageIconLabel.setIcon(images[imageIndex]);

}

if(event.getSource()==controlBtn){

System.out.println("------------");

imageIconLabel.setIcon(null);

// imageIconLabel.setIcon(new ImageIcon());

}

}

// main method

public static void main(String[] args) {

// create a new instance of ImageRandomAccess

ImageRandomAccess2 phases = new ImageRandomAccess2();

// Create a frame and container for the panels.

JFrame mainFrame = new JFrame("ImangeRandomAccess");

// Set the look and feel.

try {

UIManager.setLookAndFeel(UIManager

.getCrossPlatformLookAndFeelClassName());

} catch (Exception e) {

}

mainFrame.setContentPane(phases.mainPanel);

// Exit when the window is closed.

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Show the converter.

mainFrame.pack();

mainFrame.setLocationRelativeTo(null);

mainFrame.setVisible(true);

}

}

java web项目上传一些图片,必须刷新文件夹才能显示图片,如何编写自动刷新代码??

图片不会自动显示是不是浏览器缓存问题~?就是譬如两次请求都是同一个地址,但是如果你存储的图片已经更改,但是名字没变,这样浏览器貌似是不会重新发出请求。你可以在请求时候附加一个随机数,

或者你可以再详细描述下,你说的文件夹是在哪里的文件夹,网络上的?~还是本地?你说显示图片是在哪里显示?页面上?还是本地文件夹里面?

java web 图片更新问题

其实很简单,有时候是编译环境还没刷新过来,你把开发环境myeclopse关掉重新打开。。或者点击项目名称,按着F5,一阵狂刷新。。。就OK。。

java刷新图像的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java刷新按钮、java刷新图像的信息别忘了在本站进行查找喔。