一个JFrame中放着一个JPanel
这个JPanel中放着一个JButton初始时,JPanel大小为100 * 100
当点击此JButton后,JPanel大小会自动的增加10 变成110 * 110
再点JButton,变成120 * 120
如此类推。如何解决此问题!?
谢谢高手门,急......
最好有点代码引导下.....

解决方案 »

  1.   

    在按钮事件里JPanel.setSize或者setPreferredSize 还有你要注意你的布局模式
      

  2.   

    我的组件布局模式 GridBagLayout 的
    直接使用setSize 或者 setPreferredSize就差不多了吗?
    不用重写paint 或者 paintComponent方法吗?
      

  3.   

    这样行吗?import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class PanelSize extends JFrame{
    private JPanel p = null;
    private int width = -1;
    private int height = -1;
    public PanelSize(String title)
    {
    super(title);
    p = new JPanel();
    JButton b = new JButton("add 10");

    width = 100;
    height = 100;
    b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
    width += 10;
    height += 10;
    setPanelSize();
    }

    });
    p.add(b);
    p.setSize(width, height);
    this.getContentPane().add(p);
    this.setSize(width, height);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void setPanelSize()
    {
    this.setSize(width, height);
    p.setSize(width, height);
    }

    public static void main(String args[])
    {
    new PanelSize("Test");
    }
    }
      

  4.   


    不用重写,个人认为可以使用setPrefferredSize实现。或者GridBagConstraints最后两个参数就是需要另外扩展的大小
      

  5.   

    PS,最好还是别用GridBagLayout,本身就是有Bug的
      

  6.   

    问题又有了郁闷死!在JDialog里面有个JPanel 而这个JPanel可以变化了!
    但JDialog还是不动!!!
      

  7.   

    个人认为,要解决这个问题,应该要约束JFrame的大小,也就是说每当JPanel的大小在改变的时候,JFrame也应该要改变,也就是说最后还应该有这么一个语句才能见效吧:
    jFrame.repaint();
    个人感觉罢了,呵呵,学习中
      

  8.   

    如果你不是要绘制自己的界面或制图,没必要重写paint 或者 paintComponent方法.
    设置大小jframe用setSize(),jpanel用setPreferredSize()就行了.
      

  9.   

    试了试import java.awt.Dimension;public class TestFrame extends javax.swing.JFrame {
        
        public TestFrame() {
            initComponents();
        }
                                
        private void initComponents() {
            jPanel1 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            jPanel1.setLayout(new java.awt.BorderLayout());        jPanel1.setPreferredSize(new java.awt.Dimension(100, 99));
            jButton1.setText("btn");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });        jPanel1.add(jButton1, java.awt.BorderLayout.CENTER);        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);        pack();
        }                           private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            Dimension size = new Dimension(jPanel1.getPreferredSize().width + 10, jPanel1.getPreferredSize().height + 10);
            jPanel1.setPreferredSize(size);
            this.pack();
        }                                        
        
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TestFrame().setVisible(true);
                }
            });
        }
                          
        private javax.swing.JButton jButton1;
        private javax.swing.JPanel jPanel1;