查看完整版本: [已解決]JFrame按按鈕顯示元件問題
頁: [1]

h10292 發表於 2010-1-3 02:27 PM

[已解決]JFrame按按鈕顯示元件問題

本帖最後由 h10292 於 2010-1-3 11:25 PM 編輯


import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Test extends JFrame
{
     private JLabel label;
     private JPanel panel;
     private JButton button;
  
     public Test()
     {
          super( "測式元件");               
          setLayout( null );
  
          button = new JButton( "顯示" );
          button.setBounds( 100, 100, 60, 30 );
          add( button );
          button.addActionListener(
               new ActionListener()
               {
                     public void actionPerformed( ActionEvent e )
                     {
                            panel = new JPanel( new FlowLayout() );
                            label = new JLabel( "測試用:" );
                            panel.add( label );
                            panel.setBounds( 300, 300, 100, 100 );
                            add( panel );
                     }
               }
         );
    }
         
    public static void main(String args[])
    {
         Test test = new Test();
          test.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          test.setSize( 500 , 500 );
          test.setVisible( true );
     }
}
我希望能在按按鈕之後顯示JPanel(含裡面的元件)

然而執行起來是不成功的

但如果將視窗縮小在打開後就能夠顯示

問:(1)為什麼將視窗縮小在打開後就能夠顯示?
       (2)如果想要像我本來希望的(按按鈕後直接顯示元件),那這程式要如何改寫?...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

gnu 發表於 2010-1-3 07:37 PM

this.updateUI();
?

h10292 發表於 2010-1-3 11:24 PM

原來如此

加入panel.updataUI( )    確實可以直接一點就出來

感謝gnu幫忙解決問題
頁: [1]