• 重写swing UI


    http://stackoverflow.com/

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.util.Vector;

    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.border.BevelBorder;
    import javax.swing.border.MatteBorder;
    import javax.swing.border.SoftBevelBorder;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.JTableHeader;

    public class JTableMouseMovingEvent extends JFrame
    {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        /**  */
        private JTable table;

        /**  */
        private String[] columnName = new String[] {"title1", "name", "age"};

        /**  */
        private DefaultTableModel model = null;

        /**  */
        private int rowCount = -1;

        /**
         * Launch the application
         *
         * @param args
         */
        public static void main(String args[])
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    try
                    {
                        JTableMouseMovingEvent frame = new JTableMouseMovingEvent();
                        frame.setVisible(true);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            });
        }

        /**
         * Create the frame
         */
        public JTableMouseMovingEvent()
        {
            super();
            setBounds(100, 100, 500, 375);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            final JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            getContentPane().add(panel, BorderLayout.CENTER);

            final JScrollPane scrollPane = new JScrollPane();
            panel.add(scrollPane, BorderLayout.CENTER);

            table = new JTable();
            //table.setBorder(new MatteBorder(2, 2, 2, 2, Color.GREEN));
            table.setGridColor(Color.DARK_GRAY);
            table.setRowHeight(30);
    //        table.setShowGrid(false);
    //        table.setShowVerticalLines(false);
    //        table.setShowHorizontalLines(false);
            //table.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
            table.addMouseMotionListener(new MouseMotionAdapter()
            {
                @Override
                public void mouseMoved(final MouseEvent e)
                {
                    rowCount = table.rowAtPoint(e.getPoint());
                    int row = table.rowAtPoint(e.getPoint());
                    int col = table.columnAtPoint(e.getPoint());
                    table.setRowSelectionInterval(row, row);
                    table.setColumnSelectionInterval(col, col);
                    //table.setForeground(new java.awt.Color(0, 255, 51));
                    //table.setSelectionBackground(Color.red);//设置选择行的颜色
                }
            });

            table.addMouseListener(new java.awt.event.MouseAdapter()
            {
                @Override
                public void mouseReleased(java.awt.event.MouseEvent e)
                {
                    if (e.getClickCount() == 1 && SwingUtilities.isRightMouseButton(e))
                    {
                        int row = table.rowAtPoint(e.getPoint());
                        int col = table.columnAtPoint(e.getPoint());
                        table.setRowSelectionInterval(row, row);
                        table.setColumnSelectionInterval(col, col);

                    }
                }
            });

            scrollPane.setViewportView(table);
            model = new DefaultTableModel(null, columnName);
            Vector<String> row = null;
            for (int i = 0; i < 10; i++)
            {
                row = new Vector<String>();
                row.add("" + i);
                row.add("table" + i);
                row.add("" + (20 + i));
                model.addRow(row);
            }
            table.setModel(model);
            table.setDefaultRenderer(Object.class, new TableCellRenderer());
            //
        }

        class TableCellRenderer extends DefaultTableCellRenderer
        {
            /**  */
            private static final long serialVersionUID = -5172589582171358319L;

            /** {@inheritDoc} */
            @Override
            public Component getTableCellRendererComponent(JTable jtable, Object value, boolean isSelected,
                boolean hasFocus, int row, int column)
            {

                JComponent c =
                    (JComponent)super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                JTableHeader head = table.getTableHeader();
                if (head != null)
                {
                    head.setBackground(Color.gray);
                    head.setForeground(Color.white);
                    head.setPreferredSize(new Dimension(300, 40));
                    head.setBorder(new SoftBevelBorder(BevelBorder.RAISED));
                    //head.setFont(new Font("宋体",Font.PLAIN,14));
                }
                JLabel label =
                    (JLabel)super.getTableCellRendererComponent(jtable, value, isSelected, hasFocus, row, column);
                label.setHorizontalAlignment(SwingConstants.LEFT);
                //label.setBorder(new MatteBorder(1, 1, 1, 1, new Color(120,120,120)));
                label.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
                label.setText("    "+label.getText());
                label.setFont(new Font("", Font.PLAIN, 16));
                if (row == rowCount)
                {
                    label.setBackground(Color.red);
                    //label.setFont(new Font("", Font.PLAIN, 15));
                    //label.setForeground(new java.awt.Color(0, 255, 51));
                    //label.setBorder(new MatteBorder(2, 2, 2, 2, Color.GREEN));
                }
                else
                {
                    label.setBackground(new Color(255, 255, 255));
                }

                return label;
            }
        }

    }

  • 相关阅读:
    周鸿祎:很多程序员聪明,但我一看就知道他不会成功
    Ubuntu/centos/redhat/SUSE sipp安装(带rtp支持,3.5.1版本)
    ffmpeg源码分析之媒体打开过程
    搜集的动植物分类、检索网站
    sipp命令 各参数含义
    最简单的一个win32程序
    vi学习笔记
    删除结点 (双向链表)
    插入结点(双向链表)
    La=LaULb (单链表)
  • 原文地址:https://www.cnblogs.com/standy225/p/3083940.html
Copyright © 2020-2023  润新知