点+按钮时候上面的文本域会变高并且把下面那个文本域覆盖了,如何做到不覆盖,下面的文本域自动下移package com.myframe;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class FrameTest extends JFrame{
    public FrameTest() {
    }
    public static void main(String args[]){
        SimpleFrame frame = new SimpleFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.pack();
        frame.setVisible(true);
    }
}class SimpleFrame extends JFrame{
    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEITH = 400;
    public static final int DEFAULTMAX_HEIGHT = 120;
    private JPanel panel;
    private JTextArea textArea,textArea1;
    private JScrollPane scrollPanel,scrollPanel1;
    private JButton bigbutton;
    private JToolBar bar;    public SimpleFrame(){
        setTitle("框框");
        setSize(DEFAULT_WIDTH,DEFAULT_HEITH);        bar = new JToolBar();
        bigbutton = new JButton("+");
        bigbutton.addActionListener(new AddAction());        bar.add(bigbutton);
        add(bar,BorderLayout.NORTH);        panel =  new JPanel();
        add(panel);
        bar.add(bigbutton);        textArea =  new JTextArea();
        textArea.setRows(5);
        textArea.setColumns(20);
        textArea.setLineWrap(true);
        scrollPanel = new JScrollPane(textArea);
        panel.add(scrollPanel);
        
        textArea1 = new JTextArea();
        textArea1.setRows(5);
        textArea1.setColumns(20);
        textArea1.setLineWrap(true);
        scrollPanel1 = new JScrollPane(textArea1);
        panel.add(scrollPanel1);        JPopupMenu popup = new JPopupMenu();
        textArea.setComponentPopupMenu(popup);
        JMenuItem item = new JMenuItem("变大");
        popup.add(item);
        item.addActionListener(new AddAction());
        textArea.add(popup);
    }    private class AddAction implements ActionListener{
        public void actionPerformed(ActionEvent event){
            int i= scrollPanel.getHeight();
            if(i<DEFAULTMAX_HEIGHT){
                scrollPanel.setBounds(
                        new Rectangle(scrollPanel.getX(), scrollPanel.getY(),
                                      scrollPanel.getWidth(),
                                      scrollPanel.getHeight() + 40));
                scrollPanel.updateUI();
          }
       }
    }
}

解决方案 »

  1.   

    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; public class FrameTest extends JFrame{ 
        public FrameTest() { 
        } 
        public static void main(String args[]){ 
            SimpleFrame frame = new SimpleFrame(); 
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            //frame.pack(); 
            frame.setVisible(true); 
        } 
    } class SimpleFrame extends JFrame{ 
        public static final int DEFAULT_WIDTH = 300; 
        public static final int DEFAULT_HEITH = 400; 
        public static final int DEFAULTMAX_HEIGHT = 130; 
        private JPanel panel; 
        private JTextArea textArea,textArea1; 
        private JScrollPane scrollPanel,scrollPanel1; 
        private JButton bigbutton; 
        private JToolBar bar;     public SimpleFrame(){ 
            setTitle("dfsd"); 
            setSize(DEFAULT_WIDTH,DEFAULT_HEITH);         bar = new JToolBar(); 
            bigbutton = new JButton("+"); 
            bigbutton.addActionListener(new AddAction());         bar.add(bigbutton); 
            add(bar,BorderLayout.NORTH);         panel =  new JPanel(); 
            add(panel); 
            bar.add(bigbutton);         panel.setLayout(null);
            
            textArea =  new JTextArea(); 
            textArea.setRows(5); 
            textArea.setColumns(20); 
            textArea.setLineWrap(true); 
            scrollPanel = new JScrollPane(textArea); 
            scrollPanel.setBounds(new Rectangle(20,20,100,50));
            panel.add(scrollPanel); 
            
            textArea1 = new JTextArea(); 
            textArea1.setRows(5); 
            textArea1.setColumns(20); 
            textArea1.setLineWrap(true); 
            scrollPanel1 = new JScrollPane(textArea1); 
            scrollPanel1.setBounds(new Rectangle(20,100,100,50));
            panel.add(scrollPanel1);         JPopupMenu popup = new JPopupMenu(); 
            textArea.setComponentPopupMenu(popup); 
            JMenuItem item = new JMenuItem("變大"); 
            popup.add(item); 
            item.addActionListener(new AddAction()); 
            textArea.add(popup); 
        }     private class AddAction implements ActionListener{ 
            public void actionPerformed(ActionEvent event){ 
                int i= scrollPanel.getHeight(); 
                if(i <DEFAULTMAX_HEIGHT){ 
                    scrollPanel.setBounds( 
                            new Rectangle(scrollPanel.getX(), scrollPanel.getY(), 
                                          scrollPanel.getWidth(), 
                                          scrollPanel.getHeight() + 40)); 
                    scrollPanel.updateUI(); 
                    if (scrollPanel.getY()+ scrollPanel.getHeight()> scrollPanel1.getY()){
                     scrollPanel1.setBounds(new Rectangle(scrollPanel1.getX(), scrollPanel.getY()+scrollPanel.getHeight()+20,scrollPanel1.getWidth(), 
                                scrollPanel1.getHeight()));
                     panel.add(scrollPanel1);
                    }
                    scrollPanel1.updateUI(); 
              } 
          } 
        } 
      

  2.   

    别用null布局,不就省了好多麻烦吗
      

  3.   

    這個用box應該挺合適    你找個小例子做做吧    我不常用布局~   都是用null~~ 也不熟悉
      

  4.   

    布局的话,要想用好SWING,GridBagLagout是必须要熟悉的。推荐LZ去看下这方面的资料。不过LZ的问题用Grid就可以实现了