顶层容器(重量级)
注意:每个应用程序至少有一个顶层容器,也就是我们说的窗口
中间容器
JFrame窗体的底层是RootPane(javax.swing.JRootPane)
示例:
import javax.swing.JFrame;
public class FirstSwingDemo {
public static void main(String[] args) {
JFrame jf = new JFrame("第一个Swing窗口");//创建一个Java预定义的窗体
/*方法1:setSize(int newWidth, newHeight)*/
jf.setSize(400,480);//设置窗体的大小
/*方法2:setSize(Dimension newSize)*/
Dimension d = new Dimension(100,100);
jf.setSize(d);
jf.setVisible(true);//设置窗体是否可见
}
}
方法
描述
setSize(int newWidth, int newHeight) / setSize(Dimension newSize)
设置窗体大小
setVisible(boolean visibleFlag)
设置窗体是否可见
setTitle(String newTitle)
设置窗体标题
示例(给窗体添加一个面板)
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FirstSwingDemo {
public static void main(String[] args) {
JFrame jf = new JFrame("第一个Swing窗口");//创建一个Java预定义的窗体
jf.setSize(400,480);//设置窗体的大小
jf.setVisible(true);//设置窗体是否可见
JPanel jp = new JPanel();//创建一个面板容器对象
Container container = jf.getContentPane();//将JPanel对象添加到父容器中
container.setLayout(null);//设置布局
container.add(jp);//添加组件
/**
* 设置布局和大小
*/
jp.setSize(100,100);
jp.setLocation(50,50);
/**
* 设置背景色
*/
jp.setBackground(Color.black);
}
}
JLabel是最容易使用的Swing组件,用来创建标签
最常用的构造方法是:
JLabel(String str):str用于标签的文本
JLabel(Icon icon):icon用于标签的图标;获取图标最容易的方式是使用ImageIcon类,它实现了Icon,所以可以将ImageIcon类型的对象作为参数传递给JLabel构造方法的Icon参数
JLabel jl = new JLabel("第一个标签");
jf.getContentPane().add(jl,BorderLayout.CENTER);
//显示图片
JLabel jl = new JLabel(new ImageIcon("icon.jpg"));
jf.getContentPane().add(jl);
示例:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FirstSwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("第一个Swing窗口");//创建一个Java预定义的窗体
Dimension d = new Dimension(400,200);
frame.setSize(d);
/**
* 添加一个用于显示的标签
*/
JLabel label = new JLabel();
label.setPreferredSize(new Dimension(d.width,60));
frame.getContentPane().add(label,BorderLayout.NORTH);
/**
* 添加文本输入框
*/
JTextField text = new JTextField();
text.setPreferredSize(new Dimension(d.width,60));
/**
* 添加文本输入框Enter事件
*/
text.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText(text.getText());
}
});
frame.getContentPane().add(text,BorderLayout.CENTER);
frame.setVisible(true);
}
}
以上这些都是AbstractButton的子类,包含了许多用于控制按钮行为的方法。如下:
通过以下方法可以获取和修改关联的文本内容:
示例:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SecondSwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("这是按钮的练习");
Dimension d = new Dimension(400,200);
frame.setSize(d);
JLabel label = new JLabel();
label.setPreferredSize(new Dimension(d.width,60));
frame.getContentPane().add(label,BorderLayout.NORTH);
/**
* 添加按钮
*/
JButton button = new JButton("按钮");
button.setPreferredSize(new Dimension(d.width,60));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("测试一下按钮");
}
});
frame.getContentPane().add(button,BorderLayout.SOUTH);//在窗体中增加按钮
frame.setVisible(true);//显示窗体
}
}
示例:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class ThirdSwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("这是组合框的测试");
Dimension d = new Dimension(400,200);
frame.setSize(d);
frame.setLayout(null);
ArrayList<String> cars = new ArrayList<>();
cars.add("哈弗");
cars.add("吉利");
cars.add("大众");
cars.add("本田");
cars.add("长安");
/**
* 创建下拉菜单
*/
JComboBox<String> box = new JComboBox<>();
box.setBounds(d.width/2-100,d.height/2-60,200,60);
/**
* 添加下拉菜单内容
*/
for(String brands : cars) {
box.addItem(brands);
}
box.setSelectedItem(cars.get(4));//设置当前选中的选项
frame.getContentPane().add(box,BorderLayout.CENTER);
frame.setVisible(true);
}
}
边框布局示例:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FourthSwingDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("BorderLayout示例");
Dimension d = new Dimension(400,200);
frame.setSize(d);
BorderLayout layout = new BorderLayout();
Container panel = frame.getContentPane();
panel.setLayout(layout);
Font font = new Font("黑体",Font.BOLD,24);
JButton north = new JButton("北");
north.setFont(font);
panel.add(north,BorderLayout.NORTH);
JButton south = new JButton("南");
south.setFont(font);
panel.add(south,BorderLayout.SOUTH);
JButton west = new JButton("西");
west.setFont(font);
panel.add(west,BorderLayout.WEST);
JButton east = new JButton("东");
east.setFont(font);
panel.add(east,BorderLayout.EAST);
JButton center = new JButton("中");
center.setFont(font);
panel.add(center,BorderLayout.CENTER);
frame.setVisible(true);
}
}
布置容器的流布局管理器
具有指定的对齐方式以及指定的水平和垂直间隙
align参数的值必须为以下的值之一:
FlowLayout.LEFT、FlowLayout.RIGHT、FlowLayout.CENTER、FlowLayout.LEADING、FlowLayout.TRAILING
流布局示例:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FlowLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout示例");
Dimension d = new Dimension(1280,1080);
frame.setSize(d);
JPanel panel = new JPanel();
panel.setBackground(new Color(200,200,200));
FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
panel.setLayout(layout);
frame.getContentPane().add(panel,BorderLayout.CENTER);
for(int i = 0; i < 2; i++) {
ImageIcon icon = new ImageIcon("myfinder.png");
JLabel label = new JLabel(icon);
label.setPreferredSize(new Dimension(
icon.getIconWidth(),icon.getIconHeight()));
panel.add(label);
}
JButton button = new JButton("窗体减半");
button.setFont(new Font("微软雅黑",Font.PLAIN,24));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.setSize(new Dimension(
panel.getWidth()/2,panel.getHeight()));
panel.doLayout();
}
});
panel.add(button);
frame.setVisible(true);
}
}
GridLayout示例:
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridLayoutDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout示例");
Dimension d = new Dimension(680,500);
frame.setSize(d);
JPanel gridPanel = new JPanel();
int rowMax = 5;
int colMax = 4;
gridPanel.setLayout(new GridLayout(rowMax,colMax));
frame.getContentPane().add(gridPanel);
String[][] signal = {
{"(",")","%","C"},
{"1","2","3","+"},
{"4","5","6","-"},
{"7","8","9","*"},
{"0",".","=","/"}
};
for (int i = 0; i < rowMax; i++) {
for(int j = 0; j < colMax; j++) {
JButton button = new JButton();
button.setFont(new Font("微软雅黑",Font.BOLD,54));
button.setText(signal[i][j]);
gridPanel.add(button);
}
}
frame.setVisible(true);
}
}
渐变色面板示例:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class GradientPanel extends JPanel{
public GradientPanel(BorderLayout borderLayout) {
super(borderLayout);
}
@ Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(!isOpaque()) {
return;
}
int width = this.getWidth();
int height = this.getHeight();
Graphics2D g2 = (Graphics2D)g;
GradientPaint grandientPaint = new GradientPaint(
width/4,
height/4,
Color.red,
width,
height,
Color.blue,
false);
g2.setPaint(grandientPaint);
g2.fillRect(0, 0, width, height);
}
public static void main(String[] args) {
JFrame frame = new JFrame("渐变色面板示例");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500,400);
GradientPanel grandientPanel = new GradientPanel(new BorderLayout());
frame.getContentPane().add(grandientPanel);
frame.setVisible(true);
}
}
常见的图形绘制方法:
示例:
@ Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(0, //起点横坐标
this.getHeight, //起点纵坐标
this.getWidth, //终点横坐标
this.getHeight/2);//终点纵坐标
}
常见的图形填充方法:
示例:
@ Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillArc(this.getWeight/2-100, //圆心横坐标
this.getHeight/2-100,//圆心纵坐标
200, 200, //圆的直径
0, 360);//起始弧度,终止弧度
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章