java学习第二十六次笔记—— ——图形界面

系列文章目录

前言

风吹年年,年年有风,慢慢及漫漫

一、窗口

1

package Demo01;

import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class jFreameTest extends JFrame{
    
    
	
	public jFreameTest(){
    
    
		JFrame f = new JFrame("窗体标题");//创建窗体对象,可在参数中设置窗体标题
		//设置窗体可见
				setVisible(true);
			//注意:点击关闭没有结束程序的运行
			
			/*
			 * EXIT_ON_CLOSE:隐藏窗体,并停止程序
			 * DO_NOTHING_ON_CLOSE:无任何操作
			 * HIDE_ON_CLOSE:隐藏窗体但不停止程序
			 * DISPOSE_ON_CLOSE:释放窗体资源	
			 * 
			 */
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
				setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				
				//setLocation(200,400);//设置位置,单位是像素,从左上角0.0开始
				//setSize(420,300);//设置窗体大小,单位像素
				setBounds(200,600,400,300);//位置,大小
				
				Container c = getContentPane();
				c.setBackground(Color.pink);
				
				JLabel l = new JLabel("自动登录");
				
				c.add(l);//添加组件
				//c.remove(l);//移除组件
				
				c.validate();
				
				//f.setContentPane(c);
				
				setResizable(false);
				
				System.out.println("x="+getX()+"Y="+getY());
		
		
	}
	
	public static void main(String[] args) {
    
    
		
		new jFreameTest();
	
	}

}

2

package Demo01;

import java.awt.Container;

import javax.swing.JDialog;
import javax.swing.JLabel;

public class jDialogTest extends JDialog{
    
    
	
	
	
	public static void main(String[] args) {
    
    
		JDialog jd=new JDialog();
		jd.setVisible(true);
		jd.setBounds(200,200,400,600);
		
		
		Container c =jd.getContentPane();//获取窗体容器
		c.add(new JLabel("这是一个标签"));
	}

}

おすすめ

転載: blog.csdn.net/weixin_54405545/article/details/117518852