swing是轻型组件(lightweight component),AWT是重型组件(heavyweight component).
Frame类中的 setLocationRelativeTo()的用法
JFrame类中的 setLocationRelativeTo()是从 java.awt.Window类继承的方法,其原型为: public void setLocationRelativeTo(Component c); 用法为: 设置此窗口相对于指定组件的位置。如果此组件当前未显示,或者 c 为 null ,则此窗口位于屏幕的中央。如果该组件的底部在视线以外,则将该窗口放置在 Component 最接近窗口中心的一侧。因此,如果 Component 在屏幕的右部,则 Window 将被放置在左部,反之亦然。参数: c - 确定窗口位置涉及的组件
package chapter12;
import java.awt.*;
import javax.swing.*;
public class ShowFlowLayout extends JFrame{ public ShowFlowLayout(){ //Set FlowLayout, aligned left with horizontal水平的 gap 10 and //vertical垂直的 gap 20 between components组件。。向右对齐,添加顺序还是左起的 setLayout (new FlowLayout(FlowLayout.RIGHT,20,20)); add(new JLabel("First Name")); add(new JTextField(8)); add(new JLabel("MI")); add(new JTextField(1)); add(new JLabel("Last Name")); add(new JTextField(8)); // add(new JButton("OK"));// add(new JButton("OK"));// add(new JButton("Cancel")); //书上原话是:同一个按钮(好像有区别于对象button1)在容器中重复添加N次只能出现最后一次。 JButton button1=new JButton("once"); add(button1); add(button1); //可是这种。。。。。不算同一个按钮了?同一个按钮指的是对象吗,是引用吗,还是堆空间里的,
//只要new了就能往容器里添加一个 JButton button2=new JButton("hello"); add(button2); button2=new JButton("sayHello"); add(button2);
button2=new JButton("sayHello"); add(button2); button2=new JButton("sayHello"); add(button2); } public static void main(String[] args){ ShowFlowLayout frame=new ShowFlowLayout(); frame.setTitle("ShowFlowLayout"); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400,200); frame.setVisible(true); }}