定义出跟图片一样的标签三角标志是为了排序的
鼠标点击了标签 就会变成向上排序 则标签显示为name 和 上三角
鼠标再点击标签 就会变成向下排序 则标签显示为name 和 下三角谢谢 各位了 ~

解决方案 »

  1.   


    import java.awt.*;
    import java.awt.event.*;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;public class Test1 extends JFrame { private static final long serialVersionUID = 1L;
    private final JButton buttonUp;
    private final JButton buttonDown; public static void main(String[] args) {
    Test1 t = new Test1();
    } public Test1() { setSize(200, 300);
    setLocation(200, 300);
    setLayout(new FlowLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buttonUp = new JButton("Name ↑");
    buttonDown = new JButton("Name ↓");
    getContentPane().add(buttonUp);
    getContentPane().add(buttonDown);
    buttonDown.setVisible(false); buttonUp.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) { buttonUp.setVisible(false);
    buttonDown.setVisible(true);
    JOptionPane.showMessageDialog(null, "实现降序"); }
    });
    buttonDown.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) { buttonDown.setVisible(false);
    buttonUp.setVisible(true);
    JOptionPane.showMessageDialog(null, "实现升序"); }
    });
    setVisible(true);
    }}
    不知道这样行不行?
      

  2.   

    用一个button就行了,两个有点浪费了.
            ImageIcon ico=new ImageIcon("图片地址");
            ico.setImage(ico.getImage().getScaledInstance(50,20,Image.SCALE_DEFAULT));
            buttonUp = new JButton(ico);小三角和文字都做成图片比较简单.
      

  3.   


    package demo;import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.border.LineBorder;public class DemoForLabel extends JFrame {
    private static final long serialVersionUID = -1134814145773051971L;
    private String strUp="▲";
    private String strDown="▼";
    private String strName="Name";
    private boolean isUp=true;
    /**
     * Launch the application
     * @param args
     */
    public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    DemoForLabel frame = new DemoForLabel();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }
    public DemoForLabel() {
    super();
    getContentPane().setLayout(null);
    setBounds(100, 100, 253, 188);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel nameLabel = new JLabel();
    nameLabel.setBorder(new LineBorder(Color.LIGHT_GRAY, 1, false));
    nameLabel.addMouseListener(new MouseAdapter() {
    public void mouseClicked(final MouseEvent e) {
    isUp=!isUp;
    if(isUp)
    {
    nameLabel.setText(strName+" "+strUp);
    //下面添加实现排序的相关算法
    }
    else
    {
    nameLabel.setText(strName+" "+strDown);
    //下面添加实现排序的相关算法
    }
    }
    });
    nameLabel.setText(strName+" "+strUp);
    nameLabel.setBounds(27, 28, 68, 18);
    getContentPane().add(nameLabel);
    }}
      

  4.   

    能用JDK6吗?
    如果可以,使用其java.swing.table.TableRowSorter可实现表格排序,省却许多麻烦。
    就算不可以用JDK6,也可以看看相关的源码,然后照猫画虎。以下是使用TableRowSorter的效果图:
      

  5.   

    顶,接楼上import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.RowSorter;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;public class Test {
    public static void main(String args[]) {
    Runnable runner = new Runnable() {
    public void run() {
    JFrame frame = new JFrame("Sorting JTable");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "AMZN", "Amazon", 41.28 },
    { "EBAY", "eBay", 41.57 },
    { "GOOG", "Google", 388.33 },
    { "MSFT", "Microsoft", 26.56 },
    { "NOK", "Nokia Corp", 17.13 },
    { "ORCL", "Oracle Corp.", 12.52 },
    { "SUNW", "Sun Microsystems", 3.86 },
    { "TWX", "Time Warner", 17.66 },
    { "VOD", "Vodafone Group", 26.02 },
    { "YHOO", "Yahoo!", 37.69 } };
    String columns[] = { "Symbol", "Name", "Price" };
    TableModel model = new DefaultTableModel(rows, columns) {
    public Class getColumnClass(int column) {
    Class returnValue;
    if ((column >= 0) && (column < getColumnCount())) {
    returnValue = getValueAt(0, column).getClass();
    } else {
    returnValue = Object.class;
    }
    return returnValue;
    }
    }; JTable table = new JTable(model);
    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(
    model);

    table.setRowSorter(sorter);
    JScrollPane pane = new JScrollPane(table);
    frame.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
    }
    };
    EventQueue.invokeLater(runner);
    }
    }