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+
"\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 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;
轉載于:https://www.cnblogs.com/Tanqurey/p/10485337.html
總結
以上是生活随笔為你收集整理的JAVA自学笔记25的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。