纠正一个错误 : JLable 是可以直接放在 JFrame 上的. 可以运行如下的例子看效果,例子代码来自 sun.import java.awt.*; import java.awt.event.*; import javax.swing.*;/* FrameDemo.java requires no other files. */ public class FrameDemo { /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */ private static void createAndShowGUI() { //Suggest that the L&F (rather than the system) //decorate all windows. This must be invoked before //creating the JFrame. Native look and feels will //ignore this hint. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("FrameDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel emptyLabel = new JLabel(""); emptyLabel.setPreferredSize(new Dimension(175, 100)); frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); //Display the window. frame.pack(); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
比如从下面的程序可以看出来
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;public class mainFrame extends JFrame
{
public mainFrame()
{
init();
}
void init()
{
Container contain=this.getContentPane();
JPanel pan=new JPanel(new FlowLayout());
contain.setLayout(new BorderLayout());
contain.add(txt,BorderLayout.NORTH);
contain.add(txtarea,BorderLayout.CENTER);
pan.add(btn1,FlowLayout.LEFT);
pan.add(btn2,FlowLayout.CENTER);
pan.add(btn3,FlowLayout.RIGHT);
contain.add(pan,BorderLayout.SOUTH);
this.setSize(400,400);
this.setVisible(true);
}
JTextField txt=new JTextField("017100512");
JTextArea txtarea=new JTextArea(20,40);
JButton btn1=new JButton("Search");
JButton btn2=new JButton("Update");
JButton btn3=new JButton("Modify");}
可以运行如下的例子看效果,例子代码来自 sun.import java.awt.*;
import java.awt.event.*;
import javax.swing.*;/* FrameDemo.java requires no other files. */
public class FrameDemo {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Suggest that the L&F (rather than the system)
//decorate all windows. This must be invoked before
//creating the JFrame. Native look and feels will
//ignore this hint.
JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window.
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel emptyLabel = new JLabel("");
emptyLabel.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); //Display the window.
frame.pack();
frame.setVisible(true);
} public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
简而言之:
相同:frame,panel既是容器,又是组件。
不同:是panel不能独立存在。就是不能作为顶级存在。
子窗口与父窗口的关系
JFrame和JPanel是AWT的后继版本SWING中相应的组件
JPanel不可单独存在!