※ 此程式碼並非優化版本,僅供個人學習用途,若有任何問題請告知。
程式碼如下:
結果如下:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.CardLayout; | |
import java.awt.Font; | |
import java.awt.Menu; | |
import java.awt.MenuBar; | |
import java.awt.MenuItem; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.ItemEvent; | |
import java.awt.event.ItemListener; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.swing.JButton; | |
import javax.swing.JComboBox; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
import javax.swing.JTextField; | |
public class personal extends JFrame { | |
public static void create(int x, int y, int width, int height) { | |
jframe(x, y, width, height); | |
} | |
public static void info(JFrame info_jf, int x, int y, boolean visible) { | |
info_jf.setBounds(x+50, y+50, 250, 130); | |
info_jf.setDefaultCloseOperation(info_jf.EXIT_ON_CLOSE); | |
info_jf.setResizable(false); | |
JPanel info_jp = new JPanel(); | |
info_jp.setLayout(null); | |
info_jf.add(info_jp); | |
JLabel info_text = new JLabel("練習用程式"); | |
info_text.setBounds(90, 5, 100, 50); | |
info_jp.add(info_text); | |
JLabel info_text2 = new JLabel("感謝使用"); | |
info_text2.setBounds(95, 30, 100, 50); | |
info_jp.add(info_text2); | |
info_jf.setVisible(visible); | |
} | |
public static void jframe(int x, int y, int width, int height) { | |
/*視窗*/ | |
JFrame jf = new JFrame("簡易個人資料"); | |
jf.setBounds(x, y, width, height); | |
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); | |
jf.setResizable(false); | |
/*選單列*/ | |
MenuBar menu = new MenuBar(); | |
jf.setMenuBar(menu); | |
Menu file = new Menu("檔案"); | |
MenuItem newfile = new MenuItem("另開視窗"); | |
file.add(newfile); | |
MenuItem restart = new MenuItem("重新啟動"); | |
file.add(restart); | |
MenuItem line = new MenuItem("------------"); | |
file.add(line); | |
MenuItem exit = new MenuItem("結束"); | |
file.add(exit); | |
menu.add(file); | |
Menu edit = new Menu("編輯"); | |
MenuItem clear = new MenuItem("全部重置"); | |
edit.add(clear); | |
menu.add(edit); | |
Menu help = new Menu("幫助"); | |
MenuItem about = new MenuItem("關於此程式"); | |
help.add(about); | |
menu.add(help); | |
/*面板*/ | |
CardLayout cards = new CardLayout(); | |
JPanel main = new JPanel(); //主面板 | |
main.setLayout(cards); | |
JPanel first_panel = new JPanel(); //第一個面板 | |
first_panel.setLayout(null); | |
JPanel second_panel = new JPanel(); //第二個面板 | |
second_panel.setLayout(null); | |
main.add("1", first_panel); | |
main.add("2", second_panel); | |
jf.add(main); | |
/*文字顯示*/ | |
JLabel name = new JLabel("*姓名:"); | |
name.setBounds(20, 10, 50, 30); | |
first_panel.add(name); | |
JLabel sex = new JLabel("*性別:"); | |
sex.setBounds(225, 10, 50, 30); | |
first_panel.add(sex); | |
JLabel birth = new JLabel("*出生年月日:"); | |
birth.setBounds(20, 60, 100, 30); | |
first_panel.add(birth); | |
JLabel mail = new JLabel("*電子信箱:"); | |
mail.setBounds(20, 110, 100, 30); | |
first_panel.add(mail); | |
JLabel address = new JLabel("通訊地址:"); | |
address.setBounds(20, 160, 100, 30); | |
first_panel.add(address); | |
JLabel note = new JLabel("備註事項:"); | |
note.setBounds(20, 210, 100, 30); | |
first_panel.add(note); | |
/*單行輸入*/ | |
JTextField name_tf = new JTextField(); //姓名 | |
name_tf.setBounds(70, 10, 110, 30); | |
first_panel.add(name_tf); | |
JTextField mail_tf = new JTextField(); //電子信箱 | |
mail_tf.setBounds(95, 110, 235, 30); | |
first_panel.add(mail_tf); | |
JTextField address_tf = new JTextField(); //通訊地址 | |
address_tf.setBounds(90, 160, 240, 30); | |
first_panel.add(address_tf); | |
/*年月日各種集合*/ | |
List<Integer> yearlist = new ArrayList<Integer>(); //年 | |
for(int i = 2015; i >= 1958; --i) {yearlist.add(i);} | |
List<Integer> monthlist = new ArrayList<Integer>(); //月 | |
for(int i = 1; i <= 12; ++i) {monthlist.add(i);} | |
List<Integer> date_two28list = new ArrayList<Integer>(); //天數_二月(平年) | |
for(int i = 1; i <= 28; ++i) {date_two28list.add(i);} | |
List<Integer> date_two29list = new ArrayList<Integer>(); //天數_二月(閏年) | |
for(int i = 1; i <= 29; ++i) {date_two29list.add(i);} | |
List<Integer> date_thirty = new ArrayList<Integer>(); //天數_偶數月 | |
for(int i = 1; i <= 30; ++i) {date_thirty.add(i);} | |
List<Integer> date_thirtyone = new ArrayList<Integer>(); //天數_奇數月 | |
for(int i = 1; i <= 31; ++i) {date_thirtyone.add(i);} | |
/*下拉式選單*/ | |
JComboBox sexbox = new JComboBox(); //性別 | |
sexbox.addItem("男"); | |
sexbox.addItem("女"); | |
sexbox.setBounds(275, 10, 55, 30); | |
first_panel.add(sexbox); | |
JComboBox yearbox = new JComboBox(); //出生年月日 //年 | |
for(Integer n : yearlist) {yearbox.addItem(n);} | |
yearbox.setBounds(105, 60, 55, 30); | |
first_panel.add(yearbox); | |
JLabel year = new JLabel("年"); | |
year.setBounds(170, 60, 20, 30); | |
first_panel.add(year); | |
JComboBox monthbox = new JComboBox(); //月 | |
for(Integer n : monthlist) {monthbox.addItem(n);} | |
monthbox.setBounds(190, 60, 40, 30); | |
first_panel.add(monthbox); | |
JLabel month = new JLabel("月"); | |
month.setBounds(240, 60, 20, 30); | |
first_panel.add(month); | |
JComboBox datebox = new JComboBox(); //日 | |
datebox.setBounds(265, 60, 40, 30); | |
for(Integer n : date_thirtyone) {datebox.addItem(n);} | |
first_panel.add(datebox); | |
JLabel date = new JLabel("日"); | |
date.setBounds(315, 60, 20, 30); | |
first_panel.add(date); | |
/*多行輸入*/ | |
JTextArea note_contain = new JTextArea(); //備註事項 | |
JScrollPane note_contain_jsp = new JScrollPane(note_contain); | |
note_contain_jsp.setBounds(20, 245, 310, 80); | |
first_panel.add(note_contain_jsp); | |
/*按鈕*/ | |
JButton finish = new JButton("送出"); | |
finish.setBounds(270, 340, 60, 30); | |
finish.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent arg0) { | |
boolean check = false; | |
if(name_tf.getText().equals("") || mail_tf.getText().equals("")) {check = false;} | |
else {check = true;} | |
if(check == true) { | |
JLabel finish_info = new JLabel("感謝您的填寫"); | |
Font finish_info_text = new Font(Font.DIALOG, Font.BOLD, 30); | |
finish_info.setFont(finish_info_text); | |
finish_info.setBounds(80, 150, 300, 30); | |
second_panel.add(finish_info); | |
cards.show(main, "2"); | |
} | |
else { | |
JOptionPane.showMessageDialog(null, "仍有未填之資訊", "提醒您", JOptionPane.WARNING_MESSAGE); | |
} | |
} | |
}); | |
first_panel.add(finish); | |
/*判斷日期變化(閏年、7月8月、奇偶數天)*/ | |
monthbox.addItemListener(new ItemListener() { | |
public void itemStateChanged(ItemEvent arg0) { | |
int month_temp = Integer.parseInt(monthbox.getSelectedItem().toString()); | |
datebox.removeAllItems(); | |
int year_temp = Integer.parseInt(yearbox.getSelectedItem().toString()); | |
if(month_temp == 2) { | |
if((((year_temp / 4) == 0) && ((year_temp / 100) != 0)) || ((year_temp / 400) == 0)) {for(Integer n : date_two29list) {datebox.addItem(n);}} | |
else {for(Integer n : date_two28list) {datebox.addItem(n);}} | |
yearbox.addItemListener(new ItemListener() { | |
public void itemStateChanged(ItemEvent arg0) { | |
int year_temp = Integer.parseInt(yearbox.getSelectedItem().toString()); //取得目前所選年份 | |
datebox.removeAllItems(); | |
if((((year_temp % 4) == 0) && ((year_temp % 100) != 0)) || ((year_temp % 400) == 0)) {for(Integer n : date_two29list) {datebox.addItem(n);}} | |
else {for(Integer n : date_two28list) {datebox.addItem(n);}} | |
} | |
}); | |
} | |
else if((month_temp == 7) || (month_temp == 8)) {for(Integer n : date_thirtyone) {datebox.addItem(n);}} | |
else if((month_temp % 2 == 0 && month_temp < 7) || (month_temp % 2 != 0 && month_temp > 8)) {for(Integer n : date_thirty) {datebox.addItem(n);}} | |
else {for(Integer n : date_thirtyone) {datebox.addItem(n);}} | |
} | |
}); | |
/*選單列各項功能*/ | |
JFrame info_jf = new JFrame("關於此程式"); | |
newfile.addActionListener(new ActionListener() { //另開視窗 | |
public void actionPerformed(ActionEvent arg0) { | |
create(x+10, y+10, width, height); | |
} | |
}); | |
restart.addActionListener(new ActionListener() { //重新啟動 | |
public void actionPerformed(ActionEvent arg0) { | |
jf.setVisible(false); | |
info(info_jf, x, y, false); | |
create(x, y, width, height); | |
} | |
}); | |
exit.addActionListener(new ActionListener() { //結束 | |
public void actionPerformed(ActionEvent arg0) { | |
System.exit(0); | |
} | |
}); | |
clear.addActionListener(new ActionListener() { //全部重置 | |
public void actionPerformed(ActionEvent arg0) { | |
name_tf.setText(null); //清空JTextField | |
mail_tf.setText(null); | |
address_tf.setText(null); | |
sexbox.setSelectedIndex(0); //JComboBox回到第一格 | |
yearbox.setSelectedIndex(0); | |
monthbox.setSelectedIndex(0); | |
datebox.setSelectedIndex(0); | |
} | |
}); | |
about.addActionListener(new ActionListener() { //關於此程式 | |
public void actionPerformed(ActionEvent arg0) { | |
info(info_jf, x, y, true); | |
} | |
}); | |
jf.setVisible(true); | |
} | |
public static void main(String[] args) { | |
create(400, 180, 365, 445); | |
} | |
} |
結果如下:
沒有留言:
張貼留言