Java GUI Mad God Talking Series Video Summary (13)

One, drop-down box

//这是我自己的包
package GUI;
//导入必要的包  
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
public class TestDemo extends JFrame {
    
    
	
	public TestDemo(){
    
    
		//得到这个容器
		 Container container = this.getContentPane();
		 
		 //下拉框
		JComboBox status = new JComboBox();
		
		//给下拉框添加内容
		status.addItem(null);
		status.addItem("正在热映");
		status.addItem("已下架");
		status.addItem("即将上映");
		
		//将下拉框添加至容器上
		container.add(status);
			
		//设置大小 位置 可见性 及关闭事件
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
 
public static void main(String[] args) {
    
    
		new TestDemo();
	}
}

Second, the list box

//这是我自己的包
package GUI;
//导入必要的包  
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Vector;

import javax.swing.*;
public class TestDemo extends JFrame {
    
    
	
	public TestDemo(){
    
    
		//得到这个容器
		 Container container = this.getContentPane();
		 
		//列表框 生成列表内容  稀疏数组:压缩数据
		//使用数组类型也可以:String[] contents ={"1","2","3"};
		Vector contents = new Vector();
			
		//相当于动态的
		contents.add("zhangsan");
		contents.add("zhangsan1");
		contents.add("zhangsan2");
			
		//列表中需放入内容
		JList jList = new JList(contents);
			
		container.add(jList);
			
		//设置大小 位置 可见性 及关闭事件
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
 
public static void main(String[] args) {
    
    
		new TestDemo();
	}
}

Three, the text box

//这是我自己的包
package GUI;
//导入必要的包  
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Vector;

import javax.swing.*;
public class TestDemo extends JFrame {
    
    
	
	public TestDemo(){
    
    
		//得到这个容器
		Container container = this.getContentPane();
		 
		//两个文本框
		JTextField textField = new JTextField("hello");
		JTextField textField2 = new JTextField("world",20);//设置初始文本和大小
			
		//放在指定的地方
		container.add(textField2,BorderLayout.NORTH);
		container.add(textField,BorderLayout.SOUTH);
			
		//设置大小 位置 可见性 及关闭事件
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
 
public static void main(String[] args) {
    
    
		new TestDemo();
	}
}

Fourth, the password box

//这是我自己的包
package GUI;
//导入必要的包  
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Vector;

import javax.swing.*;
public class TestDemo extends JFrame {
    
    
	
	public TestDemo(){
    
    
		//得到这个容器
		Container container = this.getContentPane();
		 
		//密码框
		JPasswordField passwordField = new JPasswordField();
		
		//设置为 *
		passwordField.setEchoChar('*');
		container.add(passwordField);
			
		//设置大小 位置 可见性 及关闭事件
		this.setVisible(true);
		this.setBounds(100, 100, 500, 500);
		this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
 
public static void main(String[] args) {
    
    
		new TestDemo();
	}
}

Guess you like

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