终于明白为什么教科书上都喜欢用构造方法构造窗体。而不是直接写在main里
来源:互联网 发布:什么软件 看翡翠台 编辑:程序博客网 时间:2024/05/02 02:58
package com.study;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//Cannot make a static reference to the non-static field txtUser
//不能对非静态字段 txtUser 进行静态引用
public class QQLogin2 extends JFrame implements ActionListener {
/**
* 属性serialVersionUID
*/
private static final long serialVersionUID = 1L;
JTextField txtUser = new JTextField();
JPasswordField txtPass = new JPasswordField();
this.setSize(250, 125);
// new组件
JLabel labUser = new JLabel("用户名");
JLabel labPass = new JLabel("密码");
JButton btnLogin = new JButton("登陆");
JButton btnReg = new JButton("注册");
JButton btnCancel = new JButton("取消");
// 注册事件监听
btnLogin.addActionListener(this);
btnReg.addActionListener(this);
btnCancel.addActionListener(this);
// 布置输入面板
JPanel panInput = new JPanel();
panInput.setLayout(new GridLayout(2, 2));
panInput.add(labUser);
panInput.add(txtUser);
panInput.add(labPass);
panInput.add(txtPass);
// 布置按钮面板
JPanel panButton = new JPanel();
panButton.setLayout(new FlowLayout());
panButton.add(btnLogin);
panButton.add(btnReg);
panButton.add(btnCancel);
// 布置窗体
this.setLayout(new BorderLayout());
this.add(panInput, BorderLayout.CENTER);
this.add(panButton, BorderLayout.SOUTH);
}
public static void main(String args[]) {
QQLogin2 w = new QQLogin2();
w.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getActionCommand().equals("登陆")) {
String u = txtUser.getText();
String p = txtPass.getPassword().toString();
}
if (arg0.getActionCommand().equals("注册")) {
System.out.println("用户点了注册");
}
if (arg0.getActionCommand().equals("取消")) {
System.out.println("用户点了取消");
}
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//Cannot make a static reference to the non-static field txtUser
//不能对非静态字段 txtUser 进行静态引用
public class QQLogin2 extends JFrame implements ActionListener {
/**
* 属性serialVersionUID
*/
private static final long serialVersionUID = 1L;
JTextField txtUser = new JTextField();
JPasswordField txtPass = new JPasswordField();
//构造方法-终于明白为什么教科书上都喜欢用构造方法构造窗体。而不是直接写在main里。
//(因为main是静态的方法,里面不能直接使用-非静态的成员。liuhailun20150317)
public QQLogin2() {this.setSize(250, 125);
// new组件
JLabel labUser = new JLabel("用户名");
JLabel labPass = new JLabel("密码");
JButton btnLogin = new JButton("登陆");
JButton btnReg = new JButton("注册");
JButton btnCancel = new JButton("取消");
// 注册事件监听
btnLogin.addActionListener(this);
btnReg.addActionListener(this);
btnCancel.addActionListener(this);
// 布置输入面板
JPanel panInput = new JPanel();
panInput.setLayout(new GridLayout(2, 2));
panInput.add(labUser);
panInput.add(txtUser);
panInput.add(labPass);
panInput.add(txtPass);
// 布置按钮面板
JPanel panButton = new JPanel();
panButton.setLayout(new FlowLayout());
panButton.add(btnLogin);
panButton.add(btnReg);
panButton.add(btnCancel);
// 布置窗体
this.setLayout(new BorderLayout());
this.add(panInput, BorderLayout.CENTER);
this.add(panButton, BorderLayout.SOUTH);
}
public static void main(String args[]) {
QQLogin2 w = new QQLogin2();
w.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent arg0) {
if (arg0.getActionCommand().equals("登陆")) {
String u = txtUser.getText();
String p = txtPass.getPassword().toString();
}
if (arg0.getActionCommand().equals("注册")) {
System.out.println("用户点了注册");
}
if (arg0.getActionCommand().equals("取消")) {
System.out.println("用户点了取消");
}
}
}
0 0
- 终于明白为什么教科书上都喜欢用构造方法构造窗体。而不是直接写在main里
- 为什么很多人都喜欢new一个类,而不是直接声明一个对象呢?
- 为什么List的构造方法是ArrayList(),而不是同名的List()?
- 鲁迅的文章从教科书里消失了,花60年时间终于弄明白了他是在骂谁
- 大流量网站,CSS为什么直接写在页面里?
- 反射(构造方法,方法,main,字段)
- 构造方法为什么不能继承
- @Autowired写在变量上和构造器上的区别
- 为什么服务器都喜欢用LINUX?
- 构造方法的作用,为什么要写构造方法?
- 为什么单起一个service而不是直接在Activity里用thread或者asynctask来load图片那?
- 为什么构造方法不是静态方法
- 终于明白c++中为什么返回的istream等对象能直接进行bool判断
- 那个叫鲁迅的终于从教科书里消失了
- 那个叫鲁迅的终于从教科书里滚蛋了
- 为什么单例模式构造方法私有
- 构造函数 & main函数
- main中静态方法,静态块,构造方法执行顺序
- new && new(std::nothrow)
- iOS常用加密方法(aes、md5、base64)
- java 通过继承类AbstractRoutingDataSource 而实现的 多数据源切换 的 缺陷(二)
- mutex_lock在不同编译选项下的一个坑
- RGB、YUV和YCbCr
- 终于明白为什么教科书上都喜欢用构造方法构造窗体。而不是直接写在main里
- 微信js_sdk的分享功能的问题
- android wifi通信1
- Android 从零开始
- Leetcode: Implement strStr()
- JAVA进阶学习-java基本类型装箱、拆箱
- 成为高级程序员的 10 个步骤
- HDU-1212-Big Number
- 两种动态代理的实现方式