「java刷新label」java刷新当前页面

博主:adminadmin 2023-01-14 20:06:11 449

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

本文目录一览:

急急急!!!!java如何每单机一次按钮刷新一次label的内容,求大神

假如你的那个按钮对象叫做 button, Label对象叫做 label

button.addMouseListener(new MouseListener(){

public void mouseClicked(MouseEvent e) {

Random r new Random();

label.setText(""+r.nextInt());

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

});

java swt 怎样实现label不断刷新 不影响其他组件

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.Timer;

public class TimerDemo extends JFrame {

public static void main(String[] args) {

new TimerDemo();

}

public TimerDemo() {

this.setSize(600, 450);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setLayout(new FlowLayout());

final JLabel label = new JLabel("0");

final Timer timer = new Timer(1000, new ActionListener() {

public void actionPerformed(ActionEvent e) {

int i = Integer.parseInt(label.getText());

label.setText(String.valueOf(i + 1));

}

});

timer.start();

final JButton button = new JButton("Stop");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (e.getActionCommand() == "Stop") {

timer.stop();

button.setText("Restart");

} else {

timer.restart();

button.setText("Stop");

}

}

});

add(label);

add(button);

this.setVisible(true);

}

}

请问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 JLabel类更新问题

你的意思就是让表格里面的内容显示2的时候去除以前显示的1.swing这点做的非常不灵活。就是因为swing不灵活,所以才会那么冷门。

你需要一个表格模型,在更新表格之前要删除原先表格的内容,然后再统治模型更新,最后执行刷新操作。

下面是三段代码。

((DefaultTableModel) jTable.getModel()).getDataVector().clear(); //清除表格数据

((DefaultTableModel) jTable.getModel()).fireTableDataChanged();//通知模型更新

jTable.updateUI();//刷新表格

欢迎追问

在JAVA中如何实现JLabel的背景设置和文本的刷新

void setIcon(Icon icon) 定义此组件将要显示的图标。

void setText(String text) 定义此组件将要显示的单行文本。

还有,从JComponent继承来的setBackground方法

java Jlabel文字怎么实时更新

可以使用JLabel的setText(text);例如:label.setText(text);

下面是一个具体的实例,当单击change按钮时改变文字内容。

public class WinTest implements ActionListener

{

JLabel label = new JLabel("注意我会变哦!");

public WinTest()

{

JFrame frame = new JFrame();

frame.setLayout(new FlowLayout());

JButton button = new JButton("change");

button.addActionListener(this);

frame.add(button);

Font font = new Font("黑体",Font.PLAIN,40);

label.setFont(font);

frame.add(label);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

frame.setVisible(true);

}

public static void main(String[] args)

{

new WinTest();

}

@Override

public void actionPerformed(ActionEvent e)

{

if("change".equals(e.getActionCommand()))

{

label.setText("啊哈哈!我会72变,啊哈哈哈哈哈哈!");

}

}

}

关于java刷新label和java刷新当前页面的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。