JButton组件表示一个普通的按钮
JButton类常用方法
演示
import javax.swing.*;import java.awt.*;public class Hello { public static void main(String[] args) { Jframe frame = new Jframe("一"); JButton but = new JButton("点击"); Font font = new Font("Serief",Font.BOLD,25); but.setFont(font); frame.add(but); frame.setSize(200,70); frame.setLocation(500,300); frame.setVisible(true); }}
按钮上显示图片
import javax.swing.*;public class Hello { public static void main(String[] args) { Jframe frame = new Jframe("一"); String Path="C:\Users\30452\Desktop\123.jpg"; Icon icon = new ImageIcon(Path,"MLDN"); JButton but = new JButton(icon); frame.add(but); frame.setSize(500,600); frame.setLocation(300,200); frame.setVisible(true); }}
布局管理器在Swing中主要使用以下5种常见的布局管理器:FlowLayout、BorderLayout、GridLayout、CardLayout、绝对定位。
FlowLayout流式布局管理器,使用此种布局方式会使所有的组件像流水一样依次进行排列
演示:
import javax.swing.*;import java.awt.*;public class Hello { public static void main(String[] args) { Jframe frame = new Jframe("一"); frame.setLayout(new FlowLayout(FlowLayout.CENTER,4,4)); JButton but = null; for(int i=0;i<16;i++){ but = new JButton("按钮"); frame.add(but); } frame.setSize(300,300); frame.setVisible(true); }}
BorderLayoutBorderLayout将一个窗体的版面划分成东、西、南、北、中5个区域
演示:
import javax.swing.*;import java.awt.*;public class Hello { public static void main(String[] args) { Jframe frame = new Jframe("一"); frame.setLayout(new BorderLayout(3,3)); frame.add(new JButton("上"),BorderLayout.NORTH); frame.add(new JButton("下"),BorderLayout.SOUTH); frame.add(new JButton("左"),BorderLayout.WEST); frame.add(new JButton("右"),BorderLayout.EAST); frame.add(new JButton("中"),BorderLayout.CENTER); frame.pack(); frame.setVisible(true); }}
GridLayoutGridLayout布局管理器是以表格的形式进行管理
演示:
import javax.swing.*;import java.awt.*;public class Hello { public static void main(String[] args) { Jframe frame = new Jframe("一"); frame.setLayout(new GridLayout(3,5,3,3)); JButton but = null; for(int i=0;i<15;i++){ but = new JButton("按钮"); frame.add(but); } frame.pack(); frame.setVisible(true); }}
CardLayoutCardLayout就是将一组组件彼此重叠地进行布局,就像一张张卡片一样,这样每次只会展现一个界面
import javax.swing.*;import java.awt.*;public class Hello { public static void main(String[] args) { Jframe frame = new Jframe("一");// 取得窗体容器 Container cont = frame.getContentPane(); CardLayout card = new CardLayout(); frame.setLayout(card); cont.add(new JLabel("A",JLabel.CENTER),"first"); cont.add(new JLabel("B",JLabel.CENTER),"second"); cont.add(new JLabel("C",JLabel.CENTER),"third"); cont.add(new JLabel("D",JLabel.CENTER),"fourth"); cont.add(new JLabel("E",JLabel.CENTER),"fifth"); frame.pack(); frame.setVisible(true); card.show(cont,"third"); for(int i=0;i<5;i++){ try { Thread.sleep(3000); }catch (InterruptedException e){ } card.next(cont); } }}
绝对定位Component中提供了setBounds()方法,可以定位一个组件的坐标,使用X、Y的坐标表示方式
public void setBounds(int x,int y,int width,int height)
演示:
import javax.swing.*;import javax.swing.plaf.ButtonUI;public class Hello { public static void main(String[] args) { Jframe frame = new Jframe("一"); frame.setLayout(null); JLabel title = new JLabel("确定要退出吗?"); JButton a = new JButton("确定"); JButton b = new JButton("取消"); frame.setSize(200,90); title.setBounds(45,5,150,20); a.setBounds(10,30,80,20); b.setBounds(100,30,80,20); frame.add(title); frame.add(a); frame.add(b); frame.setVisible(true); }}