哪家做网站公司最好,南京江宁网站建设,wordpress调用自定义文章类型文章,建筑企业培训课程JAVA自学笔记25 1、GUI 1#xff09;图形用户接口#xff0c;以图形的方式#xff0c;来显示计算机操作的界面#xff0c;更方便更直观 2#xff09;CLI 命令行用户接口#xff0c;就是常见的Dos#xff0c;操作不直观 3#xff09; 类Dimension 类内封装单个对象… JAVA自学笔记25 1、GUI 1图形用户接口以图形的方式来显示计算机操作的界面更方便更直观 2CLI 命令行用户接口就是常见的Dos操作不直观 3 类Dimension 类内封装单个对象组件中组件的宽度和高度精确到整数 Dimension(int width,int height); 类Point //窗体案例
Frame fnew Frame();//也可通过带参构造设置标题String title,//设置窗体标题
f.setTitle(dd);
//设置窗体大小
//f.setSize(100,200);
Dimension dnew Dimension(100,200);
//设置窗体位置//f.setLocation(400,200);
Point pnew Point(400,200);
f.location(p);//f.setBounds(int x.int y,int width,int height);//同时设置坐标及长宽Thread.sleep(3000);//休眠3秒再弹出
f.setVisible(true);2、事件监听器 1把事件源和事件关联起来 2 //窗体关闭案例
Frame fnew Frame();
f.setTitle(dd);
f.setBounds(100200300,400);/
Thread.sleep(3000);
//事件监听
f.addWindowListener(new WindowListener()
{
public void WindowClosing(WindowEvent e){
System.exit(0);
}
});
f.setVisible(true);3)适配器设计模式 接口须重写方法较多且不需都实现时可提供一个适配器类仅仅空实现即可在实现类重写即可 接口–适配器–实现类 WindowAdapter 适配器类 //窗体关闭案例
Frame fnew Frame();
f.setTitle(dd);
f.setBounds(100200300,400);
Thread.sleep(3000);f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
f.setVisible(true);4布局 //窗体添加按钮并对按钮添加事件
//使用流式布局
Frame fnew Frame();
f.setTitle(dd);
f.setBounds(100200300,400);
Thread.sleep(3000);
//设置布局为流式布局
f.setLayout(new FlowLayout());//创建按钮对象
Button bunew 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 fnew Frame(数据转移);
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());//创建文本框
final TextField tfnew TextField(20);//文本框内可放20个字符
//创建按钮
Button bunew Button(数据转移);
//创建文本域
final TextAreas tanew TextArea(10,40);
//
//对按钮添加事件
bu.addActionListener(new ActionListener)(){
ppublic void actionPerformed(ActionEvent e){
//获取文本框的值
String tf_strtf.getText().trim();//设置给文本域并换行
ta.append(tf_stf\r\n);
}
//获取光标
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 fnew Frame(更改背景色);
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());//创建四个按钮
Button redButtonnew Button(red);f.add(redButton);//设置窗体关闭
f.addWindowListener(new WindowAdapte(){
public void windowClosing(WindowEvent e);{
System.exit(0);
}
});
//对按钮添加动作事件
/*redButton.addActionLostener(new ActionLister(){
public void actionPerformed(ActionEvent e){
f.setBackground(Color.RED);
}
});*///对按钮添加鼠标点击事件
/*redButton.addMouseListener(new MouseAdapter(){
public mouseClicked(MouseEvent e){
f.setBackground(color.RED);
}
});*///对按钮添加鼠标的进入事件
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 fnew Frame(只能输入数字字符);
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());//创建Label标签对象
Label labelnew Label(请输入QQ号码仅为数字);
TextFiled tfnew TextField(40);//为文本框添加事件
tf.addKeyListener((new) KeyAdapter({
public void KeyPressed(KeyEvent e){
char che.getKeyChar();
if(!(ch0ch9)){}
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 fnew Frame(只能输入数字字符);
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());//创建菜单栏
MenuBar mbnew MenuBar();//创建菜单
Menu mnew Menu(文件);
//创建菜单项
MenuItem minew 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 fnew Frame(设置多级菜单);
//设置窗体属性和布局
f.setBounds(100,500,633,100);
f.setLayout(new FlowLayout());//创建菜单栏
MenuBar mbnew MenuBar();//创建菜单
Menu m1new Menu(文件);
Menu m2new Menu(更改名称);
//创建菜单项
MenuItem mi1new MenuItem(退好好学习);
MenuItem mi2new MenuItem(天天向上);
MenuItem mi3new MenuItem(恢复标题);
MenuItem mi4new MenuItem(退打开记事本);
MenuItem mi5new 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 rRuntime.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 tkToolKit.getDefaultToolKit();
//根据路径获取图片
Image itk.getImage(src\\cn\\itcast\\resource\\a.jpg);
//给窗体设置图片
jf.setIconImage(i);}
} //设置窗体居中
//获取工具对象
Toolkit tkToolkit.getDefaultTookit();//获取屏幕宽高
Dimension dtk.getScreenSize();
double screenWidthd.getWidth();
double screenHeightd.getHeight();
//获取窗体宽高
int frameWidthjf.getWidth();
int frameHeightjf.getHeight();//获取新的宽高
int width(int)(screenWidth-frameWidth)/2;
int height(int)(screenHeight-frameHeight)/2; 转载于:https://www.cnblogs.com/Tanqurey/p/10485337.html