JComponent类
官方API:https://docs.oracle.com/javase/tutorial/uiswing/components/jcomponent.html#complookapi
除顶级容器外,名称以“J”开头的所有Swing组件都从 JComponent
该类开始。例如JPanel
,JScrollPane
,JButton
,和JTable
所有的继承JComponent
。然而,JFrame
和JDialog
千万不要因为它们实现顶层容器。
在JComponent
类扩展了 Container
类,它本身延伸 Component
。该Component
课程包括从提供布局提示到支持绘画和活动的所有内容。该Container
班有添加组件的容器,铺出来的支持。这部分的API表总结了最经常使用的方法Component
和Container
,以及的JComponent
。
JComponent功能
本JComponent
类提供了以下功能,它的后代:
工具提示
通过使用setToolTipText
方法指定字符串,您可以为组件的用户提供帮助。当光标暂停在组件上时,指定的字符串将显示在组件附近的小窗口中。
/**
* 组件工具提示
*/
package JComponent类;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ToolTipTextDemo {
private static void setToolTipText() {
JFrame frame=new JFrame("工具提示");
frame.setSize(300, 400);
frame.setLocation(300, 300);
frame.setLayout(null);
//设置一个按钮
JButton button=new JButton("按钮");
button.setBounds(50, 50, 100, 40);
//设置按钮组件提示
button.setToolTipText("这是一个按钮");
//将组件添加到主窗口
frame.add(button);
//设置一个标签
JLabel label=new JLabel("标签");
//设置标签的大小和位置
label.setBounds(50, 100,100, 40);
//设置标签组件提示
label.setToolTipText("这是一个标签");
//将组件添加到主窗口
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
setToolTipText();
}
}
绘画和边框
该setBorder
方法允许您指定组件在其边缘周围显示的边框。要绘制组件的内部,请覆盖该paintComponent
方法。
package JComponent类;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.EtchedBorder;
public class BorderDemo {
private static void setBorder() {
JFrame frame = new JFrame("设置边框");
frame.setSize(300, 400);
frame.setLocation(300, 300);
frame.setLayout(null);
// 设置一个按钮
JButton button1 = new JButton("按钮1");
// 为按钮设置红色实线边框
button1.setBorder(BorderFactory.createLineBorder(Color.red));
// 设置button的大小和位置
button1.setBounds(50, 50, 100, 30);
// 将button添加到主窗口
frame.add(button1);
// 设置一个按钮
JButton button2 = new JButton("按钮2");
// 为按钮设置边框
button2.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
// 设置button的大小和位置
button2.setBounds(50, 100, 100, 30);
// 将button添加到主窗口
frame.add(button2);
// 设置一个按钮
JButton button3 = new JButton("按钮3");
// 为按钮设置边框
button3.setBorder(BorderFactory.createRaisedBevelBorder());
// 设置button的大小和位置
button3.setBounds(50, 150, 100, 30);
// 将button添加到主窗口
frame.add(button3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
setBorder();
}
}
应用程序范围的可插拔外观
在幕后,每个JComponent
对象都有一个相应的ComponentUI
对象,可以执行所有绘图,事件处理,大小确定等操作JComponent
。究竟使用哪个ComponentUI
对象取决于您可以使用该UIManager.setLookAndFeel
方法设置的当前外观。
自定义属性
您可以将一个或多个属性(名称/对象对)与任何属性相关联JComponent
。例如,布局管理器可能使用属性将约束对象与其JComponent
管理的每个约束对象相关联。您使用putClientProperty
和getClientProperty
方法放置和获取属性。
支持布局
尽管Component
该类提供了诸如getPreferredSize
和的布局提示方法getAlignmentX
,但它没有提供任何设置这些布局提示的方法,缺少创建子类和覆盖方法。为了给你另一种方法来设置布局提示,则JComponent
类增加setter方法。
支持可访问性
本JComponent
类提供API和基本功能,以帮助辅助技术,如屏幕阅读器获取Swing组件的信息,有关辅助功能的更多信息,请参阅 如何支持辅助技术。
支持拖放
本JComponent
类提供的API来设置组件的传输处理程序,这是Swing的拖放支持的基础。有关详细信息,请参阅 DnD简介。
双缓冲
双缓冲平滑在屏幕上绘画。有关详细信息,请参阅 执行自定义绘画。
键绑定
当用户按下键盘上的键时,此功能会使组件做出反应。例如,在按钮具有焦点的许多外观中,键入Space键相当于鼠标单击按钮。外观自动设置按下和释放Space键之间的绑定以及对按钮产生的效果。有关键绑定的详细信息,请参阅 如何使用键绑定。
JComponent API
本JComponent
类提供了许多新的方法和继承了许多方法Component
和Container
。下表总结了我们最常使用的方法。
自定义组件外观
方法 | 目的 |
---|---|
void setBorder(Border) Border getBorder() | 设置或获取组件的边框。有关详细信息,请参见 如何使用边框。 |
void setForeground(Color) void setBackground(Color) | 设置组件的前景色或背景色。前景通常是用于在组件中绘制文本的颜色。假设组件是不透明的,背景是(毫不奇怪)组件背景区域的颜色。 |
Color getForeground() Color getBackground() | 获取组件的前景色或背景色。 |
void setOpaque(boolean) boolean isOpaque() | 设置或获取组件是否不透明。不透明组件用其背景颜色填充其背景。 |
void setFont(Font) Font getFont() | 设置或获取组件的字体。如果尚未为组件设置字体,则返回其父级的字体。 |
void setCursor(Cursor) Cursor getCursor() | 设置或获取光标显示在组件及其包含的所有组件上(除了具有自己的光标集的子组件)。例:aPanel.setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR)); |
设置和获取组件状态
方法 | 目的 |
---|---|
void setComponentPopupMenu(JPopupMenu) | 设置JPopupMenu 为此JComponent 。UI负责注册绑定并添加必要的侦听器,以便JPopupMenu 在适当的时间显示。当JPopupMenu 显示取决于外观:有些人可能会显示它上的鼠标事件,有些人可能启用键绑定。 如果popup 为null,则getInheritsPopupMenu 返回true ,然后getComponentPopupMenu 将委托给父级。这提供了一种使所有子组件继承popupmenu 父组件的方法。 |
void setTransferHandler(TransferHandler) TransferHandler getTransferHandler() | 设置或删除transferHandler 属性。所述TransferHandler 支撑件通过剪切,复制交换数据,或粘贴到/从剪贴板以及拖放。有关详细信息,请参阅 DnD简介。 |
void setToolTipText(String) | 设置要在工具提示中显示的文本。有关更多信息,请参见如何使用工具提示。 |
void setName(String) String getName() | 设置或获取组件的名称。当您需要将文本与不显示文本的组件相关联时,这非常有用。 |
boolean isShowing() | 确定组件是否在屏幕上显示。这意味着组件必须是可见的,并且必须位于可见和显示的容器中。 |
void setEnabled(boolean) boolean isEnabled() | 设置或获取组件是否已启用。启用的组件可以响应用户输入并生成事件。 |
void setVisible(boolean) boolean isVisible() | 设置或获取组件是否可见。组件最初是可见的,顶级组件除外。 |
处理事件
方法 | 目的 |
---|---|
void addHierarchyListener(hierarchyListener l) void removeHierarchyListener(hierarchyListener l) | 添加或删除指定的层次结构侦听器,以便在此容器所属的层次结构发生更改时从此组件接收层次结构更改事件。如果listener l为null,则不会抛出异常并且不执行任何操作。 |
void addMouseListener(MouseListener) void removeMouseListener(MouseListener) | 向组件添加或从组件中删除 鼠标侦听器。当用户使用鼠标与收听的组件进行交互时,将通知鼠标侦听器。 |
void addMouseMotionListener(MouseMotionListener) void removeMouseMotionListener(MouseMotionListener) | 向组件添加或从组件中删除 鼠标移动侦听器。当用户在侦听组件的边界内移动鼠标时,将通知鼠标移动侦听器。 |
void addKeyListener(KeyListener) void removeKeyListener(KeyListener) | 向组件添加或从组件中删除 键侦听器。当用户在键盘上键入并且收听的组件具有键盘焦点时,将通知键侦听器。 |
void addComponentListener(ComponentListener) void removeComponentListener(ComponentListener) | 向组件添加组件侦听器或从组件中删除 组件侦听器。监听组件被隐藏,显示,移动或调整大小时会通知组件侦听器。 |
boolean contains(int,int) boolean contains(Point) | 确定指定的点是否在组件内。应根据组件的坐标系指定参数。这两个int 参数分别指定x和y坐标。 |
Component getComponentAt(int, int) Component getComponentAt(Point) | |
Component setComponentZOrder(component comp, int index) | 将指定的组件移动到容器中指定的z-order索引。 如果组件是某个其他容器的子组件,则在将其添加到此容器之前将其从该容器中删除。此方法之间的重要区别在于java.awt.Container.add(Component, int) ,removeNotify 除非必要且底层本机窗口系统允许,否则此方法在从前一个容器中删除组件时不会调用该组件。这样,如果组件具有键盘焦点,则在移动到新位置时它会保持焦点。 注意: z顺序确定组件的绘制顺序。具有最高z次序涂料的组件首先涂漆,具有最低z次序涂料的组件最后涂漆。在组件重叠的情况下,具有较低z次序的组件在具有较高z次序的组件上绘制。 |
Component getComponentZOrder(component comp) | 返回容器内组件的z顺序索引。组件在z顺序层次结构中越高,其索引越低。具有最低z顺序索引的组件最后绘制,高于所有其他子组件。 |
绘画组件
方法 | 目的 |
---|---|
void repaint() void repaint(int,int,int,int) | 请求重新绘制全部或部分组件。这四个int 参数指定要绘制的矩形的边界(x,y,宽度,高度,按此顺序)。 |
void repaint(Rectangle) | 请求重新绘制组件中的指定区域。 |
void revalidate() | 请求再次布置组件及其受影响的容器。除非在可见之后显式更改组件的大小/对齐提示,否则通常不需要调用此方法,或者在可见后更改包含层次结构。总是repaint 在之后调用revalidate 。 |
void paintComponent(Graphics) | 绘制组件。重写此方法以实现自定义组件的绘制。 |
处理包含层次结构
方法 | 目的 |
---|---|
Component add(Component) Component add(Component, int) void add(Component, Object) | 将指定的组件添加到此容器中。此方法的单参数版本将组件添加到容器的末尾。如果存在,int 参数表示新组件在容器中的位置。如果存在,则Object 参数为当前布局管理器提供布局约束。 |
void remove(int) void remove(Component) void removeAll() | 从此容器中删除一个或所有组件。如果存在,则int 参数指示要移除的组件的容器内的位置。 |
JRootPane getRootPane() | 获取包含该组件的根窗格。 |
Container getTopLevelAncestor() | 获取组件的最顶层容器 - a Window ,Applet 如果组件尚未添加到任何容器,则返回null。 |
Container getParent() | 获取组件的直接容器。 |
int getComponentCount() | 获取此容器中的组件数。 |
Component getComponent(int) Component[] getComponents() | 获取此容器中的一个或所有组件。该int 参数指示组件的位置来获得。 |
Component getComponentZOrder(int) Component[] getComponentZOrder() | 返回容器内组件的z顺序索引。组件在z顺序层次结构中越高,其索引越低。具有最低z顺序索引的组件最后绘制,高于所有其他子组件。 |
布局组件
方法 | 目的 |
---|---|
void setPreferredSize(Dimension) void setMaximumSize(Dimension) void setMinimumSize(Dimension) | 设置组件的首选,最大或最小尺寸,以像素为单位。首选大小表示组件的最佳尺寸。组件不应大于其最大大小,且不小于其最小大小。请注意,这些仅是提示,某些布局管理器可能会忽略这些提示。 |
Dimension getPreferredSize() Dimension getMaximumSize() Dimension getMinimumSize() | 获取组件的首选大小,最大大小或最小大小(以像素为单位)。许多JComponent类都有setter和getter方法。对于那些JComponent 没有相应setter方法的非子类,可以通过创建子类并覆盖这些方法来设置组件的首选,最大或最小大小。 |
void setAlignmentX(float) void setAlignmentY(float) | 沿x轴或y轴设置对齐。这些值表示组件如何相对于其他组件对齐。该值应为0到1之间的数字,其中0表示沿原点对齐,1表示距离原点最远,0.5表示居中,依此类推。请注意,这些仅是提示,某些布局管理器可能会忽略这些提示。 |
float getAlignmentX() float getAlignmentY() | 获取组件沿x轴或y轴的对齐方式。对于JComponent 没有相应setter方法的非子类,可以通过创建子类并覆盖这些方法来设置组件的对齐方式。 |
void setLayout(LayoutManager) LayoutManager getLayout() | 设置或获取组件的布局管理器。布局管理器负责在容器内调整和定位组件。 |
void applyComponentOrientation(ComponentOrientation)void setComponentOrientation(ComponentOrientation) | 设置ComponentOrientation 此容器的属性及其中包含的所有组件。有关更多信息,请参阅设置容器的方向。 |
获取大小和位置信息
方法 | 目的 |
---|---|
int getWidth() int getHeight() | 获取以像素为单位测量的组件的当前宽度或高度。 |
Dimension getSize() Dimension getSize(Dimension) | 以像素为单位获取组件的当前大小。使用此方法的单参数版本时,调用方负责创建Dimension 返回结果的实例。 |
int getX() int getY() | 获取组件原点相对于父级左上角的当前x或y坐标(以像素为单位)。 |
Rectangle getBounds() Rectangle getBounds(Rectangle) | 获取以像素为单位测量的组件边界。边界指定组件相对于其父级的宽度,高度和原点。使用此方法的单参数版本时,调用方负责创建Rectangle 返回结果的实例。 |
Point getLocation() Point getLocation(Point) | 获取组件相对于父级左上角的当前位置(以像素为单位)。使用单参数版本的getLocation 方法时,调用者负责创建Point 返回结果的实例。 |
Point getLocationOnScreen() | 返回相对于屏幕左上角的位置。 |
Insets getInsets() | 获取组件边框的大小。 |
指定绝对大小和位置
方法 | 目的 |
---|---|
void setLocation(int,int) void setLocation(Point) | 设置组件相对于父级左上角的位置(以像素为单位)。这两个int 参数按顺序指定x和y。不使用布局管理器时,使用这些方法定位组件。 |
void setSize(int,int) void setSize(Dimension) | 设置以像素为单位测量的组件大小。这两个int 参数按顺序指定宽度和高度。不使用布局管理器时,使用这些方法调整组件的大小。 |
void setBounds(int,int,int,int) void setBounds(Rectangle) | 设置相对于父组件左上角的大小和位置(以像素为单位)。这四个int 参数按顺序指定x,y,width和height。在不使用布局管理器时,使用这些方法来定位和调整组件的大小。 |
实例
实例1-自定义组件外观
/**
* 自定义组件外观
*/
package JComponent类;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
public class SetLookDemo {
private static void setLook() {
JFrame frame = new JFrame("设置边框");
frame.setSize(300, 400);
frame.setLocation(300, 300);
frame.setLayout(null);
// 设置一个按钮
JButton button1 = new JButton("按钮1");
//设置按钮的字体
button1.setFont(new Font("宋体",Font.BOLD,20));
//获取组件设置的字体
Font f=button1.getFont();
System.out.println("按钮设置的字体是:"+f);
// 设置按钮的边框
Border border=BorderFactory.createLineBorder(Color.red);
button1.setBorder(border);
//获取按钮的边框
Border bor=button1.getBorder();
System.out.println("按钮的边框是:"+bor);
//设置组件是否不透明
button1.setOpaque(true);//不透明
button1.setOpaque(false);//按钮透明
//获取组件是否被设置为不透明
System.out.println("按钮组件是否被设置为不透明:"+button1.isOpaque());
//设置按钮的前景色
button1.setForeground(Color.green);
//获取按钮的前景色
Color col=button1.getForeground();
System.out.println("按钮的前景色是:"+col);
//设置按钮的背景色
button1.setBackground(Color.orange);
//获取按钮的背景色
Color col2=button1.getBackground();
System.out.println("按钮的背景色是:"+col2);
//设置光标显示在组件上
button1.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//获取显示在组件上的光标
Cursor cur=button1.getCursor();
System.out.println("显示在组件上的光标是:"+cur);
// 设置button的大小和位置
button1.setBounds(50, 50, 100, 30);
// 将button添加到主窗口
frame.add(button1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
setLook();
}
}
实例2-设置和获取组件状态
/**
* 设置和获取组件状态
*/
package JComponent类;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.Border;
public class SetComponentStatement {
private static void setStatement() {
JFrame frame = new JFrame("设置边框");
frame.setSize(300, 400);
frame.setLocation(300, 300);
frame.setLayout(null);
// 设置一个按钮
JButton button1 = new JButton("按钮");
// 设置button的大小和位置
button1.setBounds(50, 50, 100, 30);
//设置工具提示
button1.setToolTipText("这是一个按钮");
//设置组件名称
button1.setName("我是按钮");
//获取组件名称
String name=button1.getName();
System.out.println("按钮组件的名称是:"+name);
//确定组件是否在屏幕上显示
button1.isShowing();
// 将button添加到主窗口
frame.add(button1);
//设置组件是否已经启用
button1.setEnabled(false);
System.out.println("组件是否已经启用:"+button1.isEnabled());
//设置组件是否可见
button1.setVisible(true);
System.out.println("组件是否可见:"+button1.isVisible());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
setStatement();
}
}
实例3-处理包含层次结构
/**
* 处理包含层次结构
*/
package JComponent类;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;
public class SetComponent {
private static void setComponent() {
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setLocation(300, 300);
frame.setLayout(null);
// 设置一个按钮
JButton button1 = new JButton("按钮");
// 设置button的大小和位置
button1.setBounds(50, 50, 100, 30);
// 删除组件
// frame.remove(button1);
// 删除所有组件
// frame.removeAll();
// 添加组件到主窗口
frame.add(button1);
// 获取包含组件的根窗格
JRootPane jrp = button1.getRootPane();
System.out.println("该组件的根窗格是:" + jrp);
//获取组件的最顶层容器
Container con=button1.getTopLevelAncestor();
System.out.println("该组件的最顶层容器是:"+con);
//获取组件的直接容器
Container con2=button1.getParent();
System.out.println("该组件的直接容器:"+con2);
//获取此容器中的组件数
int count=frame.getComponentCount();
System.out.println("此容器中的组件数:"+count);
//获取此容器中的一个组件
Component con3=frame.getComponent(0);//从0开始的
System.out.println("此容器中的一个组件:"+con3);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
setComponent();
}
}
实例4-布局组件
/**
* 布局组件
*/
package JComponent类;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import javax.swing.JButton;
import javax.swing.JFrame;
public class SetLayoutConponent {
private static void layoutConponent() {
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setLocation(300, 300);
frame.setLayout(null);
// 设置一个按钮
JButton button1 = new JButton("按钮");
// 设置button的大小和位置
button1.setBounds(50, 50, 100, 30);
//添加组件到主窗口
frame.add(button1);
//设置组件的首选值
Dimension setpreferredSize=new Dimension(100,50);
button1.setPreferredSize(setpreferredSize);
//获取组件的首选大小
Dimension getPreferredSize=button1.getPreferredSize();
System.out.println("组件的首选大小为:"+getPreferredSize);
//设置组件的最大值
Dimension setMaxSize=new Dimension(100,100);
button1.setMaximumSize(setMaxSize);
//获取组件的最大值
Dimension getMaxSize=button1.getMaximumSize();
System.out.println("组件的最大大小为:"+getMaxSize);
//设置组件的最小值
Dimension setMinSize=new Dimension(20,20);
button1.setMinimumSize(setMinSize);
//获取组件的最小值
Dimension getMinSize=button1.getMinimumSize();
System.out.println("组件的最小大小为:"+getMinSize);
//设置组件沿x轴对齐
button1.setAlignmentX(0.5f);
//获取组件沿x轴的对齐方式
System.out.println("组件沿x轴对齐方式为:"+button1.getAlignmentX());
//设置组件沿y轴对齐
button1.setAlignmentY(0.5f);
//获取组件沿y轴的对齐方式
System.out.println("组件沿y轴对齐方式为:"+button1.getAlignmentY());
//设置布局管理器
frame.setLayout(new FlowLayout());
//获取布局管理器
LayoutManager lm=frame.getLayout();
System.out.println("容器的布局管理器为:"+lm);
//设置ComponentOrientation
ComponentOrientation co=ComponentOrientation.LEFT_TO_RIGHT;
button1.applyComponentOrientation(co);
button1.setComponentOrientation(co);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
layoutConponent();
}
}
实例5-获取大小和位置信息
/**
* 获取组件大小和位置信息
*/
package JComponent类;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GetSizeAndLocationInfo {
private static void getSizeInfo() {
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setLocation(300, 300);
frame.setLayout(null);
// 设置一个按钮
JButton button1 = new JButton("按钮");
button1.setBounds(50, 50, 100, 50);
// 将button添加到主窗口
frame.add(button1);
// 获取组件的当前宽度
int width = button1.getWidth();
System.out.println("组件的当前宽度为:" + width);
// 获取组件的当前高度
int height = button1.getHeight();
System.out.println("组件的当前高度为:" + height);
// 获取组件的X位置
int x = button1.getX();
System.out.println("组件的当前X位置为:" + x);
// 获取组件的Y位置
int y = button1.getY();
System.out.println("组件的当前Y位置为:" + y);
// 获取组件的大小
Dimension di = button1.getSize();
System.out.println("组件的当前大小为:" + di);
// 获取组件的位置
Rectangle rect = button1.getBounds();
System.out.println("组件的当前位置为:" + rect);
// 获取组件相对于父级组件左上角的当前位置
Point point = button1.getLocation();
System.out.println("组件相对于父级组件左上角位置为:" + point);
// 获取组件边框的大小
Insets in = button1.getInsets();
System.out.println("组件边框的大小为:" + in);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
getSizeInfo();
}
}
实例6-指定绝对大小和位置
package JComponent类;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
public class SetAbsoluteSizeAndLocationDemo {
private static void setSizeAndLocation() {
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setLocation(300, 300);
frame.setLayout(null);
// 设置一个按钮
JButton button1 = new JButton("按钮");
// 设置组件的位置
button1.setLocation(300, 200);
// 获取组件的位置
System.out.println("组件位置为:" + button1.getLocation());
// 通过Point来设置组件新位置
Point p = new Point(0, 0);
button1.setLocation(p);
//设置组件大小
button1.setSize(100,50);
//获取组件的大小
System.out.println("组件大小为:"+button1.getSize());
//通过Dimension来设置组件大小
Dimension d=new Dimension(80,60);
button1.setSize(d);
//同时设置组件大小和位置
button1.setBounds(50, 50, 100, 50);
//获取组件大小和位置
System.out.println("获取组件大小和位置:"+button1.getBounds());
//通过Rectangle设置组件大小和位置
Rectangle rect=new Rectangle(10,10,200,80);
button1.setBounds(rect);
// 将button添加到主窗口
frame.add(button1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
setSizeAndLocation();
}
}