WEB开发网
开发学院软件开发Java 为JTable添加JScrollPane给corner设置颜色 阅读

为JTable添加JScrollPane给corner设置颜色

 2009-09-22 00:00:00 来源:WEB开发网   
核心提示: 这样就效果如图三:图三不过当时不知道这个setOpaque(true),而是用了另外一个比较笨的方法,为JTable添加JScrollPane给corner设置颜色(2),如:JLabeljb=newJLabel();//jb.setOpaque(true);jb.setBackground(C

这样就效果如图三:

为JTable添加JScrollPane给corner设置颜色

图三

不过当时不知道这个setOpaque(true),而是用了另外一个比较笨的方法,如:

        JLabel jb = new JLabel();    
        //jb.setOpaque(true);
        jb.setBackground(Color.black);
        jb.setBorder(BorderFactory.createLineBorder(Color.red, 20));
        jScrollPane1.setCorner(JScrollPane.UPPER_RIGHT_CORNER, jb);

测试的代码如:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
public class KJTable extends JFrame{
    private JTable table;
    private KJTableCellRenderer renderer = new KJTableCellRenderer();
    private BorderLayout borderLayout = new BorderLayout();
    private JScrollPane jScrollPane1 = new JScrollPane();
    public KJTable(){
        this.setLayout(borderLayout);
        table = new JTable();
        table.setRowHeight(20);
        Object rows[][] ={
                { "王明", "中国", 44 },
                { "姚明", "中国", 25 },
                { "赵子龙", "西蜀", 1234 },
                { "曹操", "北魏", 2112 },
                { "Bill Gates", "美国", 45 },
                { "Mike", "英国", 33 },
                { "王明", "中国", 44 },
                { "姚明", "中国", 25 },
                { "赵子龙", "西蜀", 1234 },
                { "曹操", "北魏", 2112 },
                { "Bill Gates", "美国", 45 },
                { "Mike", "英国", 33 }};
        String columns[] = { "姓名", "国籍", "年龄" };
        TableModel model = new DefaultTableModel(rows, columns);
        JTable table = new JTable(model);    
        table.setModel(model);
        table.getTableHeader().setDefaultRenderer(renderer);
        table.setDefaultRenderer(Object.class, renderer);
        jScrollPane1.getViewport().add(table);
        getContentPane().add(jScrollPane1, BorderLayout.CENTER);        
        JLabel jb = new JLabel();    
        //jb.setOpaque(true);
        jb.setBackground(Color.black);
        jb.setBorder(BorderFactory.createLineBorder(Color.red, 20));
        jScrollPane1.setCorner(JScrollPane.UPPER_RIGHT_CORNER, jb);        
        this.setSize(300, 200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new KJTable();
    }    
    private class KJTableCellRenderer extends JLabel implements TableCellRenderer{
        public Component getTableCellRendererComponent(JTable table, Object value,
                            boolean isSelected, boolean hasFocus, int row, int col) {            
            if(row<0){
                setHorizontalAlignment(SwingConstants.CENTER);
                setBackground(new Color(45,145,180));
                setForeground(Color.white);
            }    
            else {
                setHorizontalAlignment(SwingConstants.LEFT);
                if(row %2 == 0){
                    setBackground(new Color(255,255,205));
                    setForeground(Color.blue);
                }
                else{
                    setBackground(new Color(255,220,150));
                    setForeground(Color.red);
                }            
            }        
            setText(value.toString());
            setOpaque(true);//
            return this;
        }    
    }
}

上一页  1 2 

Tags:JTable 添加 JScrollPane

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接