首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

们帮小弟我看看这个JPanel为什么显示出来

2012-01-09 
大虾们帮我看看这个JPanel为什么显示出来郁闷呀..运行程序以后Panel显示不出来,大家给我说说怎么改呀....p

大虾们帮我看看这个JPanel为什么显示出来
郁闷呀..运行程序以后Panel显示不出来,大家给我说说怎么改呀....


package   com.swtdesigner;

import   java.awt.BorderLayout;
import   java.awt.event.ActionEvent;
import   java.awt.event.ActionListener;

import   javax.swing.JButton;
import   javax.swing.JFrame;
import   javax.swing.JPanel;
import   javax.swing.JScrollPane;
import   javax.swing.JTextArea;

public   class   TestArea   {

private   JFrame   frame;
public   static   final   int   DEFAULT_WIDTH   =   300;
      public   static   final   int   DEFAULT_HEIGHT   =   300;    

      private   JTextArea   textArea;
      private   JScrollPane   scrollPane;
      private   JPanel   buttonPanel;
      private   JButton   wrapButton;
/**
  *   Launch   the   application
  *   @param   args
  */
public   static   void   main(String   args[])   {
try   {

TestArea   window   =   new   TestArea();
window.frame.setVisible(true);
}   catch   (Exception   e)   {
e.printStackTrace();
}

}

/**
  *   Create   the   application
  */
public   TestArea()   {
initialize();
}

/**
  *   Initialize   the   contents   of   the   frame
  */
private   void   initialize()   {
    JFrame   frame   =   new   JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.setTitle( "TextAreaTest ");
            frame.setSize(DEFAULT_WIDTH,   DEFAULT_HEIGHT);

            buttonPanel   =   new   JPanel();
        //     buttonPanel.setBounds(200,200,   200,   200);

            //   add   button   to   append   text   into   the   text   area

            JButton   insertButton   =   new   JButton( "Insert ");
            buttonPanel.add(insertButton);
            insertButton.addActionListener(new
                  ActionListener()
                  {
                        public   void   actionPerformed(ActionEvent   event)
                        {
                              textArea.append( "The   quick   brown   fox   jumps   over   the   lazy   dog.   ");
                        }
                  });



            //   add   button   to   turn   line   wrapping   on   and   off

            wrapButton   =   new   JButton( "Wrap ");
            buttonPanel.add(wrapButton);
            wrapButton.addActionListener(new
                  ActionListener()
                  {
                        public   void   actionPerformed(ActionEvent   event)
                        {    
                              boolean   wrap   =   !textArea.getLineWrap();
                              textArea.setLineWrap(wrap);
                              scrollPane.revalidate();
                              wrapButton.setText(wrap   ?   "No   Wrap "   :   "Wrap ");
                        }
                  });
       
            frame.add(buttonPanel,   BorderLayout.SOUTH);

            //   add   a   text   area   with   scroll   bars
           
            textArea   =   new   JTextArea(8,   40);
            scrollPane   =   new   JScrollPane(textArea);

            frame.add(scrollPane,   BorderLayout.CENTER);
      }

}





[解决办法]
注意你的Frame

JFrame frame = new JFrame(); ---》不用重新定义。直接frame=new JFrame();
它把属性里的frame屏蔽了

热点排行