Java之JFrame的使用
1.Frame1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package welcome;
import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.borland.jbcl.layout.*;
public class Frame1 extends JFrame { JPanel contentPane; Label label1 = new Label(); XYLayout xYLayout1 = new XYLayout(); public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { 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)); } protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } }
|
2.welcome.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package welcome;
import javax.swing.UIManager; import java.awt.*;
public class welcome { boolean packFrame = false;
public welcome() { Frame1 frame = new Frame1(); if (packFrame) { frame.pack(); } else { frame.validate(); } 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); } 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
显示如下图,选择Libraries,Add External JAS
以上为eclipse操作。