Java 圖形界面
1. 組件
JAVA的圖形界面下有兩組控件,一組是awt,一組是swing。 一般都是使用swing
1.1 標籤
(1) Label用於顯示文字
import java.awt.Color;
import javax.swing.JFrame;import javax.swing.JLabel;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JLabel l = new JLabel("LOL文字");
(2)文字顏色
l.setForeground(Color.red);
l.setBounds(50, 50, 280, 30);
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
(3)使用JLabel顯示圖片
java GUI 顯示圖片是通過在label上設置圖標實現的
import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JLabel l = new JLabel();
//根據圖片創建ImageIcon對象
ImageIcon i = new ImageIcon("e:/project/j2se/shana.png");
//設置ImageIcon
l.setIcon(i);
//label的大小設置爲ImageIcon,否則顯示不完整
l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
f.add(l);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
3 按鈕
JButton 普通按鈕
import javax.swing.JButton;import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("一鍵秒對方基地掛");
b.setBounds(50, 50, 280, 30);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
4 複選框
JCheckBox 複選框
使用isSelected來獲取是否選中了
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JCheckBox bCheckBox = new JCheckBox("物理英雄");
//設置 爲 默認被選中
bCheckBox.setSelected(true);
bCheckBox.setBounds(50, 50, 130, 30);
JCheckBox bCheckBox2 = new JCheckBox("魔法英雄");
bCheckBox2.setBounds(50, 100, 130, 30);
//判斷 是否 被 選中
System.out.println(bCheckBox2.isSelected());
f.add(bCheckBox);
f.add(bCheckBox2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
5單選框
JRadioButton 單選框
使用isSelected來獲取是否選中了
爲了實現只能選中一個,還需要用到ButtonGroup
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 200);
f.setLayout(null);
JRadioButton b1 = new JRadioButton("物理英雄");
// 設置 爲 默認被選中
b1.setSelected(true);
b1.setBounds(50, 50, 130, 30);
JRadioButton b2 = new JRadioButton("魔法 英雄");
b2.setBounds(50, 100, 130, 30);
System.out.println(b2.isSelected());
f.add(b1);
f.add(b2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
6 按鈕組
ButtonGroup 對按鈕進行分組,把不同的按鈕,放在同一個分組裏 ,同一時間,只有一個 按鈕 會被選中
import javax.swing.ButtonGroup;import javax.swing.JFrame;import javax.swing.JRadioButton;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 240);
f.setLayout(null);
JRadioButton b1 = new JRadioButton("物理英雄");
b1.setSelected(true);
b1.setBounds(50, 50, 130, 30);
JRadioButton b2 = new JRadioButton("魔法 英雄");
b2.setBounds(50, 100, 130, 30);
// 按鈕分組
ButtonGroup bg = new ButtonGroup();
// 把b1,b2放在 同一個 按鈕分組對象裏 ,這樣同一時間,只有一個 按鈕 會被選中
bg.add(b1);
bg.add(b2);
f.add(b1);
f.add(b2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
7 下拉框
JComboBox 下拉框
使用getSelectedItem來獲取被選中項
使用setSelectedItem() 來指定要選中項
import javax.swing.JComboBox;import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 240);
f.setLayout(null);
//下拉框出現的條目
String[] heros = new String[] { "卡特琳娜", "庫奇" };
JComboBox cb = new JComboBox(heros);
cb.setBounds(50, 50, 80, 30);
f.add(cb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
8 對話框
JOptionPane 用於彈出對話框
JOptionPane.showConfirmDialog(f, “是否 使用外掛 ?”);
表示詢問,第一個參數是該對話框以哪個組件對齊
JOptionPane.showInputDialog(f, “請輸入yes,表明使用外掛後果自負”);
接受用戶的輸入
JOptionPane.showMessageDialog(f, “你使用外掛被抓住! “);
顯示消息
mport javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(580, 240);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
int option = JOptionPane.showConfirmDialog(f, "是否 使用外掛 ?");
if (JOptionPane.OK_OPTION == option) {
String answer = JOptionPane.showInputDialog(f, "請輸入yes,表明使用外掛後果自負");
if ("yes".equals(answer))
JOptionPane.showMessageDialog(f, "你使用外掛被抓住! 封號一年!");
}
}
}
9 文本框
JTextField 輸入框
setText 設置文本
getText 獲取文本
JTextField 是單行文本框,如果要輸入多行數據,請使用JTextArea
tfPassword.grabFocus(); 表示讓密碼輸入框獲取焦點
import java.awt.Dimension;import java.awt.FlowLayout;
import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JLabel lName = new JLabel("賬號:");
// 輸入框
JTextField tfName = new JTextField("");
tfName.setText("請輸入賬號");
tfName.setPreferredSize(new Dimension(80, 30));
JLabel lPassword = new JLabel("密碼:");
// 輸入框
JTextField tfPassword = new JTextField("");
tfPassword.setText("請輸入密碼");
tfPassword.setPreferredSize(new Dimension(80, 30));
f.add(lName);
f.add(tfName);
f.add(lPassword);
f.add(tfPassword);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
tfPassword.grabFocus();
}
}
10 密碼框
JPasswordField 密碼框
與文本框不同,獲取密碼框裏的內容,推薦使用getPassword,該方法會返回一個字符數組,而非字符串
import java.awt.Dimension;import java.awt.FlowLayout;
import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPasswordField;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JLabel l = new JLabel("密碼:");
// 密碼框
JPasswordField pf = new JPasswordField("");
pf.setText("&48kdh4@#");
pf.setPreferredSize(new Dimension(80, 30));
// 與文本框不同,獲取密碼框裏的內容,推薦使用getPassword,該方法會返回一個字符數組,而非字符串
char[] password = pf.getPassword();
String p = String.valueOf(password);
System.out.println(p);
f.add(l);
f.add(pf);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
11 文本域
JTextArea:文本域。
和文本框JTextField不同的是,文本域可以輸入多行數據
如果要給文本域初始文本,通過\n來實現換行效果
JTextArea通常會用到append來進行數據追加
如果文本太長,會跑出去,可以通過setLineWrap(true) 來做到自動換行
import java.awt.Dimension;import java.awt.FlowLayout;
import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextArea;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JLabel l = new JLabel("文本域:");
JTextArea ta = new JTextArea();
ta.setPreferredSize(new Dimension(200, 150));
//\n換行符
ta.setText("搶人頭!\n搶你妹啊搶!\n");
//追加數據
ta.append("我去送了了了了了了了了了了了了了了了了了了了了了了了了");
//設置自動換行
ta.setLineWrap(true);
f.add(l);
f.add(ta);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
12 進度條
import java.awt.Dimension;import java.awt.FlowLayout;
import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JProgressBar;import javax.swing.JTextArea;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JProgressBar pb = new JProgressBar();
//進度條最大100
pb.setMaximum(100);
//當前進度是50
pb.setValue(50);
//顯示當前進度
pb.setStringPainted(true);
f.add(pb);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
13 文件選擇器
JFileChooser 表示文件選擇器
使用FileFilter用於僅選擇.txt文件
fc.setFileFilter(new FileFilter() {
public String getDescription() {
return ".txt";
}
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".txt");
}
});
fc.showOpenDialog(); 用於打開文件
fc.showSaveDialog(); 用於保存文件
import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;
import javax.swing.JButton;import javax.swing.JFileChooser;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.filechooser.FileFilter;
public class TestGUI {
public static void main(String[] args) {
final JFrame f = new JFrame("LOL");
f.setLayout(new FlowLayout());
final JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
// TODO Auto-generated method stub
return ".txt";
}
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".txt");
}
});
JButton bOpen = new JButton("打開文件");
JButton bSave = new JButton("保存文件");
f.add(bOpen);
f.add(bSave);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250, 150);
f.setLocationRelativeTo(null);
f.setVisible(true);
bOpen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(f);
File file = fc.getSelectedFile();
if (returnVal == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(f, "計劃打開文件:" + file.getAbsolutePath());
}
}
});
bSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showSaveDialog(f);
File file = fc.getSelectedFile();
if (returnVal == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(f, "計劃保存到文件:" + file.getAbsolutePath());
}
}
});
}
}
面板
1 基本面板
JPanel 即爲基本面板
面板和JFrame一樣都是容器,不過面板一般用來充當中間容器,把組件放在面板上,然後再把面板放在窗體上。
一旦移動一個面板,其上面的組件,就會全部統一跟着移動,採用這種方式,便於進行整體界面的設計
import java.awt.Color;import java.awt.FlowLayout;
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JPanel p1 = new JPanel();
// 設置面板大小
p1.setBounds(50, 50, 300, 60);
// 設置面板背景顏色
p1.setBackground(Color.RED);
// 這一句可以沒有,因爲JPanel默認就是採用的FlowLayout
p1.setLayout(new FlowLayout());
JButton b1 = new JButton("英雄1");
JButton b2 = new JButton("英雄2");
JButton b3 = new JButton("英雄3");
// 把按鈕加入面板
p1.add(b1);
p1.add(b2);
p1.add(b3);
JPanel p2 = new JPanel();
JButton b4 = new JButton("英雄4");
JButton b5 = new JButton("英雄5");
JButton b6 = new JButton("英雄6");
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setBackground(Color.BLUE);
p2.setBounds(10, 150, 300, 60);
// 把面板加入窗口
f.add(p1);
f.add(p2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
2 ContentPanel
JFrame上有一層面板,叫做ContentPane
平時通過f.add()向JFrame增加組件,其實是向JFrame上的 ContentPane加東西
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("一鍵秒對方基地掛");
b.setBounds(50, 50, 280, 30);
f.add(b);
// JFrame上有一層面板,叫做ContentPane
// 平時通過f.add()向JFrame增加組件,其實是向JFrame上的 ContentPane加東西
// f.add等同於f.getContentPane().add(b);
f.getContentPane().add(b);
// b.getParent()獲取按鈕b所處於的容器
// 打印出來可以看到,實際上是ContentPane而非JFrame
System.out.println(b.getParent());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
3 SplitPanel
創建一個水平JSplitPane,左邊是pLeft,右邊是pRight
import java.awt.Color;import java.awt.FlowLayout;
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JSplitPane;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JPanel pLeft = new JPanel();
pLeft.setBounds(50, 50, 300, 60);
pLeft.setBackground(Color.RED);
pLeft.setLayout(new FlowLayout());
JButton b1 = new JButton("蓋倫");
JButton b2 = new JButton("提莫");
JButton b3 = new JButton("安妮");
pLeft.add(b1);
pLeft.add(b2);
pLeft.add(b3);
JPanel pRight = new JPanel();
JButton b4 = new JButton("英雄4");
JButton b5 = new JButton("英雄5");
JButton b6 = new JButton("英雄6");
pRight.add(b4);
pRight.add(b5);
pRight.add(b6);
pRight.setBackground(Color.BLUE);
pRight.setBounds(10, 150, 300, 60);
// 創建一個水平JSplitPane,左邊是p1,右邊是p2
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pLeft, pRight);
// 設置分割條的位置
sp.setDividerLocation(80);
// 把sp當作ContentPane
f.setContentPane(sp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
4 JScrollPanel
使用帶滾動條的面板有兩種方式
1. 在創建JScrollPane,把組件作爲參數傳進去
JScrollPane sp = new JScrollPane(ta);
2 希望帶滾動條的面板現實其他組件的時候,調用setViewportView
sp.setViewportView(ta);
import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
//準備一個文本域,在裏面放很多數據
JTextArea ta = new JTextArea();
for (int i = 0; i < 1000; i++) {
ta.append(String.valueOf(i));
}
//自動換行
ta.setLineWrap(true);
JScrollPane sp = new JScrollPane(ta);
f.setContentPane(sp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
5 TabbedPanel
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JPanel p1 = new JPanel();
p1.setBounds(50, 50, 300, 60);
p1.setBackground(Color.RED);
p1.setLayout(new FlowLayout());
JButton b1 = new JButton("英雄1");
JButton b2 = new JButton("英雄2");
JButton b3 = new JButton("英雄3");
p1.add(b1);
p1.add(b2);
p1.add(b3);
JPanel p2 = new JPanel();
JButton b4 = new JButton("英雄4");
JButton b5 = new JButton("英雄5");
JButton b6 = new JButton("英雄6");
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.setBackground(Color.BLUE);
p2.setBounds(10, 150, 300, 60);
JTabbedPane tp = new JTabbedPane();
tp.add(p1);
tp.add(p2);
// 設置tab的標題
tp.setTitleAt(0, "紅色tab");
tp.setTitleAt(1, "藍色tab");
ImageIcon i = new ImageIcon("e:/project/j2se/j.png");
tp.setIconAt(0,i );
tp.setIconAt(1,i );
f.setContentPane(tp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
6 CardLayerout
CardLayerout 佈局器 很像TTabbedPanel ,在本例裏面上面是一個下拉框,下面是一個CardLayerout 的JPanel
這個JPanel裏有兩個面板,可以通過CardLayerout方便的切換
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("CardLayerout");
JPanel comboBoxPane = new JPanel();
String buttonPanel = "按鈕面板";
String inputPanel = "輸入框面板";
String comboBoxItems[] = { buttonPanel, inputPanel };
JComboBox<String> cb = new JComboBox<>(comboBoxItems);
comboBoxPane.add(cb);
// 兩個Panel充當卡片
JPanel card1 = new JPanel();
card1.add(new JButton("按鈕 1"));
card1.add(new JButton("按鈕 2"));
card1.add(new JButton("按鈕 3"));
JPanel card2 = new JPanel();
card2.add(new JTextField("輸入框", 20));
JPanel cards; // a panel that uses CardLayout
cards = new JPanel(new CardLayout());
cards.add(card1, buttonPanel);
cards.add(card2, inputPanel);
f.add(comboBoxPane, BorderLayout.NORTH);
f.add(cards, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(250, 150);
f.setLocationRelativeTo(null);
f.setVisible(true);
cb.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent evt) {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, (String) evt.getItem());
}
});
}
}