Java中JList.repaint()的作用
时间: 2024-03-18 14:44:14 浏览: 84
JList.repaint()方法的作用是重绘JList组件。当JList的数据模型发生变化或者用户进行了交互操作时,需要调用该方法来更新JList的显示内容。该方法会触发JList的paint()方法,以便重新绘制JList所包含的所有单元格。这样可以保证JList显示的内容与数据模型保持一致,并且反映出用户的操作。
相关问题
用Java代码实现用Java编写一个程序: 1)从已有的图象文件列表中选择某一个,即可显示该图象,并可做旋转等变换。 2)在画布区域上可以进行各种规则图形的绘制(如:线段、矩形、多边形、圆等),还可通过鼠标的拖动自由绘图。 3)可以连续播放所选文件夹下的所有图象。
好的,下面是一个用Java代码实现的示例:
```java
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageEditor extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel canvasPanel;
private Canvas canvas;
private JList<String> fileList;
private JScrollPane scrollPane;
private JButton rotateButton;
private JButton playButton;
private JPopupMenu popupMenu;
private JMenuItem rotateMenuItem;
private JMenuItem deleteMenuItem;
private JMenuItem playMenuItem;
private ArrayList<File> imageFiles;
private int curIndex;
public ImageEditor() {
super("Image Editor");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 初始化UI组件
canvasPanel = new JPanel(new BorderLayout());
canvas = new Canvas();
canvasPanel.add(canvas, BorderLayout.CENTER);
fileList = new JList<>();
fileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fileList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
curIndex = fileList.getSelectedIndex();
if (curIndex >= 0) {
loadImage(imageFiles.get(curIndex));
}
}
});
scrollPane = new JScrollPane(fileList);
scrollPane.setPreferredSize(new Dimension(150, 0));
rotateButton = new JButton("Rotate");
rotateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showPopupMenu(e.getComponent(), e.getX(), e.getY());
}
});
playButton = new JButton("Play");
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
playImages();
}
});
// 初始化弹出菜单
popupMenu = new JPopupMenu();
rotateMenuItem = new JMenuItem("Rotate");
rotateMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
rotateImage();
}
});
deleteMenuItem = new JMenuItem("Delete");
deleteMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteImage();
}
});
playMenuItem = new JMenuItem("Play");
playMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
playImages();
}
});
popupMenu.add(rotateMenuItem);
popupMenu.add(deleteMenuItem);
popupMenu.add(playMenuItem);
// 添加UI组件到主窗口
JPanel buttonPanel = new JPanel();
buttonPanel.add(rotateButton);
buttonPanel.add(playButton);
add(canvasPanel, BorderLayout.CENTER);
add(scrollPane, BorderLayout.WEST);
add(buttonPanel, BorderLayout.SOUTH);
// 加载图像文件列表
imageFiles = new ArrayList<>();
File dir = new File("images");
File[] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".gif");
}
});
Arrays.sort(files);
for (File file : files) {
imageFiles.add(file);
}
fileList.setListData(getFileNames(imageFiles));
if (imageFiles.size() > 0) {
fileList.setSelectedIndex(0);
}
}
// 获取文件名列表
private String[] getFileNames(ArrayList<File> files) {
String[] names = new String[files.size()];
for (int i = 0; i < files.size(); i++) {
names[i] = files.get(i).getName();
}
return names;
}
// 显示弹出菜单
private void showPopupMenu(Component invoker, int x, int y) {
boolean selected = (curIndex >= 0);
rotateMenuItem.setEnabled(selected);
deleteMenuItem.setEnabled(selected);
playMenuItem.setEnabled(selected);
popupMenu.show(invoker, x, y);
}
// 加载图像文件并显示到画布上
private void loadImage(File file) {
try {
BufferedImage image = ImageIO.read(file);
canvas.setImage(image);
canvas.repaint();
} catch (IOException e) {
e.printStackTrace();
}
}
// 旋转图像
private void rotateImage() {
if (curIndex >= 0) {
double angle = Math.toRadians(90);
BufferedImage image = canvas.getImage();
AffineTransform transform = new AffineTransform();
transform.rotate(angle, image.getWidth() / 2.0, image.getHeight() / 2.0);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage rotatedImage = new BufferedImage(image.getHeight(), image.getWidth(), image.getType());
op.filter(image, rotatedImage);
canvas.setImage(rotatedImage);
canvas.repaint();
}
}
// 删除图像
private void deleteImage() {
if (curIndex >= 0) {
imageFiles.remove(curIndex);
fileList.setListData(getFileNames(imageFiles));
if (imageFiles.size() > 0) {
curIndex = Math.min(curIndex, imageFiles.size() - 1);
fileList.setSelectedIndex(curIndex);
loadImage(imageFiles.get(curIndex));
} else {
canvas.setImage(null);
canvas.repaint();
}
}
}
// 连续播放图像
private void playImages() {
if (imageFiles.size() > 0) {
Timer timer = new Timer(1000, new ActionListener() {
int index = 0;
public void actionPerformed(ActionEvent e) {
loadImage(imageFiles.get(index));
index = (index + 1) % imageFiles.size();
}
});
timer.start();
}
}
// 画布类
private class Canvas extends JPanel {
private static final long serialVersionUID = 1L;
private BufferedImage image;
public void setImage(BufferedImage image) {
this.image = image;
}
public BufferedImage getImage() {
return image;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
int x = (getWidth() - image.getWidth()) / 2;
int y = (getHeight() - image.getHeight()) / 2;
g.drawImage(image, x, y, null);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ImageEditor editor = new ImageEditor();
editor.setVisible(true);
}
});
}
}
```
这个程序使用了Swing库来实现图形用户界面,使用了Java的图像处理库ImageIO和AffineTransform来实现图像的加载和旋转,使用了Java的定时器Timer来实现图像的连续播放。你可以在程序中修改图像文件列表所在的目录。
要求通过点击左边的目录Button,可以实现右边面板的切换。 左边面板可以分别编写四个面板类分别为“消息中心”,“学生信息”,“工作日志”,“个性设置”。右边面板切换可以根据JSplitPane中的方法setRightComponent(Component)实现。其中“消息中心”需要声明消息类Message,包括消息内容(String)和状态(即,已读,未读)(Boolean)。 消息面板主要用列表框Jlist<Message>实现。可以根据消息状态进行筛选信息,也可以根据消息信息内容部分匹配进行筛选,可以删除选中信息,可以删除全部信息。给出详细的代码及注释。
好的,这是您的第一个问题。下面是一个示例代码,可以实现左边目录Button的点击切换右边面板的功能:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class PanelSwitchDemo extends JFrame implements ActionListener {
private JPanel panelLeft;
private JPanel panelRight;
private JButton btnMsgCenter;
private JButton btnStudentInfo;
private JButton btnWorkLog;
private JButton btnSettings;
public PanelSwitchDemo() {
super("Panel Switch Demo");
// 创建左边的面板
panelLeft = new JPanel(new GridLayout(4, 1));
btnMsgCenter = new JButton("消息中心");
btnStudentInfo = new JButton("学生信息");
btnWorkLog = new JButton("工作日志");
btnSettings = new JButton("个性设置");
panelLeft.add(btnMsgCenter);
panelLeft.add(btnStudentInfo);
panelLeft.add(btnWorkLog);
panelLeft.add(btnSettings);
// 将左边的面板添加到窗口的左边
add(panelLeft, BorderLayout.WEST);
// 创建右边的面板
panelRight = new JPanel();
// 将右边的面板添加到窗口的中间
add(panelRight, BorderLayout.CENTER);
// 设置按钮的事件监听器
btnMsgCenter.addActionListener(this);
btnStudentInfo.addActionListener(this);
btnWorkLog.addActionListener(this);
btnSettings.addActionListener(this);
// 设置窗口的大小和关闭方式
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// 获取点击的按钮
JButton btn = (JButton) e.getSource();
// 根据不同的按钮切换右边的面板
if (btn == btnMsgCenter) {
panelRight.removeAll();
// 创建消息列表框
JList<Message> lstMsg = new JList<>(new Message[] {
new Message("消息1", false),
new Message("消息2", true),
new Message("消息3", false),
new Message("消息4", true),
new Message("消息5", false),
});
panelRight.add(new JScrollPane(lstMsg));
} else if (btn == btnStudentInfo) {
panelRight.removeAll();
// 创建学生信息表格
JTable tblStudent = new JTable(new Object[][] {
{"张三", "男", 20},
{"李四", "女", 21},
{"王五", "男", 22},
{"赵六", "女", 23},
}, new Object[] {"姓名", "性别", "年龄"});
panelRight.add(new JScrollPane(tblStudent));
} else if (btn == btnWorkLog) {
panelRight.removeAll();
// 创建工作日志文本框
JTextArea txtWorkLog = new JTextArea(10, 30);
txtWorkLog.setText("2021-01-01 08:00:00 上班打卡\n2021-01-01 12:00:00 午休开始\n2021-01-01 13:00:00 午休结束\n2021-01-01 18:00:00 下班打卡");
panelRight.add(new JScrollPane(txtWorkLog));
} else if (btn == btnSettings) {
panelRight.removeAll();
// 创建个性设置面板
JPanel panelSettings = new JPanel(new GridLayout(2, 2));
panelSettings.add(new JLabel("背景颜色:"));
panelSettings.add(new JComboBox<>(new String[] {"红色", "绿色", "蓝色"}));
panelSettings.add(new JLabel("字体大小:"));
panelSettings.add(new JTextField("12"));
panelRight.add(panelSettings);
}
// 刷新窗口
revalidate();
repaint();
}
// 消息类
private static class Message {
private String content;
private boolean read;
public Message(String content, boolean read) {
this.content = content;
this.read = read;
}
public String getContent() {
return content;
}
public boolean isRead() {
return read;
}
public void setRead(boolean read) {
this.read = read;
}
@Override
public String toString() {
return content + (read ? "(已读)" : "(未读)");
}
}
public static void main(String[] args) {
new PanelSwitchDemo();
}
}
```
注:这只是示例代码,您需要根据自己的实际需求进行修改和完善。
阅读全文