图形用户界面设计

图形用户界面设计
1.创建窗口
import java.awt.;
public class J_FrmApp
{
static Frame fra=new Frame(“FrmApp”);
public static void main(String args[])
{
fra.setSize(250,150);
fra.setLocation(100,200);
fra.setVisible(true);
System.out.println(“State:”+fra.getState());
System.out.println(“Title:”+fra.getTitle());
System.out.println(“Visible:”+fra.isVisible());
}
}
2.添加标签
import java.awt.
;
public class J_LabApp
{
public static void main(String args[])
{
Frame fra=new Frame(“LabApp”);
Label lab=new Label(); //创建1个空标签
fra.setSize(250,150);
lab.setText(“This is a label”); //为标签添加标题
lab.setAlignment(Label.CENTER);
lab.setBackground(Color.white);
lab.setForeground(Color.black); //引用Component类的方法设置标签标题颜色
Font fnt=new Font(“Serief”, Font.ITALIC+Font.BOLD,22);
lab.setFont(fnt);
fra.add(lab);
fra.setVisible(true);
}
}
3.添加按钮
import java.awt.*;
public class J_Buttapp
{
public static void main(String args[])
{
Frame fra=new Frame(“ButtApp”);
fra.setSize(250,170);
fra.setLayout(null); //关闭页面设置
Button butt=new Button(“click”);
butt.setSize(100,50);
butt.setLocation(75,60);
fra.add(butt);
fra.setVisible(true); }
}

4.添加文本行
import java.awt.;
public class J_TextApp
{
public static void main(String args[])
{
Frame fra=new Frame(“文本框程序”);
TextField txt1=new TextField(50);
TextField txt2=new TextField(“Text Field”, 50);
fra.setBounds(0,0,300,200);
fra.setLayout(null);
txt1.setBounds(50,50,130,20); //设置文本框的大小
txt2.setBounds(50,100,130,30);
fra.add(txt1); fra.add(txt2);
fra.setVisible(true); }
}
5.应用BorderLayout布局
import java.awt.
;
public class J_BorLay
{
public static void main(String args[])
{
Frame frm=new Frame(“BorderLayout”);
BorderLayout layout=new BorderLayout(5,7);
frm.setBounds(0,0,300,200);
frm.setLayout(layout);
Button butN,butS,butW,butE,butC;
butN=new Button(“north button”);
butS=new Button(“south button”);
butW=new Button(“west button”);
butE=new Button(“east button”);
butC=new Button(“center button”);
frm.add(butN, BorderLayout.NORTH);
frm.add(butS, BorderLayout.SOUTH);
frm.add(butW, BorderLayout.WEST);
frm.add(butE, BorderLayout.EAST);
frm.add(butC, BorderLayout.CENTER);
frm.setVisible(true);
}
}
6.应用FlowLayout布局
import java.awt.;
import javax.swing.
;
public class J_FlowLay
{
public static void main(String args[])
{
Frame frm=new Frame(“BorderLayout”);
FlowLayout layout=new FlowLayout();
frm.setBounds(0,0,200,200);
frm.setLayout(layout);
Button but1,but2;
TextField txt1,txt2;
but1=new Button(“button 1”);
but2=new Button(“button 2”);
txt1=new TextField(“text 1”,10);
txt2=new TextField(“text 2”,10);
frm.add(but1);
frm.add(but2);
frm.add(txt1);
frm.add(txt2);
frm.setVisible(true);
}
}
7.应用GridLayout布局
import java.awt.;
public class J_GridLay
{
public static void main(String args[])
{
Frame frm=new Frame(“GridLayout”);
GridLayout layout=new GridLayout(2,2); //定义2行2列的GridLayout
frm.setBounds(0,0,200,200);
frm.setLayout(layout);
String names[]={ “butt1”,“butt2”,“butt3”,“butt4”};
for(int i=0; i<names.length; i++)
{
frm.add( new Button(names[i]));
}
frm.setVisible(true);
}
}
8.处理按钮单击事件
import java.awt.
;
import java.awt.event.;
public class J_ButtEventApp extends Frame implements ActionListener
{
static J_ButtEventApp frm=new J_ButtEventApp();
public static void main(String args[])
{
frm.setTitle(“ButtEventApp”);
frm.setSize(300,160);
frm.setLayout(null);
Button btn;
btn=new Button(“push”);
btn.setBounds(120,80,60,30);
btn.addActionListener(frm);
frm.add(btn,BorderLayout.CENTER);
frm.setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
frm.setBackground(Color.red);
}
}
9.利用内部类处理按钮单击事件
import java.awt.
;
import java.awt.event.;
public class J_InnClassEvent
{
static Frame frm=new Frame();
public static void main(String args[])
{
frm.setTitle(“ButtEventApp”);
frm.setSize(300,160);
frm.setLayout(null);
Button btn;
btn=new Button(“push”);
btn.setBounds(120,80,60,30);
btn.addActionListener(new J_InnCla());
frm.add(btn,BorderLayout.CENTER);
frm.setVisible(true);
}
static class J_InnCla implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
frm.setBackground(Color.red);
}
}
}
10.处理ActionEvent事件
import java.awt.
;
import java.awt.event.;
public class J_ActEvent extends Frame implements ActionListener
{
static J_ActEvent frm=new J_ActEvent();
static Button btn1,btn2;
static Label lbl;
public static void main(String args[])
{
frm.setTitle(“ActionEvent”);
frm.setSize(240,160);
frm.setLayout(new FlowLayout());
btn1=new Button(" button 1 “);
btn2=new Button(” button 2 “);
lbl=new Label(” no clicking ");
btn1.addActionListener(frm);
btn2.addActionListener(frm);
frm.add(btn1);
frm.add(btn2);
frm.add(lbl);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn1)
lbl.setText(“button 1 clicked”);
else
lbl.setText(“button 2 clicked”);
}
}
11.处理ItemEvent事件
import java.awt.
;
import java.awt.event.;
public class J_IteEvent extends Frame implements ItemListener
{
static J_IteEvent frm=new J_IteEvent();
static Checkbox chb1,chb2;
static TextField txt1;
public static void main(String args[])
{
frm.setTitle(“ItemEvent”);
frm.setSize(240,160);
frm.setLayout(new FlowLayout());
CheckboxGroup grp=new CheckboxGroup();
chb1=new Checkbox(“green”);
chb2=new Checkbox(“yellow”);
txt1=new TextField(" None is selected ");
chb1.setCheckboxGroup(grp);
chb2.setCheckboxGroup(grp);
chb1.addItemListener(frm);
chb2.addItemListener(frm);
frm.add(chb1); frm.add(chb2);
frm.add(txt1); frm.setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==chb1)
txt1.setText(“green is selected”);
else
if (e.getSource()==chb2)
txt1.setText(“yellow is selected”);
}
}
12.处理TextEvent事件
import java.awt.
;
import java.awt.event.;
public class J_TexEvent extends Frame implements TextListener
{
static J_TexEvent frm=new J_TexEvent();
static TextField txt1,txt2;
public static void main(String args[])
{ frm.setTitle(“TextEvent”);
frm.setSize(240,160);
frm.setLayout(new FlowLayout());
txt1=new TextField(20);
txt2=new TextField(20);
txt1.addTextListener(frm);
frm.add(txt1);
txt2.setEditable(false);
frm.add(txt1);
frm.add(txt2);
frm.setVisible(true);
}
public void textValueChanged(TextEvent e)
{
txt2.setText(txt1.getText());
}
}
13.处理KeyEvent事件
import java.awt.
;
import java.awt.event.;
public class J_KeysEvent extends Frame implements KeyListener
{ static J_KeysEvent frm=new J_KeysEvent();
static TextField txf;
static TextArea txa;
public static void main(String args[])
{
frm.setTitle(“KeyEvent”); frm.setSize(240,200);
frm.setLayout(new FlowLayout());
txf=new TextField(20); txa=new TextArea(6,20);
txa.setEditable(false);
txf.addKeyListener(frm);
frm.add(txf); frm.add(txa);
frm.setVisible(true);
}
public void keyPressed(KeyEvent e)
{ txa.setText("");
txa.append(e.getKeyChar()+" is pressed!\n");
}
public void keyReleased(KeyEvent e)
{ txa.append(e.getKeyChar()+" is released!\n"); }
public void keyTyped(KeyEvent e)
{ txa.append(e.getKeyChar()+" is typed!\n"); }
}
14.利用KeyAdapter类处理TextEvent事件
import java.awt.
;
import java.awt.event.;
public class J_KeysEvent2 extends Frame
{
static J_KeysEvent2 frm=new J_KeysEvent2();
static TextField txt1,txt2;
public static void main(String args[])
{
frm.setTitle(“KeyEvent”);
frm.setSize(240,130);
frm.setLayout(new FlowLayout());
txt1=new TextField(20); txt2=new TextField(20);
txt2.setEditable(false);
txt1.addKeyListener(new J_KeysAdapter());
frm.add(txt1);
frm.add(txt2);
frm.setVisible(true);
}
static class J_KeysAdapter extends KeyAdapter
{
public void keyTyped(KeyEvent e)
{
txt2.setText(e.getKeyChar()+" is entered!" );
}
}
}
15.MouseListener接口处理MouseEvent事件
import java.awt.
;
import java.awt.event.;
public class J_MouEvent extends Frame implements MouseListener
{
static J_MouEvent frm=new J_MouEvent();
static TextArea txa1,txa2;
public static void main(String args[])
{
frm.setTitle(“MouEvent”); frm.setSize(240,300);
frm.setLayout(new FlowLayout());
txa1=new TextArea(5,30);
txa2=new TextArea(8,30);
txa2.setEditable(false);
txa1.addMouseListener(frm);
frm.add(txa1); frm.add(txa2);
frm.setVisible(true);
}
public void mouseEntered(MouseEvent e) //鼠标进入事件处理
{
txa2.setText(“Mouse enters txa1\n”);
}
public void mouseClicked(MouseEvent e) //鼠标单击事件处理
{
txa2.append(“Mouse is
clisked at[”+e.getX()+","+e.getY()+"]\n");
}
public void mousePressed(MouseEvent e) //鼠标按键事件处理
{
txa2.append(“Mouse is pressed at [”+e.getX()+","+e.getY()+"]\n");
}
public void mouseReleased(MouseEvent e) //鼠标键释放事件处理
{
txa2.append(“Mouse is released at [”+e.getX()+","+e.getY()+"]\n");
}
public void mouseExited(MouseEvent e) //鼠标离开事件处理
{
txa2.append("Mouse exits from txa1 ");
}
}
16.MouseMontionListener处理MouseEvent事件
import java.awt.
;
import java.awt.event.;
public class J_MouEvent2 extends Frame implements MouseMotionListener
{
static J_MouEvent2 frm=new J_MouEvent2();
static TextArea txa;
static TextField txt1,txt2;
public static void main(String args[])
{
frm.setTitle(“MouEvent”);
frm.setSize(240,200);
frm.setLayout(new FlowLayout());
txa=new TextArea(5,30);
txt1=new TextField(30);
txt2=new TextField(30);
txa.setEditable(false);
txa.addMouseMotionListener(frm);
frm.add(txa);
frm.add(txt1);
frm.add(txt2);
frm.setVisible(true);
}
public void mouseMoved(MouseEvent e) //处理鼠标移动事件
{
txt1.setText(“Mouse is moved in txa”);
}
public void mouseDragged(MouseEvent e) //处理鼠标拖动事件
{
txt2.setText(“Mouse is dragged in txa”);
}
}
17.处理WindowEvent事件
import java.awt.
;
import java.awt.event.*;
public class J_WinEvent extends Frame implements WindowListener
{
static J_WinEvent frm=new J_WinEvent();
public static void main(String args[])
{ frm.setTitle(“MouEvent”);
frm.setSize(240,200);
frm.addWindowListener(frm);
frm.setVisible(true);
}
public void windowClosing(WindowEvent e)
{ System.out.println(“windowClosing() method”);
System.exit(0);
}
public void windowClosed(WindowEvent e)
{ }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowIconified(WindowEvent e)
{ System.out.println(“windowIconified() method”);
}
public void windowDeiconified(WindowEvent e)
{ System.out.println(“windowDeiconified() method”);
}
public void windowOpened(WindowEvent e)
{ System.out.println(“windowOpened() method”);
}
}

猜你喜欢

转载自blog.csdn.net/qq_42304657/article/details/89228546