import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class simple01
{
 public static void main(String[] args)
 {
   LoginFrame frame =new LoginFrame();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.show();
 }
}
class LoginFrame extends JFrame
{
  public LoginFrame()
  {
    setTitle("simple");
    setSize(WIDTH,HEIGHT);
    Container contentPane = getContentPane();
    JPanel textPanel = new JPanel();
    JLabel inputPassword = new JLabel("请输入密码: ",JLabel.CENTER);
    myPassword = new JPasswordField(20);
    textPanel.add(inputPassword);
    textPanel.add(myPassword);
    JLabel checkedPassword =new JLabel("密码验证:",JLabel.LEFT);
    checkPassword =new JTextField(20);
    checkPassword.setEditable(false);
    textPanel.add(checkedPassword);
    textPanel.add(checkPassword);
    loginButton =new JButton("登录");
    loginButton.addActionListener(new LoginAction());
    textPanel.add(loginButton);
    contentPane.add(textPanel);
  }
private class LoginAction implements ActionListener
{
  public void actionPerformed(ActionEvent event)
  {
    char[] s=myPassword.getPassword();
    String gets = new String(s);
    if(gets.equals(CHECKPASSWORD))
    {
      checkPassword.setText("通过!");
    }
    else
    {
      checkPassword.setText("失败!");
    }
  }
  }
  public static final int WIDTH = 280;
  public static final int HEIGHT = 180;
  public static final String CHECKPASSWORD = "pasword";
  private JPasswordField myPassword;
  private JTextField checkPassword;
  private JButton loginButton;
}     
   这条语句“frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)”的具体意识和用法是什么?
    有可语句“Container contentPane = getContentPane()”,为什么可以没有语句setContentPane()?

解决方案 »

  1.   

    扣的真细啊 swing 一般都是自己生成代码的你问的问题 我也不是很清楚 学习ing
      

  2.   

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)”的意思就是JFrame退出就关闭
     getContentPane()获取值,要附值给Container contentPane 
      

  3.   

    如果是applet程序,你是不需要加frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)”这句话。默认关闭窗口是有效的。
    getContentPane(),是获得ContentPane 面板,在1.5 跟你直接在往 frame 上加,是没有“区别”的。不过强烈建议你往ContentPane 面板上
    增加组件。可以看看一个frame到底是由什么组成的。以上
        
      

  4.   

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)”的意思就是JFrame关闭时应用程序也关闭 
    getContentPane()获得窗体的内容面板,在JFrame上的所有控件都是放到内容面板上的(除了菜单),所以因该先得到内容面板,在往内容面板上添加控件
      

  5.   

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    The exit application default window close operation. If a window has this set as the close operation and is closed in an applet, a SecurityException may be thrown. It is recommended you only use this in an application.百度出来的javadoc中对于JFrame.EXIT_ON_CLOSE的解释,意思是在窗口关闭时推出程序。Container contentPane = getContentPane(); 
     The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case. For example, to add a child to an AWT frame you'd write:       frame.add(child);
     However using JFrame you need to add the child to the JFrame's content pane instead:       frame.getContentPane().add(child);
    意思是,awt中是直接向窗体添加子对象,但是,在swing中,你应该向jframe的内容板来添加对象