1.窗口
public class demo3 {
public static void main(String[] args) {
Frame frame = new Frame("昨晚梦到了那个她");
Button CENTER = new Button("CENTER");
Button EAST = new Button("EAST");
Button WEST = new Button("WEST");
Button NORTH = new Button("NORTH");
Button SOUTH = new Button("SOUTH");
frame.add(CENTER,BorderLayout.CENTER);
frame.add(EAST,BorderLayout.EAST);
frame.add(WEST,BorderLayout.WEST);
frame.add(NORTH,BorderLayout.NORTH);
frame.add(SOUTH,BorderLayout.SOUTH);
frame.setSize(400,400);
frame.setLocation(200,200);
frame.setVisible(true);
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
2.弹窗,面板
public class demo1 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setLocation(100,100);
frame.setSize(400,400);
//frame.setLayout(new GridLayout(2,2));//设置布局 网格布局
Panel panel = new Panel(new GridLayout(2,2));
panel.add(new Button("shiyan"),BorderLayout.CENTER);
panel.add(new Button("shiyan"),BorderLayout.WEST);
panel.add(new Button("shiyan"),BorderLayout.EAST);
panel.add(new Button("shiyan"),BorderLayout.NORTH);
frame.add(panel);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//0正常结束程序 1异常结束程序
}
});
}
}
3.按钮
public class demo3 {
public static void main(String[] args) {
Frame frame = new Frame("昨晚梦到了那个她");
Button CENTER = new Button("CENTER");
Button EAST = new Button("EAST");
Button WEST = new Button("WEST");
Button NORTH = new Button("NORTH");
Button SOUTH = new Button("SOUTH");
frame.add(CENTER,BorderLayout.CENTER);
frame.add(EAST,BorderLayout.EAST);
frame.add(WEST,BorderLayout.WEST);
frame.add(NORTH,BorderLayout.NORTH);
frame.add(SOUTH,BorderLayout.SOUTH);
frame.setSize(400,400);
frame.setLocation(200,200);
frame.setVisible(true);
frame.setResizable(false);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
4.监听器
按一次按钮运行一次,实现监听
public class demo {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
MyActionListener actionListener = new MyActionListener();
button.addActionListener(actionListener);
frame.setBounds(400,400,400,400);
frame.add(button);
frame.pack();
frame.setVisible(true);
windowclose(frame);
}
public static void windowclose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("aaa");
}
}
5.简易计算器(加法)
public class demo3 {
public static void main(String[] args) {
new calcular().loadframe();
}
}
class calcular extends Frame{
TextField num1,num2,num3;
public void loadframe(){
num1=new TextField(10);
num2=new TextField(10);
num3=new TextField(10);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new mycalculatorlisener());
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
private class mycalculatorlisener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(num1.getText());
int i1 = Integer.parseInt(num2.getText());
num3.setText(""+(i+i1));
num1.setText("");
num2.setText("");
}
}
}
6.键盘事件
public class demo {
public static void main(String[] args) {
new key();
}
}
class key extends Frame{
public key(){
setBounds(1,2,300,400);
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println(keyCode);
if(keyCode==KeyEvent.VK_UP){
System.out.println("你按了上键");
}
}
});
}
}
7.画图
public class demo2 {
public static void main(String[] args) {
new huahua("画图");
}
}
class huahua extends Frame{
ArrayList points;
public huahua(String title){
super(title);
setBounds(200,200,300,400);
points=new ArrayList();
setVisible(true);
this.addMouseListener(new MyMouselistener());
}
public void paint(Graphics g){//绘图
Iterator iterator = points.iterator();//迭代器
while(iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x,point.y,10,10);
}
}
/*@Override
public void paint(Graphics g) {
//画画,监听鼠标的事件
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(Color.BLUE);
g.fillOval(point.x,point.y,10,10);
}
}*/
public void addPaint(Point point){
points.add(point);
}
private class MyMouselistener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
huahua huahua = (huahua) e.getSource();
huahua.addPaint(new Point(e.getX(),e.getY()));
huahua.repaint();
}
}
}