ScrollBar和JTable选择的问题

时间:2014-04-05 17:14:39

标签: java swing jtable jpanel jscrollpane

的问题:

  1. 即使最大化,也需要滚动条增加到面板高度。

  2. 点击1或2时,需要将选择颜色更改为其他颜色。

  3. 点击' x'时,字体应该更改。

  4. 代码:

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.EventQueue;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    import java.util.Vector;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.MatteBorder;
    import javax.swing.table.DefaultTableModel;
    
    import java.awt.Color;
    import javax.swing.border.CompoundBorder;
    import java.awt.Dimension;
    
    public class Sample1 extends JFrame {
    
        private JPanel contentPane;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Sample1 frame = new Sample1();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public Sample1() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);
    
            final JPanel panel = new JPanel();
            panel.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
            contentPane.add(panel, BorderLayout.WEST);
    
            Vector str = new Vector<>();
            Vector v = new Vector();
            v.add("1");
            v.add("x");
            Vector v1 = new Vector();
            v1.add("2");
            v1.add("x");
            str.addElement(v);
            str.addElement(v1);
            DefaultTableModel mo = new DefaultTableModel(str,str);
            final JTable list = new JTable(mo);
            list.setBorder(new CompoundBorder());
            list.setBackground(new Color(238, 232, 170));
            list.setShowGrid(false);
    //      list.setListData(str);
            list.getColumnModel().getColumn(1).setPreferredWidth(10);
            JScrollPane scrollPane = new JScrollPane(list);
            scrollPane.setPreferredSize(new Dimension(70, 300));
            scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            list.setTableHeader(null);
            list.setFillsViewportHeight(true); 
            panel.add(scrollPane);
    
            final JPanel panel_1 = new JPanel();
            panel_1.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
            contentPane.add(panel_1, BorderLayout.CENTER);
            panel_1.setLayout(new CardLayout(0, 0));
    
            JPanel panel_2 = new JPanel();
            panel_1.add(panel_2, "1");
    
            JLabel lblNewLabel = new JLabel("First");
            panel_2.add(lblNewLabel);
    
            JPanel panel_3 = new JPanel();
            panel_1.add(panel_3, "2");
    
            JLabel lblNewLabel_1 = new JLabel("Second");
            panel_3.add(lblNewLabel_1);
    
    
            list.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e){
                    int row = list.getSelectedRow();
                    int column = list.getSelectedColumn();
                    CardLayout cl = (CardLayout) panel_1.getLayout();
                    cl.show(panel_1,list.getValueAt(row, column).toString());
                }
            });
    
    //      list.add
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

  

即使最大化,也需要滚动条增加到面板高度。

JPanel panel = new JPanel(new BorderLayout());

这将解决您的第一个问题。