要建设一个网站需要准备些什么,效果图素材网站,中国最新军事新闻西陆网,百度云wordpress怎么搭建Component作为基类#xff0c;提供了如下常用的方法来设置组件的大小、位置、可见性等。
方法签名方法功能setLocation(int x,int y)设置组件的位置setSize(int width,int heigth)设置组件的大小setBounds(int x,int y,int width,int heigth)设置组件的位置#xff0c;大小。…Component作为基类提供了如下常用的方法来设置组件的大小、位置、可见性等。
方法签名方法功能setLocation(int x,int y)设置组件的位置setSize(int width,int heigth)设置组件的大小setBounds(int x,int y,int width,int heigth)设置组件的位置大小。setVisible(Boolean b)设置该组件的可见性
Container作为容器根类提供了如下方法来访问容器中的组件
方法签名方法功能Component add(Compinent comp)向容器中添加其他组件该组件既可以是普通组件也可以是容器并返回被添加的组件Component getComponentAt(int x,int y):返回指定点的组件int getComponentCount():返回该容器内组件的数量Component[] getComponents():返回该容器内的所有组件
package com.aynu.container;import java.awt.*;public class WindowDemo {public static void main(String[] args) {//创建一个窗口对象Frame framenew Frame(这里测试Window窗口);//指定窗口的位置大小frame.setLocation(100,100);frame.setSize(500,300);//设置窗口对象可见frame.setVisible(true);}
}Panel容器
package com.aynu.container;import javax.swing.*;
import java.awt.*;public class PanelDemo {public static void main(String[] args) {//1.创建一个Window对象因为Panel以及其他的容器都不能独立存在必须依附于window存在Frame framenew Frame(这里演示Panel);//2.创建一个Panel对象Panel p new Panel();//3.创建一个文本框和一个按钮并且把他们放入到Panel容器中p.add(new TextField(这里是一个测试文本));p.add(new JButton(这里是一个测试按钮));//4.把panel放入到Window中frame.add(p);//5.设置window的位置以及大小frame.setBounds(100,100,500,300);//6.设置window可见frame.setVisible(true);}
}ScorllPane
package com.aynu.container;import javax.swing.*;
import java.awt.*;public class ScorllPaneDemo {public static void main(String[] args) {Frame framenew Frame(这里演示ScrollPane);//1.创建一个ScrollPane对象ScrollPane sp new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);//2.往ScrollPane中添加内容sp.add(new TextField(这是测试文本));sp.add(new JButton(这是测试按钮));//3.把ScrollPane添加到window中frame.add(sp);frame.setBounds(100,100,500,300);frame.setVisible(true);}
}