我的想法是通过一个按钮,控制显示一个jscrollpane,点一下按钮,jsrollpane就显示出来,再点一下,jscrollpane就隐藏.按钮和jscrollpane都在getContentpane()上!说得不是很明白,希望大家能理解,给点建议.谢谢.我的初步想法是通过计算按钮单击的次数来控制是否显示,如果单击的次数是奇数显示,偶数不显示.

解决方案 »

  1.   

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;public class shensuo extends JFrame { private JList list=new JList();
    private JScrollPane scrollPane = new JScrollPane();
    private int count=2;
    //private boolean zhankai=false;
    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    shensuo ss=new shensuo();
    ss.setSize(218,400);
    ss.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ss.setLocationRelativeTo(null);
    ss.setVisible(true); }
    public shensuo() {
    super();
    getContentPane().setLayout(null); final JButton button = new JButton();
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if(count%2==0)
    {
    scrollPane.setBounds(0, 23, 218, 202);
    getContentPane().add(scrollPane);
    scrollPane.setViewportView(list);
    }
    else
    {
    scrollPane.setBounds(0, 23, 0, 0);
    getContentPane().add(scrollPane);
    scrollPane.setViewportView(list);
    }
    count++;
    }
    });
    button.setText("我的联系人:");
    button.setBounds(0, 0, 218, 23);
    getContentPane().add(button);  

    }}