CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  GUI 设计

JFrame和JPanel里的布局为什么不一样呢?

楼主xzjjy(青松)2006-06-01 19:30:29 在 Java / GUI 设计 提问

从JBuilder   里找了个例子,在原来的基础上又回了个JPanel,界面就变了呢?  
  这是我改后的代码,大家看看这是什么问题。  
  import   java.awt.*;  
  import   java.awt.event.*;  
  import   javax.swing.*;  
  import   javax.swing.border.*;  
  import   java.awt.Rectangle;  
   
  public   class   Frame1   extends   JFrame   {  
   
      //UI   and   components  
      JPanel   contentPane;  
      JPanel   panel   =   new   JPanel();  
      JMenuBar   menuBar1   =   new   JMenuBar();  
      JMenu   menuFile   =   new   JMenu();  
      JMenuItem   menuFileExit   =   new   JMenuItem();  
      TitledBorder   titledBorder1;  
      JPasswordField   jPasswordField1   =   new   JPasswordField(10);  
      JTextField   jTextField1   =   new   JTextField();  
      JLabel   jLabel1   =   new   JLabel();  
      JLabel   jLabel2   =   new   JLabel();  
      JButton   jButton1   =   new   JButton();  
      JOptionPane   jOptionPane1   =   new   JOptionPane();   /**  
        *   Construct   the   frame  
        */  
      public   Frame1()   {  
          enableEvents(AWTEvent.WINDOW_EVENT_MASK);  
          try   {  
              jbInit();  
          }  
          catch(Exception   e)   {  
              e.printStackTrace();  
          }  
      }  
   
      /**  
        *   Component   initialization  
        *  
        *   @throws   Exception   exception  
        */  
      private   void   jbInit()   throws   Exception     {  
   
          //initialized   content   pane  
          contentPane   =   (JPanel)   this.getContentPane();  
          titledBorder1   =   new   TitledBorder("");  
          contentPane.setLayout(new   BorderLayout());  
          this.setSize(new   Dimension(350,   300));  
          this.setTitle("Password   Sample");  
   
          //initialized   menu   bar   and   menu   items,   as   well   as   menu   event   listeners  
          this.setJMenuBar(menuBar1);  
          menuFile.setText("File");  
          menuFileExit.setText("Exit");  
          menuFileExit.addActionListener(new   ActionListener()     {  
          public   void   actionPerformed(ActionEvent   e)   {  
                  fileExit_actionPerformed(e);  
              }  
          });  
   
          //initialize   password   field   -   set   echo   character   to   '#'   and   length  
          //of   field   to   10   columns.   Add   event   listener  
          jPasswordField1.setEchoChar('#');  
          jPasswordField1.setColumns(10);  
          jPasswordField1.setBounds(new   Rectangle(107,   77,   109,   28));  
          jPasswordField1.addActionListener(new   java.awt.event.ActionListener()   {  
              public   void   actionPerformed(ActionEvent   e)   {  
                  jPasswordField1_actionPerformed(e);  
              }  
          });  
   
          //initialize   text   field   for   message  
          jTextField1.setBackground(Color.red);  
          jTextField1.setFont(new   java.awt.Font("SansSerif",   1,   24));  
          jTextField1.setBorder(BorderFactory.createRaisedBevelBorder());  
          jTextField1.setCaretColor(Color.white);  
          jTextField1.setEditable(false);  
          jTextField1.setHorizontalAlignment(SwingConstants.CENTER);  
          jTextField1.setBounds(new   Rectangle(6,   145,   329,   101));  
   
          //initialize   labels  
          jLabel1.setText("Password   is:");  
          jLabel1.setBounds(new   Rectangle(5,   33,   109,   29));  
          jLabel2.setText("Enter   password:");  
          jLabel2.setBounds(new   Rectangle(6,   76,   99,   28));  
   
          //initialize   button   and   create   event   listener  
          jButton1.setFont(new   java.awt.Font("SansSerif",   0,   12));  
          jButton1.setBorder(BorderFactory.createRaisedBevelBorder());  
          jButton1.setText("Click   for   password");  
          jButton1.setBounds(new   Rectangle(106,   22,   142,   44));  
          jButton1.addActionListener(new   java.awt.event.ActionListener()   {  
              public   void   actionPerformed(ActionEvent   e)   {  
                  jButton1_actionPerformed(e);  
              }  
          });   //add   components  
          menuFile.add(menuFileExit);  
          menuBar1.add(menuFile);  
          panel.add(jTextField1,   null);  
          panel.add(jLabel1,   null);  
          panel.add(jLabel2,   null);  
          panel.add(jPasswordField1,   null);  
          panel.add(jButton1,   null);  
          contentPane.add(panel,BorderLayout.CENTER);  
      }  
   
      /**  
        *   File   |   Exit   action   performed  
        *  
        *   @param   e   ActionEvent  
        */  
      public   void   fileExit_actionPerformed(ActionEvent   e)   {  
          System.exit(0);  
      }  
   
      /**  
        *   Overridden   so   we   can   exit   when   window   is   closed  
        *  
        *   @param   e   WindowEvent  
        */  
      protected   void   processWindowEvent(WindowEvent   e)   {  
          super.processWindowEvent(e);  
          if   (e.getID()   ==   WindowEvent.WINDOW_CLOSING)   {  
              fileExit_actionPerformed(null);  
          }  
      }  
   
   
      private   boolean   isPasswordCorrect(char[]   input)   {  
          char[]   correctPassword   =   {   'j',   'b',   'u',   'i',   'l',   'd',   'e',   'r'   };  
          if   (input.length   !=   correctPassword.length)  
                    return   false;  
          for   (int   i   =   0;     i   <   input.length;   i   ++)  
                  if   (input[i]   !=   correctPassword[i])  
                      return   false;  
          return   true;  
          }  
   
   
      void   jPasswordField1_actionPerformed(ActionEvent   e)   {  
          String   correctPassword   =   new   String("Good   job!");  
          String   incorrectPassword   =   new   String("Opps!   Try   again!");  
   
          JPasswordField   input   =   (JPasswordField)e.getSource();  
              char[]   password   =   input.getPassword();  
                  if   (isPasswordCorrect(password))   {  
                      jTextField1.setText(correctPassword);  
                      JOptionPane.showMessageDialog(jOptionPane1,   "Success!   You   typed   the   right   password.");  
                    }  
                    else   {  
                      jTextField1.setText(incorrectPassword);  
                      JOptionPane.showMessageDialog(jOptionPane1,   "Invalid   password.   Try   again.",  
                                                  "Error   Message",   JOptionPane.ERROR_MESSAGE);  
                    }  
          }  
   
   
      void   jButton1_actionPerformed(ActionEvent   e)   {  
              jButton1.setText("jbuilder");  
      }  
  } 问题点数:20、回复次数:4Top

1 楼UnAgain()回复于 2006-06-01 19:41:08 得分 8

JFrame中的ContentPane的默认布局是BorderLayout,  
  而JPanel默认布局是FlowLayout。Top

2 楼UnAgain()回复于 2006-06-01 19:42:41 得分 0

加上下面的代码  
          panel.setLayout(new   BorderLayout());  
  即可。  
  Top

3 楼beyondone()回复于 2006-06-03 15:53:20 得分 7

JFrame中的ContentPane的默认布局是BorderLayout,  
  而JPanel默认布局是FlowLayout。  
   
  正解..Top

4 楼goooooooooo()回复于 2006-06-04 22:49:18 得分 5

upTop

相关问题

关键词

得分解答快速导航

  • 帖主:xzjjy
  • UnAgain
  • beyondone
  • goooooooooo

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo