Swing代码总结1

Frame窗体

注意点就是JFrame的需要用Container的转化一下为容器。下面代码用到了JLabel(标签组件)。

import java.awt.*;
import javax.swing.*;
public class Example extends JFrame{
	public void CreateJFrame(String title){
	JFrame jf = new JFrame(title);
	Container container =jf.getContentPane();
	JLabel jl = new JLabel("Frame窗口");//标签组件
	jl.setHorizontalAlignment(SwingConstants.CENTER);
	container.add(jl);
	container.setBackground(Color.white);//背景颜色
	jf.setVisible(true);//窗体可视
	jf.setSize(200,150);//窗体尺寸
	jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
    public static void main(String args[]){
	new Example().CreateJFrame("创建一个JFrame窗口");
	}
}

运行后就这样,这个挫东西让我高兴半天。
在这里插入图片描述
关闭方式(手写笔记超过一次,这里偷懒截图算辽)在这里插入图片描述

JDialog窗体

JDialog窗体就是对话框。也需要用getContentPane转换为容器。

class MyJDialog extends JDialog{
	public void MyDialog(MyFrame frame){
	super(frame,"小弹窗",true);
	Container container =getContentPane();
	container.add(new JLabel("这是一个对话框"));
	setBounds(120,120,100,100);
	}
}
public class MyFrame extends JFrame{
	public static void main(String args[])
	{
		new MyFrame();//实例化MyJDialog类对象
	}
	public MyFrame() {
		Container container = getContentPane();//创建容器
		container.setLayout(null);
		JLabel man = new JLabel("Java Study");//窗体标签
		man.setHorizontalAlignment(SwingConstants.CENTER);
		container.add(man);
		JButton bl= new JButton("点我");
		bl.setBounds(20,20,100,21);
		bl.addActionListener(new ActionListener() {//为按钮添加事件
			public void actionPerformed(ACtionEvent e) {
				new MyJDialog(MyFrame.this).setVisible(true);
			}
		});
		container.add(bl);
		//我也不知道少了啥明天再改改
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值