6.TextField and TextArea(文本文框)
文本文档以及事件操作: 基本思想: 标签 :Label=new Label(); 文本文框: TextField =new TextField(); 按钮:Button =new Button(); 对文本问框进行加密: textField_user.setEchoChar('*'); 获得文本框中的的内容:TextField textField=(TextField) (e.getSource());
JavaBean类
package KuangStudy; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Student extends Frame implements ActionListener { //按钮 Button button_login=new Button("登入"); Button button_cancel=new Button("退出"); //文本文框 TextField textField_user=new TextField(); TextField textField_password=new TextField(); public Student(){ super.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { if(JOptionPane.showConfirmDialog(null,"您确定要退出此界面?")==0){ System.exit(0); } } }); this.setSize(300,300); this.setBackground(Color.BLUE); this.setLocationRelativeTo(null); //标签 Label label_user=new Label("账号"); Label label_password=new Label("密码"); //第一次整合 Panel panel_one=new Panel(); panel_one.setLayout(new GridLayout(2,2)); panel_one.add(label_user); panel_one.add(textField_user); panel_one.add(label_password); panel_one.add(textField_password); //进行第二次整合 Panel panel_two=new Panel(); panel_two.setLayout(new FlowLayout()); panel_two.add(button_login); panel_two.add(button_cancel); //进行画板的整合 this.setLayout(new BorderLayout()); this.add(panel_one,BorderLayout.CENTER); this.add(panel_two,BorderLayout.SOUTH); //设置替换编码 textField_user.setEchoChar('*'); //进行监听 getTextField_user().addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { // TextField textField=(TextField) (e.getSource()); System.out.println(textField.getText()); } public Button getButton_login() { return button_login; } public Button getButton_cancel() { return button_cancel; } public TextField getTextField_user() { return textField_user; } public TextField getTextField_password() { return textField_password; } }
Test类:
package KuangStudy; public class Test { public static void main(String[] args) { Student student=new Student(); student.setVisible(true); } }
7.calc 计算器的操作
基本思路: 字符串转整形: intger.parseint(字符串A) 把字符串转换成数字 对文本框的内容进行清空的操作 : textField.setText("");
JavaBean类
package KuangStudy; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Student extends Frame { //3个文本框 TextField textField_one=new TextField(10); //文本框最多储存的长度 TextField textField_two=new TextField(10); TextField textField_three=new TextField(10); //1个按钮 Button button=new Button("="); public Student(){ this.setLocationRelativeTo(null); //一个标签 Label label=new Label("+"); 设置监听,文本文框的内容设置为构造器,便于提取 button.addActionListener(new MyImplement(textField_one,textField_two,textField_three)); //进行窗体布局 this.setLayout(new FlowLayout()); this.add(textField_one); this.add(label); this.add(textField_two); this.add(button); this.add(textField_three); pack(); } public TextField getTextField_one() { return textField_one; } public TextField getTextField_two() { return textField_two; } public TextField getTextField_three() { return textField_three; } public Button getButton() { return button; } }
接口类:
package KuangStudy; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MyImplement implements ActionListener { private TextField num1,num2,num3; 构造器进行获取数据 public MyImplement(TextField num1,TextField num2,TextField num3){ this.num1=num1; this.num2=num2; this.num3=num3; } @Override public void actionPerformed(ActionEvent e) { //获得加数 int a=Integer.parseInt(num1.getText()); int b=Integer.parseInt(num2.getText()); //求和 num3.setText(""+(a+b)); //清屏 num1.setText(""); num2.setText(""); } }
测试类:
package KuangStudy; public class Test { public static void main(String[] args) { Student student=new Student(); student.setVisible(true); } }
8.calc 计算机的操作(组合类)
基本思路 : 清空的操作: setText() 字符串转阿拉布数字 Integr.parseint(字符串)
JavaBean类
package KuangStudy; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Student extends Frame implements ActionListener { //3个文本框 TextField textField_one=new TextField(10); //文本框最多储存的长度 TextField textField_two=new TextField(10); TextField textField_three=new TextField(10); //1个按钮 Button button=new Button("="); public Student(){ this.setLocationRelativeTo(null); //一个标签 Label label=new Label("+"); //进行窗体布局 this.setLayout(new FlowLayout()); this.add(textField_one); this.add(label); this.add(textField_two); this.add(button); this.add(textField_three); pack(); //进行设置监听: getButton().addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { int a=Integer.parseInt(textField_one.getText()); int b=Integer.parseInt(textField_two.getText()); textField_three.setText(""+(a+b)); textField_one.setText(""); textField_two.setText(""); } public TextField getTextField_one() { return textField_one; } public TextField getTextField_two() { return textField_two; } public TextField getTextField_three() { return textField_three; } public Button getButton() { return button; } }
Test类:
package KuangStudy; public class Test { public static void main(String[] args) { Student student=new Student(); student.setVisible(true); } }
9.Frame下的Paint
基本思想: 1.对paint()的覆盖 2.画板添加到框体中去
package KuangStudy; import java.awt.*; public class Test { public static void main(String[] args) { Frame frame=new Frame("傻子"); frame.setSize(700,500); Student student=new Student(); frame.setBackground(Color.CYAN); frame.add(student); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
package KuangStudy; import java.awt.*; public class Student extends Panel { public void paint(Graphics g){ g.fillOval(200,200,100,100); Color c=new Color(255,5,5); g.setColor(c); } }