Java GUI Mad God Talking Series Video Summary (8)

One, create a JFrame window

package GUI;
import java.awt.*;
import javax.swing.*;
/*进入swing的学习,swing是被封装好的,用起来会方便很多
 */
public class new2_4 {
    
    

	//这是一个方法
	public void init(){
    
    
		
		//顶级窗口
		JFrame jframe = new JFrame("这是一个JFrame窗口");
		
		//设置可见性 位置 大小 背景颜色
		jframe.setVisible(true);
		jframe.setBounds(100,100,200,200);
		jframe.setBackground(Color.cyan);
		
		//设置文字 JLable
		JLabel jlabel = new JLabel("欢迎来到Java学习");
		
		//添加到frame上
		jframe.add(jlabel);
		
		//关闭事件
		jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
	
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		new new2_4().init();
	}

}

Second, inherit a JFrame window

package GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class new2_41 {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		new frame2().init();
	}

}
class frame2 extends JFrame{
    
    
	
	//这是一个方法
	public  void init() {
    
    
		
		//设置大小 位置 可见性
		this.setBounds(200,200,500,500);
		this.setVisible(true);
		
		//得到这个
		Container container = this.getContentPane();
		
		//设置容器背景颜色
		container.setBackground(Color.BLUE);
		
		//设置文字 JLable
		JLabel label = new JLabel("欢迎来到java的学习");
		
		//让文本标签居中;
		label.setHorizontalAlignment(SwingConstants.CENTER);
		
		//添加到容器上
		container.add(label);
	}
	
}

Three, the pop-up window JDialog

package GUI;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class new2_5 extends JFrame {
    
    
	
	public new2_5(){
    
    
		
		//设置可见性 大小 位置 及关闭事件
		this.setVisible(true);
		this.setBounds(500, 500, 300, 300);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		//得到这个容器
		Container container = this.getContentPane();

		//绝对布局,之后放在container上的东西就相对container来选择位置了
		container.setLayout(null);
		
		//按钮
		JButton b1 = new JButton("点击弹出一个对话框");
		
		//设置按钮的属性
		b1.setBounds(60, 60, 150, 150);
		
		//将按钮放到容器上
		container.add(b1);
		
		//点击这个按钮的时候,会弹出一个弹窗
		b1.addActionListener(new ActionListener(){
    
    
			public void actionPerformed(ActionEvent e){
    
    
				//弹窗
				new MyDialogDemoo();
			}
		});
	}
	
	//主函数
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		new new2_5();
	}
}

//弹窗的窗口
class MyDialogDemoo extends JDialog{
    
    
	
	//构造器
	public MyDialogDemoo(){
    
    
		
		//设置弹窗的一些属性
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
		
		Container container = getContentPane();
		
		
		//新建标签, 
		JLabel label = new JLabel("老师带你学JAVA");
		container.add(label);

	}
}

Guess you like

Origin blog.csdn.net/qq_45911278/article/details/111573374