Java之JFrame的使用

Posted 熬过无人问津的日子才会有远方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java之JFrame的使用相关的知识,希望对你有一定的参考价值。

1.Frame1.java

package welcome;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: </p>
 * @author none
 * @version 1.0
 */

public class Frame1 extends JFrame {
	JPanel contentPane;
	Label label1 = new Label();
	XYLayout xYLayout1 = new XYLayout();
	//Construct the frame
	public Frame1(){
    	enableEvents(AWTEvent.WINDOW_EVENT_MASK);
	    try {
	    	jbInit();
	    } catch(Exception e) {
	    	e.printStackTrace();
	    }
  	}
  	//Component initialization
	private void jbInit() throws Exception{
		//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
		contentPane = (JPanel) this.getContentPane();
		label1.setFont(new java.awt.Font("Dialog", 0, 20));
		label1.setText("Welcome to JBuilder");
		contentPane.setLayout(xYLayout1);
		this.setSize(new Dimension(400, 300));
		this.setTitle("Frame Title");
		contentPane.add(label1, new XYConstraints(102, 68, 400, -1));
	}
	//Overridden so we can exit when window is closed
	protected void processWindowEvent(WindowEvent e){
		super.processWindowEvent(e);
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			System.exit(0);
		}
	}
}

2.welcome.java

package welcome;

import javax.swing.UIManager;
import java.awt.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: </p>
 * @author node
 * @version 1.0
 */

public class welcome {
	boolean packFrame = false;

	//Construct the application
	public welcome(){
		Frame1 frame = new Frame1();
		//Validate frames that have preset sizes
		//Pack frames that have useful preferred size info, e.g. from their layout
		if (packFrame) {
		  	frame.pack();
		} else {
		  	frame.validate();
		}
		//Center the window
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = frame.getSize();
		if (frameSize.height > screenSize.height) {
		  	frameSize.height = screenSize.height;
		}
		if (frameSize.width > screenSize.width) {
		  	frameSize.width = screenSize.width;
		}
		frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
		frame.setVisible(true);
	}
	//Main method
	public static void main(String[] args){
		try {
		  	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
		  	e.printStackTrace();
		}
		new welcome();
	}
}

下载依赖外部包

链接:https://pan.baidu.com/s/1xbH-lU7YAZeTlUqxxo3URA
提取码:6j7v

右击项目名称选择build path下的Configure Build Path

显示如下图,选择Libraries,Add External JAS

以上为eclipse操作。

以上是关于Java之JFrame的使用的主要内容,如果未能解决你的问题,请参考以下文章

双击图标时关闭JFrame

java如何使用ico图标作为JFrame的图标?

单击JFrame上的退出按钮需要关闭JFrame

Java JFrame - 添加标签图像

java jframe怎么内嵌浏览器

Java Gui 组件不会出现在 JFrame 中