JAVA自学笔记25
1、GUI
1)图形用户接口,以图形的方式,来显示计算机操作的界面,更方便更直观
2)CLI
命令行用户接口,就是常见的Dos,操作不直观
3)
类Dimension
类内封装单个对象组件中组件的宽度和高度(精确到整数)
Dimension(int width,int height);
类Point
Frame f=new Frame();
f.setTitle("dd");
Dimension d=new Dimension(100,200);
Point p=new Point(400,200);
f.location(p);
Thread.sleep(3000);
f.setVisible(true);
2、事件监听器
1)把事件源和事件关联起来
2)
Frame f=new Frame();
f.setTitle("dd");
f.setBounds(100,200,300,400);/
Thread.sleep(3000);
f.addWindowListener(new WindowListener()
{
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});
f.setVisible(true);
3)适配器设计模式
接口须重写方法较多且不需都实现时,可提供一个适配器类(仅仅空实现即可),在实现类重写即可
接口–适配器–实现类
WindowAdapter
适配器类
Frame f=new Frame();
f.setTitle("dd");
f.setBounds(100,200,300,400);
Thread.sleep(3000);
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
f.setVisible(true);
4)布局

Frame f=new Frame();
f.setTitle("dd");
f.setBounds(100,200,300,400);
Thread.sleep(3000);
f.setLayout(new FlowLayout());
Button bu=new Button("按钮");
bu.setSize(10,20);
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
bu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("success");
}
});
f.setVisible(true);
Frame f=new Frame("数据转移");
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
final TextField tf=new TextField(20);
Button bu=new Button("数据转移");
final TextAreas ta=new TextArea(10,40);
bu.addActionListener(new ActionListener)(){
ppublic void actionPerformed(ActionEvent e){
String tf_str=tf.getText().trim();
ta.append(tf_stf+"
");
}
tf.requestFocus();
tf.setText("");
});
f.add(tf);
f.add(bu);
f.add(ta);
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
f.setVisible(true);
final Frame f=new Frame("更改背景色");
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
Button redButton=new Button("red");
f.add(redButton);
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
redButton.addMouseListener(new MouseAdapter)(){
public void mouseEntered(MouseEvent e){
f.setBackground(color.RED);
}
});
redButton.addMouseListener(new MouseAdapter)(){
public void mouseExited(MouseEvent e){
f.setBackground(color.WHITE);
}
});
f.setVisible(true);
final Frame f=new Frame("只能输入数字字符");
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
Label label=new Label("请输入QQ号码仅为数字");
TextFiled tf=new TextField(40);
tf.addKeyListener((new) KeyAdapter({
public void KeyPressed(KeyEvent e){
char ch=e.getKeyChar();
if(!(ch>='0'&&ch<='9')){}
e.consume();
}
});
f.add(label);
f.add(tf);
f.setVisible(true);
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
菜单组件:


final Frame f=new Frame("只能输入数字字符");
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
MenuBar mb=new MenuBar();
Menu m=new Menu("文件");
MenuItem mi=new MenuItem("退出系统");
m.add(mi);
mb.add(m);
f.setMenuBar(mb);
mi.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
f.setVisible(true);
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
final Frame f=new Frame("设置多级菜单");
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());
MenuBar mb=new MenuBar();
Menu m1=new Menu("文件");
Menu m2=new Menu("更改名称");
MenuItem mi1=new MenuItem("退好好学习");
MenuItem mi2=new MenuItem("天天向上");
MenuItem mi3=new MenuItem("恢复标题");
MenuItem mi4=new MenuItem("退打开记事本");
MenuItem mi5=new MenuItem("退出系统");
m2.add(mi1);
m2.add(mi2);
m2.add(mi3);
m1.add(m2);
m1.add(mi4);
m1.add(mi5);
f.setMenuBar(mb);
mi5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
mi4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Runtime r=Runtime.getRuntime();
r.exec("notepad");
}
});
mi1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.setTitle(mi1.getLabel());
}
});
mi2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.setTitle("多级菜单");
}
});
mi3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
f.setTitle(mi1.getLabel());
}
});
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
f.setVisible(true);
4、Netbeans
1)界面开发常用
public class UiUtill{
private Uiutil(){}
public static void setFrameImage(JFrame jf){
Toolkit tk=ToolKit.getDefaultToolKit();
Image i=tk.getImage("src\cn\itcast\resource\a.jpg");
jf.setIconImage(i);
}
}

Toolkit tk=Toolkit.getDefaultTookit();
Dimension d=tk.getScreenSize();
double screenWidth=d.getWidth();
double screenHeight=d.getHeight();
int frameWidth=jf.getWidth();
int frameHeight=jf.getHeight();
int width=(int)(screenWidth-frameWidth)/2;
int height=(int)(screenHeight-frameHeight)/2;