学习日记!java
* 11.18 *
"==" 和 "equals()"
== 比较两个对象是否是同一对象
equals 比较两个对象是否有匹配的值
下面的例子同时说明了== 和new 的区别
还有如何确定对象的类
//===============Test.java========
package test;
public class Test {
public static void main(String[] arguments){
String str1,str2;
str1 = "hello";
//String str2 = "hello";
str2=str1;\\指向同一对象
System.out.println("object?:"+(str1==str2));
System.out.println("value?:"+(str1.equals(str2)));
str2 = new String(str1);\\创建了同一个对象
System.out.println("object?:"+(str1==str2));
System.out.println("value?:"+(str1.equals(str2)));
String name = str1.getClass().getName();//确定对象的类
System.out.println(name);
}
}
问题
instanceof的用法
解决了
public class MyTest{
public static void main(String[] args){
String lable = new String("ok");
System.out.println(lable);
if (lable instanceof String )
System.out.println("Thisis String");
}
}
* 11.20 *
//一个用JFrame创建对话框的例子Test2.java
import javax.swing.JFrame;
class Test2 extends JFrame{
publicTest2(){
super("KingApex的第一个对话框");
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// setVisible(true);
}
publicstatic void main(String[] args){
System.out.println("开始创建对话框......");
Test2T = new Test2();
System.out.println("创建了一个对话框");
T.setVisible(true);
}
}
说明 :
setDefaultColseOperation 的参数:
1 EXIT_ON_CLOSE 当关闭框架时,退出应用程序
2 DISPOSE_ONCLOSE 关闭框架,处理框架对象,并继续运行应用程序
3 DO_NOTHENG_ON_CLOSE 打开框架,并继续运行
4 HIDE_ON_CLOSE 关闭框架,并继续运行
--------------------------------Test2.java-------------------------
//一个用JFrame创建对话框的例子,并用ExitWindow 类 来监视 窗口
import javax.swing.JFrame;
import java.awt.event.*;
class Test2 extends JFrame{
publicTest2(){
super("KingApex的第二个对话框");
setSize(300,500);
ExitWindowexit = new ExitWindow();
addWindowListener(exit);
setVisible(true);
}
publicstatic void main(String[] args){
System.out.println("开始创建对话框......");
Test2T = new Test2();
System.out.println("创建了一个对话框");
}
}
class ExitWindow extends WindowAdapter{
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
}
说明 :
ExitWindowexit = new ExitWindow();
addWindowListener(exit);
建立了ExitWindow 和 main框架的关系
--------------------------
//在容器中添加组件的例子
import javax.swing.*;
import java.awt.event.*;
class Test2 extends JFrame{
JButtonabort = new JButton("Abort");
JButtonretry = new JButton("Retry");
JButtonfail = new JButton("fail");
publicTest2(){
super("KingApex的第三个对话框");
setSize(300,500);
JPanelpane = new JPanel();
//添加组件
pane.add(abort);
pane.add(retry);
pane.add(fail);
//以窗格为参数调用setContentPane
setContentPane(pane);
ExitWindowexit = new ExitWindow();
addWindowListener(exit);
setVisible(true);
}
publicstatic void main(String[] args){
System.out.println("开始创建对话框......");
Test2T = new Test2();
System.out.println("创建了一个对话框");
// T.show();
}
}
class ExitWindow extends WindowAdapter{
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
}
------------------在上一个例子的基础上 在button上面放ImageIcon:-----------------
...
ImageIconicon = new ImageIcon("we.gif");
JButtonabort = new JButton(icon);
JPanelpane = new JPanel();
//添加组件
pane.add(abort);
......
//容器中添加组件2
import javax.swing.*;
import java.awt.event.*;
class Test2 extends JFrame{
publicTest2(){
super("KingApex的第四个对话框");
setSize(300,500);
ImageIconicon = new ImageIcon("we.gif");
JButtonabort = new JButton(icon);
JLabellabel = new JLabel(icon);
JTextFieldText = new JTextField("这是一个文本域",10);
JPanelpane = new JPanel();
//添加组件
pane.add(abort);
pane.add(label);
pane.add(Text);
//以窗格为参数调用setContentPane
setContentPane(pane);
ExitWindowexit = new ExitWindow();
addWindowListener(exit);
setVisible(true);
}
publicstatic void main(String[] args){
System.out.println("开始创建对话框......");
Test2T = new Test2();
System.out.println("创建了一个对话框");
// T.show();
}
}
class ExitWindow extends WindowAdapter{
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
}
说明 :
在相同目录下有一个we.gif
总结:
JFrame javax.swing.JFrame;
JWindow java.swing.JWindow
事件处理类java.awt.event.*
SWing 中的所有用户界面都继承自java.awt.Component
----------------------------事件监听器--------------------------
=========ChangeTile.java========
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class ExitWindow extends WindowAdapter{
publicvoid windowClosing(){
System.exit(0);
}
}
public class ChangeTitle extends JFrameimplements ActionListener{
JButtonb1 = new JButton ("b1");
JButtonb2 = new JButton ("b2");
publicChangeTitle(){
super("Titlebar");
setSize(300,200);
b1.addActionListener(this);
b2.addActionListener(this);
JPanelpane = new JPanel();
pane.add(b1);
pane.add(b2);
setContentPane(pane);
}
//ActionListenter的唯一的方法,b1 b2 的事件监听器
publicvoid actionPerformed(ActionEvent evt){
Objectsource = evt.getSource();//getSource返回发送事件的组对象
if(source == b1)
setTitle("YouClick b1");
elseif (source == b2)
setTitle("You Click b2");
repaint();//重绘窗口
}
publicstatic void main(String[] args){
JFrameframe = new ChangeTitle();
ExitWindowexit = new ExitWindow();
frame.addWindowListener(exit);
//frame.pack();
frame.setVisible(true);
}
}
* 11.21 *
我认为Thinking injava 很中经典的一句话:
“进行面向对象的设计时,一项基本的考虑是:如何将发生变化的东西与保持不变的东西分隔开。”
调用另一类的main:Cleanser.main(args);
判断自己到底应该选用合成还是继承,一个最简单的办法就是考虑是否需要从新类上溯造型回基础类。若必须上溯,就需要继承。但如果不需要上溯造型,就应提醒自己防止继承的滥用,要记住经常问自己“我真的需要上溯造型吗”
对final进行赋值处理——要么在定义字段时使用一个表达 式,要么在每个构建器中。这样就可以确保final字段在使用前获得正确的初始化。
将类定义成final后,结果只是禁止进行继承——没有更多的限制。然而,由于它禁止了继承,所以一个final类中的所有方法都默认为final。
[解决办法]
呵呵,有机会再来看看