我在Frame里添加了一个panel,然后在panel里划线,如果划de线段超出屏幕的显示尺寸,如何作出滚动条呢,望高手赐教!希望给出源代码。

解决方案 »

  1.   

    给你个例子:import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
       
    public class JTextAreaExample extends JFrame implements
        ActionListener
    {
        public static void main(String[] argv)
        {
            JTextAreaExample mainApp = new JTextAreaExample();
        }
        
        public JTextAreaExample()
        {
            super("JTextArea Example");
            setBounds(0, 0, 300, 300);
            getContentPane().setLayout(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
            // Create the textarea...
            textarea = new JTextArea();
            textarea.setBounds(10, 10, 270, 200);  
            
            // NEW ->
            
            // Create a scrollpane
            scrollPane = new JScrollPane(JScrollPane.VERTICAL_
               SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.setBounds(10, 10, 270, 200);
            scrollPane.add(textarea);
            scrollPane.setViewportView(textarea);
            
            // <- NEW
                         
            // Create a button...
            button = new JButton("Copy Text Area to Console Window");
            button.setBounds(10, 240, 270, 25);
                            
            // Add the action listeners
            button.addActionListener(this);
                   
            // Add the objects to the content pane...
            getContentPane().add(scrollPane);   // MODIFIED
            getContentPane().add(button);
            
            setVisible(true);
        }
        
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == button)
            {
                System.out.println(textarea.getText());
            }
        }
        
        JButton   button;
        JTextArea textarea;
        JScrollPane scrollPane;   // NEW
    }