第十四节:窗体实验

图片

实验目的

1 框架类,按扭类,文本类,输入框类的基本方法使用

要求

1.在屏幕上显示如下界面,要求窗口显示在屏幕的正中间
2.窗口的尺寸如图所示

图片

步骤:

定义类继承JFrame
调用JFrame中的相关方法显示窗体
根据窗体中的组件为窗口添加成员变量并实例化每个组件。

定义窗口的布局方式
将组件按顺序添加到窗体中。
主方法中实例化窗体并显示

代码

package c;

import javax.swing.*;

public class JFrameDemo extends JFrame{

JFrameDemo(){
super("關於窗口...");
}

JFrameDemo(String title){
super(title);
}

public void init(){
this.setSize(250,250);
this.setLocation(100,100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new JFrameDemo().init();
}
}
package c;

import java.awt.*;

import javax.swing.*;

public class FlowLayoutDemo extends JFrameDemo {
JLabel jb11,jb12,jb13;
JButton jb1;
JButton jb2;
JTextField jb111;

FlowLayoutDemo(){
super("關於窗口...");
jb11=new JLabel("Happy聊天室Copyright2007-2010");
jb12=new JLabel(new ImageIcon("logo.gif"));
jb1=new JButton("系统信息");
jb13=new JLabel("请输入你的名字");
jb111=new JTextField(15);
jb2=new JButton(("退出"),new ImageIcon("exit.gif"));  
}

    public void init(){
    super.init();
    this.setLayout(new FlowLayout(FlowLayout.CENTER));
    this.add(jb11);
    this.add(jb12);
    this.add(jb1);
    this.add(jb13);
    this.add(jb111);
    this.add(jb2);
    }
    
public static void main(String[] args) {
FlowLayoutDemo demo=new FlowLayoutDemo();
demo.init();
}
}

运行结果图

图片

小礼物走一走 or 点赞

图片

猜你喜欢

转载自www.cnblogs.com/dashucoding/p/9387920.html