Java之JFrame的使用

 JAVA   大苹果   2017-06-03 19:52   36503

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();
	}
}

下载依赖外部包jbcl.zip

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

blob.png

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

blob.png

以上为eclipse操作。