/*1.先定义面板
*2.在面板中定义组件
*3.把组件添加到面板中
*3.把面板添加到框架中
*
* 4.d定义一个监听器类
* 5.实例化一个对象
* 6.注册给“确认按钮”
*
*
*
*
* */
package com.log;
import java.awt.*;
import javax.swing.*;
public class Login extends JFrame
{
// 意思是: JPanel=new JPanel(); 错误:Syntax error on token "JPanel", VariableDeclaratorId expected after this token ,在JPanel处,应该有个变量声明
TextField tf1=new TextField("chihaojie");
String str=tf1.getText();
TextField tf2=new TextField(10);
JButton jb1=new JButton("确认");
JButton jb2=new JButton("取消");
//定义标签
JLabel jL1=new JLabel("用户名");
JLabel jL2=new JLabel("密码");
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
Login()
{
jp1.add(jL1);
jp1.add(tf1);
jp2.add(jL2);
jp2.add(tf2);
jp3.add(jb1);
jp3.add(jb2);
// 在框架的构造方法中,设置布局管理器
this.setLayout(new GridLayout(3,1)); // 学到了 使用 new GridLayout();作为参数 3行一列,在添加组件时,会不会发生覆盖???
this.add(jp1);
this.add(jp2);
this.add(jp3);
//设置框架属性
this.setSize(300,300);//设置框架大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
System.out.println("有输出没有?"+str);
}
public static void main(String[] args)
{
new Login();
//String str=tf1.getText();提示出错:Cannot make a static reference to the non-static field tf1 这里之所以编译器把tf1当成类,是因为,它没看到,tf1的创建语句。
}
//创建面板中的组件
//向面板中添加,组件
}
*2.在面板中定义组件
*3.把组件添加到面板中
*3.把面板添加到框架中
*
* 4.d定义一个监听器类
* 5.实例化一个对象
* 6.注册给“确认按钮”
*
*
*
*
* */
package com.log;
import java.awt.*;
import javax.swing.*;
public class Login extends JFrame
{
// 意思是: JPanel=new JPanel(); 错误:Syntax error on token "JPanel", VariableDeclaratorId expected after this token ,在JPanel处,应该有个变量声明
TextField tf1=new TextField("chihaojie");
String str=tf1.getText();
TextField tf2=new TextField(10);
JButton jb1=new JButton("确认");
JButton jb2=new JButton("取消");
//定义标签
JLabel jL1=new JLabel("用户名");
JLabel jL2=new JLabel("密码");
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
Login()
{
jp1.add(jL1);
jp1.add(tf1);
jp2.add(jL2);
jp2.add(tf2);
jp3.add(jb1);
jp3.add(jb2);
// 在框架的构造方法中,设置布局管理器
this.setLayout(new GridLayout(3,1)); // 学到了 使用 new GridLayout();作为参数 3行一列,在添加组件时,会不会发生覆盖???
this.add(jp1);
this.add(jp2);
this.add(jp3);
//设置框架属性
this.setSize(300,300);//设置框架大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
System.out.println("有输出没有?"+str);
}
public static void main(String[] args)
{
new Login();
//String str=tf1.getText();提示出错:Cannot make a static reference to the non-static field tf1 这里之所以编译器把tf1当成类,是因为,它没看到,tf1的创建语句。
}
//创建面板中的组件
//向面板中添加,组件
}