Swing視窗架構是由一個JFrame元件放入JPanel容器,再加入JLabel、JButton等元件
今天我們要說到的有:
JLabel(String text, int horizontalAlignment) 如果不須定義位置horizontalAlignment可不寫
horizontalAlignment => LEFT, RIGHT, CENTER, LEADING, TRAILING
用法: JLabel myJLabel = new JLabel("Your text");
public JLabel(Icon image, int horizontalAlignment) 如果不須定義位置horizontalAlignment可不寫
用法: JLabel myJLabel = new JLabel(icon1, JLabel.CENTER);
=> 顯示一個名為icon1的icon圖示,並置中
public JLabel(String text, Icon image, int horizontalAlignment)
用法: JLabel myJLabel = new JLabel("Here is is your tag", icon1, JLabel.CENTER);
ImageIcon(String filename)
filname => 圖像路徑
用法: ImageIcon icon = new ImageIcon("路徑")
import javax.swing.*; // 載入swing套件
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class MyJFrame extends JFrame { // 定義我們的JFrame類別繼承JFrame
private JPanel contentPane;
String[] imgName = new String[]{"Panda", "Cat", "Dog"};
JLabel[] lbl = new JLabel[imgName.length];
JLabel[] lblImg = new JLabel[imgName.length];
MyJFrame(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(650, 400, 635, 300);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
for (int i=0; i<imgName.length; i++){
lblImg[i] = new JLabel();
lblImg[i].setIcon(new ImageIcon("C:\\Users\\Max\\OneDrive\\桌面\\image\\" + imgName[i] + ".jpg"));
lblImg[i].setBounds(i*200 + 20, 30, 180, 135);
contentPane.add(lblImg[i]);
lbl[i] = new JLabel(imgName[i]);
lbl[i].setBounds(i*200 + 100, 180, 50, 20);
contentPane.add(lbl[i]);
}
setVisible(true);
}
}
public class Main extends JPanel{
public static void main(String[] args) {
MyJFrame window = new MyJFrame();
}
}
接著我想做一個帳密登入的跳出框
所以其他的元件一併介紹完一次結合起來使用
JTextField(String text, int columns)
text => 要顯示的字串 | columns => 設定欄位寬度是幾行
用法: JTextField jtxtF = new JTextField("一起來寫Java", 25);
注意: 除了要繼承JFrame還要實作addActionListener
JButton(String text, Icon icon)
用法: JButton jbtn = new JButton("確認", icon);
JOptionPane(Object message, int messageType, int optionType)
messageType:
parent是對話框的父視窗的意思
import javax.swing.*;
import java.awt.event.*;
class MyJFrame extends JFrame {
private JPanel contentPane;
private JTextField txtId, txtPwd;
private JLabel lblId, lblPwd;
private JButton btnLogin;
MyJFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 280, 180);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
txtId = new JTextField();
txtId.setToolTipText("輸入你的帳號");
txtId.setColumns(20);
txtId.setBounds(100, 20, 120, 25);
contentPane.add(txtId);
txtPwd = new JTextField();
txtPwd.setToolTipText("輸入你的密碼");
txtPwd.setColumns(20);
txtPwd.setBounds(100, 60, 120, 25);
contentPane.add(txtPwd);
lblId = new JLabel("帳號:");
lblId.setBounds(50, 25, 100, 15);
contentPane.add(lblId);
lblPwd = new JLabel("密碼:");
lblPwd.setBounds(50, 65, 100, 15);
contentPane.add(lblPwd);
btnLogin = new JButton("登入");
btnLogin.setBounds(100, 100, 80, 25);
contentPane.add(btnLogin);
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (txtId.getText().equals("account") && txtPwd.getText().equals("123456")) {
JOptionPane.showMessageDialog(null, "登入成功", "登入中", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(null, "帳密錯誤", "登入中", JOptionPane.ERROR_MESSAGE);
}
}
});
setVisible(true);
}
}
public class Main {
public static void main(String[] args) {
MyJFrame f = new MyJFrame();
}
}