JFrame的API

这是JFrame的API链接地址:
你可以根据需要进入查看,里边肯定比我教的全面链接地址

JFrame的创建和属性设置

下面是部分代码,并没有导入文件部分,原文复制运行可能会出错,仅做简单示例

public class ssc 
{
    private JFrame frame;//创建frame
	public static void main(String[] args) {	
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ssc window = new ssc();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	public ssc() {
		initialize();
	}
	
	private void initialize() {
		//页面框架的设置
		frame = new JFrame();
		frame.setType(Type.POPUP);//窗口弹出样式
		frame.setTitle("学生成绩管理系统");//窗口标题设置
		frame.setLocationRelativeTo(null);//设置为居中
		frame.getContentPane().setBackground(Color.CYAN);
		frame.setBackground(Color.CYAN);//背景色
		frame.setBounds(100, 100, 989, 600);//窗口大小
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置为点击×号后只推出当前页
        frame.setVisible(true);//设置窗口可见
        //其他详细的功能需要的话自己可以查找API,此处不详尽展示
 }

JFrame组件添加

以JLable,JTextField为例,添加进frame
(接上一部分代码)

private void initialize() {
        JTextField textField = new JTextField();//定义文本框
		textField.setBounds(280, 13, 439, 53);//设置文本框大小
		textField.setFont(new Font("楷体", Font.PLAIN, 40));
		textField.setEditable(false);//设置文本框为不可编辑
		textField.setHorizontalAlignment(SwingConstants.CENTER);//设置为居中
		textField.setBackground(Color.YELLOW);//设置背景色为黄色
		textField.setText("\u5B66\u751F\u6210\u7EE9\u7BA1\u7406\u7CFB\u7EDF");//设置文本框内容
		frame.getContentPane().add(textField);//把textField添加进frame

//标签添加,和上述方法相同,不再一一注释
        JLable label= new Label("\u5BC6\u7801\uFF1A");//
		label.setFont(new Font("华文行楷", Font.PLAIN, 22));//
		label.setBounds(328, 191, 95, 32);//
		frame.getContentPane().add(label);//
}	

完整页面代码如下(仅做参考)

代码可完整运行,但是需要提前导入swing的包

package guanli;

import java.awt.EventQueue;
import javax.swing.*;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.LineBorder;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableModel;
import java.awt.Canvas;
import java.awt.Choice;
import java.awt.Button;
import java.awt.Font;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.FrameBorderStyle;
import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import com.jgoodies.forms.layout.FormSpecs;
import java.awt.SystemColor;
import java.awt.Component;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.Label;
import java.awt.Window.Type;

public class ssc 
{
	private JButton button;
    private JFrame frame;
	private JTextField textField;
	private JTextField textField_2;
	private JPasswordField passwordField;
	public static void main(String[] args) {	
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ssc window = new ssc();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
 }
	
	public ssc() {
		initialize();
	}
	
	private void initialize() {
		//页面框架
		frame = new JFrame();
		frame.setType(Type.POPUP);
		frame.setTitle("学生成绩管理系统");
		frame.setLocationRelativeTo(null);
		frame.getContentPane().setBackground(Color.CYAN);
		frame.setBackground(Color.CYAN);
		frame.setBounds(100, 100, 989, 600);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		//退出按钮
	    button = new JButton("\u9000\u51FA");
		button.setFont(new Font("楷体", Font.PLAIN, 20));
		button.setBounds(600, 303, 119, 49);
		button.setForeground(Color.WHITE);
		frame.getContentPane().add(button);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null,"即将退出系统");
				System.out.println("退出系统");
				System.exit(0);
		}});//为退出按钮添加监听事件
		
		//标题,学生成绩管理系统
		textField = new JTextField();
		textField.setBounds(280, 13, 439, 53);
		textField.setFont(new Font("楷体", Font.PLAIN, 40));
		textField.setEditable(false);
		textField.setHorizontalAlignment(SwingConstants.CENTER);
		textField.setBackground(Color.YELLOW);
		textField.setText("\u5B66\u751F\u6210\u7EE9\u7BA1\u7406\u7CFB\u7EDF");
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		//账号输入
		textField_2 = new JTextField();
		textField_2.setFont(new Font("楷体", Font.PLAIN, 22));
		textField_2.setHorizontalAlignment(SwingConstants.CENTER);
		textField_2.setBounds(447, 123, 234, 32);
		frame.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		
		//密码输入
		passwordField = new JPasswordField();
		passwordField.setFont(new Font("宋体", Font.PLAIN, 28));
		passwordField.setBackground(Color.WHITE);
		passwordField.setHorizontalAlignment(SwingConstants.CENTER);
		passwordField.setBounds(447, 191, 234, 32);
		frame.getContentPane().add(passwordField);
		
		//账号标签
		Label label = new Label("\u8D26\u53F7\uFF1A");
		label.setForeground(SystemColor.desktop);
		label.setFont(new Font("华文行楷", Font.PLAIN, 22));
		label.setBounds(328, 123, 95, 32);
		frame.getContentPane().add(label);
		
		//密码标签
		Label label_1 = new Label("\u5BC6\u7801\uFF1A");
		label_1.setFont(new Font("华文行楷", Font.PLAIN, 22));
		label_1.setBounds(328, 191, 95, 32);
		frame.getContentPane().add(label_1);

		frame.setVisible(true);
	}
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐