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()?
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()?
解决方案 »
- 为什么结果是三- -?
- java对象回收的问题
- 用timer怎么做到每天的1点和2点时执行一个任务
- 发现一个特别快的,附件特别大的免费邮箱。大家快抢啊。
- 关于JFileChooser只选择文件夹的问题(必结贴)
- 一个再java基础中转换界面的问题,各位高手帮帮小弟,急……(解决问题后保证给分,谢谢)
- jb8中怎么把一些包加入classpath中?
- 我都不知道这是不是菜鸟问题了。有没有纯java的方法能够快速地获得某个目录下的所有文件的基本信息?
- 匿名内部类问题,请进
- 请求各位帮忙,关于绘制图像
- 大家帮忙看看这段代码为什么错误呢?
- Integer类中的decode()和valueOf()方法有什么不同么?
getContentPane()获取值,要附值给Container contentPane
getContentPane(),是获得ContentPane 面板,在1.5 跟你直接在往 frame 上加,是没有“区别”的。不过强烈建议你往ContentPane 面板上
增加组件。可以看看一个frame到底是由什么组成的。以上
getContentPane()获得窗体的内容面板,在JFrame上的所有控件都是放到内容面板上的(除了菜单),所以因该先得到内容面板,在往内容面板上添加控件
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的内容板来添加对象