我现在想要做一个GUI组件,要求是这样的:
它能构显示文字和图标,而且文字如果过长,会用省略号代替多余文字。它可以被选中,也就是类似JTable中单元格的效果,但要能显示图片,并且只能在水平方向上拉伸,而不能在数值方向上。我发现在eclipse中build和update等过程中显示的对话框中有一个组件同我的要求很像,熟悉SWT的朋友谁知道这是用的什么widget吗?或者使用Swing能做出这样的效果吗?图见:http://freehost21.websamba.com/utarget/Eclipse_snap.jpg谢谢。

解决方案 »

  1.   

    直接看 Eclipse的源码呀 org.eclipse.pde.ui 等里面有很多实例。
      

  2.   

    我看了org.eclipse.update.ui包里的一些源码,头都大了,所以来问问的啊。我再去看看pde吧恳请有人解答,多谢。一定要看看那张截图
      

  3.   

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Random;import javax.swing.*;public class ProgressList
    {
    private static class Progress {
    String title;
    String processInfo;
    int progress; public Progress(String title, int progress, String info)
    {
    processInfo = info;
    this.progress = progress;
    this.title = title;
    } public String getProcessInfo()
    {
    return processInfo;
    }
    public void setProcessInfo(String processInfo)
    {
    this.processInfo = processInfo;
    }
    public int getProgress()
    {
    return progress;
    }
    public void setProgress(int progress)
    {
    this.progress = progress;
    }
    public String getTitle()
    {
    return title;
    }
    public void setTitle(String title)
    {
    this.title = title;
    }
    }

    private static class ProgressListCellRenderer extends JPanel 
       implements ListCellRenderer { private JLabel titleIconLabel = new JLabel();
    private JLabel titleLabel = new JLabel();
    private JLabel progressInfoLabel = new JLabel();
    private JLabel stopLabel = new JLabel();
    private JProgressBar progressBar = new JProgressBar();
    private Color unfocusedSelBackground = new Color(245, 245, 245);

    private ProgressListCellRenderer() {
    super(new BorderLayout(5, 0));

    titleIconLabel.setIcon(new ImageIcon(ProgressList.class.getResource("imgs/titleIcon.png")));  //16x16 icon
    stopLabel.setIcon(new ImageIcon(ProgressList.class.getResource("imgs/stopIcon.png"))); //16x16 icon

    JPanel centerPane = new JPanel(new BorderLayout(0, 2));
    centerPane.setOpaque(false); centerPane.add(titleLabel, BorderLayout.NORTH);
    centerPane.add(progressBar, BorderLayout.CENTER);
    centerPane.add(progressInfoLabel, BorderLayout.SOUTH);
    this.add(titleIconLabel, BorderLayout.WEST);
    this.add(centerPane, BorderLayout.CENTER);
    this.add(stopLabel, BorderLayout.EAST); titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));
    titleIconLabel.setVerticalAlignment(JLabel.TOP);

    this.setOpaque(true);
    this.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 5));
    } public Component getListCellRendererComponent(
    JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
    {
    Progress progress = (Progress) value;
    progressBar.setValue(progress.getProgress());
    titleLabel.setText(progress.getTitle());
    progressInfoLabel.setText(progress.getProcessInfo());
    this.revalidate();

    if (isSelected) {
    setBackground(list.getSelectionBackground());
        titleLabel.setForeground(list.getSelectionForeground());
        progressInfoLabel.setForeground(list.getSelectionForeground());
    }
    else {
    setBackground(unfocusedSelBackground);
        titleLabel.setForeground(list.getForeground());
        progressInfoLabel.setForeground(list.getForeground());
    } return this;
    }

    protected void paintComponent(Graphics g)
    {
    ((Graphics2D)g).setRenderingHint(
    RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    super.paintComponent(g);
    }
    } private static int n = 0;

    public static void main(String[] args)
    {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
    e.printStackTrace();
    }

    final Random random = new Random();
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    final DefaultListModel model = new DefaultListModel();
    final JList list = new JList(model);
    list.setCellRenderer(new ProgressListCellRenderer());
    list.setFixedCellWidth(100);   /* important */ JButton btn = new JButton("Start");
    btn.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    final Progress progress = new Progress("Test Progress - " + (n++), 0, " ");
    model.addElement(progress);
    new Thread() {
    public void run()
    {
    while (true) {
    try {
    Thread.sleep(2000 + random.nextInt(200));
    } catch (InterruptedException e) {
    }
    int t = progress.getProgress();
    if (t >= 100) {
    return;
    } SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
    {
    int t = progress.getProgress() + random.nextInt(20);
    t = Math.min(t, 100);
    progress.setProgress(t);
    progress.setProcessInfo("Progress " + t + "% complete, current time is " + sdf.format(new Date()));
    int i = model.indexOf(progress);
    model.setElementAt(progress, i);

    if (t >= 100) {
    model.removeElement(progress);
    }
    }
    });
    }
    }
    }.start();
    }
    });
    JPanel btnPane = new JPanel();
    btnPane.add(btn);

    JScrollPane sp = new JScrollPane(list);
    JFrame f = new JFrame("Progress List Test");
    f.getContentPane().add(sp, BorderLayout.CENTER);
    f.getContentPane().add(btnPane, BorderLayout.SOUTH);
    f.setSize(400, 400);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }
    }
      

  4.   

    万分万分感谢gtlang78!强人啊!这正是我想要的效果。
    不过有一个问题:我若将ListCellRenderer中的stopLabel修改为JButton(使其带有停止功能),按钮是不能响应的,似乎它仅仅是paint了上去而并不带有按钮的功能。所有怎样才能使停止按钮能响应事件呢?谢谢!
      

  5.   

    用列表绘制器的方法实现按钮事件的相应比较麻烦,我用直接加入组件的方式重写了一遍import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Random;import javax.swing.*;public class ProgressList extends JPanel implements Scrollable
    {
    public ProgressList() 
    {
    super(null);
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    setBackground(Color.WHITE);
    } public Dimension getPreferredScrollableViewportSize()
    {
    return getPreferredSize();
    } public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
    {
    if (getComponentCount() > 0) {
    Component c = getComponent(0);
    return c.getHeight();
    }
    return 0;
    } public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
    {
    return orientation == SwingConstants.HORIZONTAL ? visibleRect.width : visibleRect.height;
    } public boolean getScrollableTracksViewportWidth()
    {
    return true;
    } public boolean getScrollableTracksViewportHeight()
    {
    return true;
    } private static class Progress extends JPanel { private static final ImageIcon CANCEL_BTN_ICON = new ImageIcon(ProgressList.class.getResource("imgs/cancelIcon.png"));
    private static final ImageIcon TITLE_ICON = new ImageIcon(ProgressList.class.getResource("imgs/titleIcon.png")); private static final Color SELECTION_BG = UIManager.getColor("List.selectionBackground");
    private static final Color SELECTION_FG = UIManager.getColor("List.selectionForeground");
    private static final Color UNSELECTION_BG = new Color(245, 245, 245);
    private static final Color UNSELECTION_FG = UIManager.getColor("List.foreground");
         private JLabel titleIconLabel = new JLabel();
    private JLabel titleLabel = new JLabel();
    private JLabel progressInfoLabel = new JLabel();
    private JButton cancelBtn = new JButton();
    private JProgressBar progressBar = new JProgressBar();
    private boolean selected;

    private Progress(String title, int progress, String processInfo) {
    super(new BorderLayout(5, 0)); titleLabel.setText(title);
    progressBar.setValue(progress);
    progressInfoLabel.setText(processInfo);

    titleIconLabel.setIcon(TITLE_ICON);
    cancelBtn.setIcon(CANCEL_BTN_ICON);

    JToolBar cancelBtnPane = new JToolBar();
    cancelBtnPane.setOpaque(false);
    cancelBtnPane.setFloatable(false);
    cancelBtnPane.setRollover(true);
    cancelBtnPane.setLayout(new BoxLayout(cancelBtnPane, BoxLayout.X_AXIS));
    cancelBtnPane.setBorder(null);
    cancelBtnPane.add(cancelBtn);

    cancelBtn.setContentAreaFilled(false);
    cancelBtn.setEnabled(isCancelable());
    cancelBtn.setMargin(new Insets(0,0,0,0));
    cancelBtn.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    cancel();
    }
    }); JPanel centerPane = new JPanel(new BorderLayout(0, 2));
    centerPane.setOpaque(false); centerPane.add(titleLabel, BorderLayout.NORTH);
    centerPane.add(progressBar, BorderLayout.CENTER);
    centerPane.add(progressInfoLabel, BorderLayout.SOUTH);
    this.add(titleIconLabel, BorderLayout.WEST);
    this.add(centerPane, BorderLayout.CENTER);
    this.add(cancelBtnPane, BorderLayout.EAST); titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));
    titleIconLabel.setVerticalAlignment(JLabel.TOP);

    this.setOpaque(true);
    this.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 5)); this.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    setSelected(!isSelected());
    }
    });

    setSelected(false);
    }

    public Dimension getMaximumSize()
    {
    Dimension d = getPreferredSize();
    d.width = Integer.MAX_VALUE; return d;
    }

    public void setSelected(boolean b)
    {
    this.selected = b;
    if (b) {
    setBackground(SELECTION_BG);
        titleLabel.setForeground(SELECTION_FG);
        progressInfoLabel.setForeground(SELECTION_FG);
    }
    else {
    setBackground(UNSELECTION_BG);
        titleLabel.setForeground(UNSELECTION_FG);
        progressInfoLabel.setForeground(UNSELECTION_FG);
    }
    }

    public boolean isSelected()
    {
    return selected;
    }

    protected void paintComponent(Graphics g)
    {
    ((Graphics2D)g).setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    GradientPaint gp = new GradientPaint(0, 0, getBackground().brighter(), 0, getHeight(), getBackground());

    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(gp);
    g2d.fillRect(0, 0, getWidth(), getHeight());
    }

    private void removeFromProgressList()
    {
    Container parent = getParent();
    if (parent != null) {
    parent.remove(this);
    parent.validate();
    parent.repaint();
    }
    }

    public int getProgress()
    {
    return progressBar.getValue();
    }

    public void setProgress(final int progress)
    {
    if (SwingUtilities.isEventDispatchThread()) {
    progressBar.setValue(progress);
    if (progress >= 100) {
    completed();
    }
    }
    else {
    SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
    {
    progressBar.setValue(progress);
    if (progress >= 100) {
    completed();
    }
    }
    });
    }
    }

    public String getProgressInfo() 
    {
    return progressInfoLabel.getText();
    } public void setProcessInfo(final String progressInfo)
    {
    if (SwingUtilities.isEventDispatchThread()) {
    progressInfoLabel.setText(progressInfo);
    }
    else {
    SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
    {
    progressInfoLabel.setText(progressInfo);
    }
    });
    }
    } protected boolean isCancelable() 
    {
    return true;
    }

    protected void cancel() 
    {
    removeFromProgressList();
    } protected void completed()
    {
    removeFromProgressList();
    }
    } private static int n = 0;

    public static void main(String[] args)
    {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    // UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
    } catch (Exception e) {
    e.printStackTrace();
    }

    final Random random = new Random();
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    final ProgressList progressList = new ProgressList(); JButton btn = new JButton("Start");
    btn.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    final Progress progress = new Progress("Test Progress - " + (n++), 0, " ");
    progressList.add(progress);
    progressList.revalidate();
    progressList.getParent().repaint();

    new Thread() {
    public void run()
    {
    while (true) {
    try {
    Thread.sleep(2000 + random.nextInt(200));
    } catch (InterruptedException e) {
    }
    int t = progress.getProgress();
    if (t >= 100) {
    return;
    } t += random.nextInt(20);
    t = Math.min(t, 100);
    progress.setProgress(t);
    progress.setProcessInfo("Progress " + t + "% complete, current time is " + sdf.format(new Date()));
    }
    }
    }.start();
    }
    });
    JPanel btnPane = new JPanel();
    btnPane.add(btn);

    JScrollPane sp = new JScrollPane(progressList);
    JFrame f = new JFrame("Progress List Test");
    f.getContentPane().add(sp, BorderLayout.CENTER);
    f.getContentPane().add(btnPane, BorderLayout.SOUTH);
    f.setSize(400, 400);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }
    }
      

  6.   

    感谢gtlang78,你的代码是可行的。
    我修改了一下,去掉了List的背景色渐变处理,并使其仅能单一选取。再次感谢。