Java程序圖形用戶界面設(shè)計(jì)之按鈕與布局
Java程序設(shè)計(jì) 圖形用戶界面【四】
按鈕組件 JButton
JButton組件表示一個(gè)普通的按鈕
JButton類常用方法
方法 | 作用 |
---|---|
public JButton() throws HeadlessException | 創(chuàng)建一個(gè)Button對(duì)象 |
public JButton(String label) throws HeadlessException | 創(chuàng)建一個(gè)Button對(duì)象,同時(shí)指定其顯示內(nèi)容 |
public JButton(Icon icon) | 創(chuàng)建一個(gè)帶圖片的按鈕 |
public JButton(String text,Icon icon) | 創(chuàng)建一個(gè)帶圖片和文字的按鈕 |
public void setLabel(String label) | 設(shè)置Button的顯示內(nèi)容 |
public String getLabel() | 得到Button的顯示內(nèi)容 |
public void setBounds(int x,int y,int width,int height) | 設(shè)置組件的大小及顯示方式 |
public void setMnemonic(int mnemonic) | 設(shè)置按鈕的快捷鍵 |
演示
import javax.swing.*; import java.awt.*; public class Hello { public static void main(String[] args) { JFrame frame = new JFrame("一"); JButton but = new JButton("點(diǎn)擊"); 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種常見(jiàn)的布局管理器:FlowLayout、BorderLayout、GridLayout、CardLayout、絕對(duì)定位。
FlowLayout
流式布局管理器,使用此種布局方式會(huì)使所有的組件像流水一樣依次進(jìn)行排列
常量 | 作用 |
---|---|
public static final int CENTER | 居中對(duì)齊 |
public static final int LEADING | 與容器的開(kāi)始端對(duì)齊方式一樣 |
public static final int LEFT | 左對(duì)齊 |
public static final int RIGHT | 右對(duì)齊 |
public static final int TRAILING | 與容器的結(jié)束端對(duì)齊方式一樣 |
方法 | 作用 |
---|---|
public FlowLayout() | 構(gòu)造一個(gè)新的FlowLayout,居中對(duì)齊 |
public FlowLayout(int align) | 構(gòu)造一個(gè)FlowLayout,并指定對(duì)齊方式 |
public FlowLayout(int align,int hgap,int vgap) | 指定對(duì)齊方式、水平、垂直間距 |
演示:
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); } }
BorderLayout
BorderLayout將一個(gè)窗體的版面劃分成東、西、南、北、中5個(gè)區(qū)域
常量 | 作用 |
---|---|
public static final String EAST | 將組件設(shè)置在東區(qū) |
public static final String WEST | 將組件設(shè)置在西區(qū) |
public static final String SOUTH | 將組件設(shè)置在南區(qū) |
public static final String NORTH | 將組件設(shè)置在北區(qū) |
public static final String CENTER | 將組件設(shè)置在中區(qū) |
方法 | 作用 |
---|---|
public BorderLayout() | 構(gòu)造沒(méi)有間距的布局器 |
public BorderLayout(int hgap,int vgap) | 構(gòu)造有水平和垂直間距的布局器 |
演示:
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); } }
GridLayout
GridLayout布局管理器是以表格的形式進(jìn)行管理
方法 | 作用 |
---|---|
public GridLayout(int rows,int cols) | 構(gòu)造一個(gè)指定行和列的布局管理器 |
public GridLayout(int rows,int cols,int hgap,int vgap) | 構(gòu)造時(shí)指定行和列、水平和垂直間距 |
演示:
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); } }
CardLayout
CardLayout就是將一組組件彼此重疊地進(jìn)行布局,就像一張張卡片一樣,這樣每次只會(huì)展現(xiàn)一個(gè)界面
方法 | 作用 |
---|---|
public CardLayout() | 構(gòu)造CardLayout對(duì)象,各組件間距為0 |
public CardLayout(int hgap,int vgap) | 構(gòu)造CardLayout對(duì)象,指定組件間距 |
public void next(Container parent) | 翻轉(zhuǎn)到下一張卡片 |
public void previous(Container parent) | 翻轉(zhuǎn)到上一張卡片 |
public void first(Container parent) | 翻轉(zhuǎn)到第一張卡片 |
public void last(Container parent) | 翻轉(zhuǎn)到最后一張卡片 |
public void show(Container parent,String name) | 顯示具有指定組件名稱的卡片 |
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); } } }
絕對(duì)定位
Component中提供了setBounds()方法,可以定位一個(gè)組件的坐標(biāo),使用X、Y的坐標(biāo)表示方式
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); } }
到此這篇關(guān)于Java程序圖形用戶界面設(shè)計(jì)之按鈕與布局的文章就介紹到這了,更多相關(guān)Java 圖形界面按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)單的五子棋游戲示例代碼
這篇文章主要為大家介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的五子棋游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java游戲開(kāi)發(fā)有一定幫助,需要的可以參考一下2022-05-05SpringBoot使用flyway初始化數(shù)據(jù)庫(kù)
這篇文章主要介紹了SpringBoot如何使用flyway初始化數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-03-03SpringBoot+JPA?分頁(yè)查詢指定列并返回指定實(shí)體方式
這篇文章主要介紹了SpringBoot+JPA?分頁(yè)查詢指定列并返回指定實(shí)體方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java并發(fā)之ArrayBlockingQueue詳細(xì)介紹
這篇文章主要介紹了java并發(fā)之ArrayBlockingQueue詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-05-05