gpt4 book ai didi

java - 更新 JList 组件

转载 作者:太空宇宙 更新时间:2023-11-04 07:03:53 25 4
gpt4 key购买 nike

我在 JPanel 上有一个 JProgressBar 作为 JList 组件。JList 组件应该每两秒更新一次 JProgressBar。但问题是,我不知道如何将进度值传递到 JProgressBar。这是我的代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class download {
download(){}
}
class myPanelListCellRenderer extends DefaultListCellRenderer{
private static final long serialVersionUID = 1L;
private JPanel downloadPanel,labelStack;
private JProgressBar downloadProgress;
private JLabel fileTypeIconLabel,fileNameLabel,downloadInfoLabel,freeLabel;
public myPanelListCellRenderer(){
setLayout(null);
downloadPanel=new JPanel(new BorderLayout(10,20));
labelStack=new JPanel(new GridLayout(0,1,2,2));
downloadProgress=new JProgressBar() {
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
return new Dimension(400,d.height);
}
};
fileTypeIconLabel=new JLabel("test", SwingConstants.CENTER);
fileNameLabel=new JLabel("fileNameLabel", SwingConstants.CENTER);
downloadInfoLabel=new JLabel("downloadInfoLabel", SwingConstants.CENTER);
freeLabel=new JLabel("freeLabel", SwingConstants.CENTER);
fileTypeIconLabel.setFont(fileTypeIconLabel.getFont().deriveFont(60f));
labelStack.add(fileNameLabel);
labelStack.add(downloadInfoLabel);
labelStack.add(downloadProgress);
labelStack.add(freeLabel);
labelStack.setOpaque(false);
downloadPanel.add(fileTypeIconLabel,BorderLayout.LINE_START);
downloadPanel.add(labelStack,BorderLayout.CENTER);
}
@Override
public Component getListCellRendererComponent(JList<?> list,Object value,int index,boolean isSelected,boolean cellHasFocus) {
JLabel l = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
downloadPanel.setBackground(l.getBackground());
fileTypeIconLabel.setText("" + (index + 1));
return downloadPanel;
}
}

public class JListPanel implements ActionListener{
private JPanel btnPanel;
private JButton jbtAdd,jbtDelete;
private JFrame jf;
private download dl;
DefaultListModel<download> myListModel;
JList<download>myList;
public JListPanel(){
myListModel=new DefaultListModel<download>();
myList=new JList<download>(myListModel);
myList.setCellRenderer(new myPanelListCellRenderer());
myList.setVisibleRowCount(8);
btnPanel=new JPanel();
jbtAdd=new JButton("addJpanel");
jbtDelete=new JButton("delJpanel");
btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
btnPanel.add(jbtAdd);
btnPanel.add(jbtDelete);
jf=new JFrame("hello");
for(int i=0;i<6;i++){
dl=new download();
myListModel.add(0, dl);
}
jf.setLocationByPlatform(true);
jf.add(btnPanel,BorderLayout.NORTH);
jf.add(new JScrollPane(myList),BorderLayout.CENTER);
jbtAdd.addActionListener(this);
jbtDelete.addActionListener(this);
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public static void main(String args[]){
new JListPanel();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jbtAdd){
addMyPanel();
}
else if(e.getSource()==jbtDelete){
delMyPanel();
}
}
public void addMyPanel(){
myListModel.add(0,new download());
}
public void delMyPanel(){
int index = myList.getSelectedIndex();
if (index < 0) {
JOptionPane.showMessageDialog(
myList,
"Select a download to delete!",
"Select Download",
JOptionPane.ERROR_MESSAGE);
} else {
myListModel.removeElementAt(index);
}
}
}

最佳答案

不要将进度值直接传递给 JProgressBar,而是让进度条监听来自更新列表数据模型的任何进程的更改。 SwingWorker ,见过here ,对此特别方便,因为工作人员的 setProgress() 会自动通知监听器。

关于java - 更新 JList 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21703673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com