導航:首頁 > 編程語言 > java2圖形設計

java2圖形設計

發布時間:2024-12-05 02:32:31

java圖形界面設計如何從一個界面跳到另外界面再返回

import java.awt.BorderLayout;x0dx0aimport java.awt.Color;x0dx0aimport java.awt.FlowLayout;x0dx0aimport java.awt.Font;x0dx0aimport java.awt.event.ActionEvent;x0dx0aimport java.awt.event.ActionListener;x0dx0aimport java.awt.event.KeyEvent;x0dx0aimport java.awt.event.KeyListener;x0dx0aimport java.util.Calendar;x0dx0ax0dx0aimport javax.swing.JButton;x0dx0aimport javax.swing.JFrame;x0dx0aimport javax.swing.JLabel;x0dx0aimport javax.swing.JOptionPane;x0dx0aimport javax.swing.JPasswordField;x0dx0aimport javax.swing.JTextArea;x0dx0ax0dx0apublic class Test2{x0dx0apublic Test2() {}x0dx0ax0dx0aJFrame frame; //主界面框架x0dx0ax0dx0a//主界面函數x0dx0apublic void mainFace() {x0dx0aframe=new JFrame("主界面");x0dx0aframe.setLayout(new BorderLayout());x0dx0ax0dx0aJLabel label=new JLabel("這是主界面");x0dx0aframe.add(label,BorderLayout.CENTER);x0dx0ax0dx0aJButton button=new JButton("進入另一個界面");x0dx0aframe.add(button,BorderLayout.SOUTH);x0dx0abutton.addActionListener(new ActionListener() {x0dx0ax0dx0a@Overridex0dx0apublic void actionPerformed(ActionEvent e) {x0dx0aotherFace(); //進入另一個界面x0dx0aframe.dispose(); //主界面關閉x0dx0a}x0dx0ax0dx0a});x0dx0ax0dx0aframe.setSize(400,300);x0dx0aframe.setLocation(100,50);x0dx0aframe.setVisible(true);x0dx0aframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);x0dx0a}x0dx0ax0dx0a//另一個圖形界面x0dx0apublic void otherFace() {x0dx0afinal JFrame oframe=new JFrame("另一個界面");x0dx0aoframe.setLayout(new BorderLayout());x0dx0ax0dx0aJTextArea displayArea=new JTextArea();x0dx0aoframe.add(displayArea,BorderLayout.CENTER);x0dx0adisplayArea.setFont(new Font("宋體",Font.BOLD,30));x0dx0adisplayArea.setForeground(Color.red);x0dx0ax0dx0adisplayArea.append("\n\n\n\n 這是另一個圖形界面");x0dx0ax0dx0aJButton button1=new JButton("返回主界面");x0dx0aoframe.add(button1,BorderLayout.SOUTH);x0dx0ax0dx0abutton1.addActionListener(new ActionListener() {x0dx0ax0dx0a@Overridex0dx0apublic void actionPerformed(ActionEvent e) {x0dx0aframe.setVisible(true); //主界面顯示x0dx0aoframe.dispose(); //另一個界面關閉x0dx0a}x0dx0ax0dx0a});x0dx0ax0dx0aoframe.setSize(400,300);x0dx0aoframe.setLocation(150,100);x0dx0aoframe.setVisible(true);x0dx0a}x0dx0ax0dx0a//密碼輸入函數x0dx0apublic void passwordFrame() {x0dx0afinal JFrame keyFrame=new JFrame("Password Input");x0dx0akeyFrame.setLayout(new FlowLayout());x0dx0ax0dx0afinal JPasswordField keyField=new JPasswordField();x0dx0akeyField.setColumns(30);x0dx0akeyField.setFont(new Font("宋體",Font.BOLD,20));x0dx0akeyField.setEchoChar('@');x0dx0akeyFrame.add(keyField);x0dx0a x0dx0aCalendar calendar=Calendar.getInstance();x0dx0ax0dx0aint mon=calendar.get(Calendar.MONTH)+1;x0dx0aint day=calendar.get(Calendar.DAY_OF_MONTH);x0dx0aint h=calendar.get(Calendar.HOUR_OF_DAY);x0dx0aint m=calendar.get(Calendar.MINUTE);x0dx0ax0dx0afinal String nowKey=mon+"."+day+"."+h+"."+m; //密碼為現在時間的 月.日.時.分x0dx0ax0dx0akeyField.addKeyListener(new KeyListener() { //給密碼框添加鍵盤監聽器x0dx0ax0dx0apublic void keyPressed(KeyEvent e) {x0dx0aif(e.getKeyCode()==KeyEvent.VK_ENTER) {x0dx0achar[] key=keyField.getPassword();x0dx0aString keyStr = String.valueOf(key);x0dx0ax0dx0aif(keyStr.equals(nowKey)) {x0dx0amainFace(); //進入主界面x0dx0akeyFrame.dispose();x0dx0a}x0dx0aelse {x0dx0aJOptionPane.showMessageDialog(null, "輸入的密碼不正確!");x0dx0a}x0dx0a}x0dx0a}x0dx0ax0dx0a@Overridex0dx0apublic void keyReleased(KeyEvent e) {}x0dx0ax0dx0apublic void keyTyped(KeyEvent e) {}x0dx0ax0dx0a});x0dx0ax0dx0aJButton sureButton=new JButton("確認");x0dx0akeyFrame.add(sureButton);x0dx0asureButton.addActionListener(new ActionListener() { //給'確定'按鈕添加動作監聽器x0dx0ax0dx0apublic void actionPerformed(ActionEvent e) {x0dx0achar[] key=keyField.getPassword();x0dx0aString keyStr = String.valueOf(key);x0dx0ax0dx0aif(keyStr.equals(nowKey)) {x0dx0amainFace(); //進入主界面x0dx0akeyFrame.dispose();x0dx0a}x0dx0aelse {x0dx0aJOptionPane.showMessageDialog(null, "輸入的密碼不正確!");x0dx0a}x0dx0a}x0dx0ax0dx0a});x0dx0ax0dx0aJButton cancelButton=new JButton("取消");x0dx0akeyFrame.add(cancelButton);x0dx0acancelButton.addActionListener(new ActionListener() {x0dx0ax0dx0apublic void actionPerformed(ActionEvent e) {x0dx0aSystem.exit(0);x0dx0a}x0dx0ax0dx0a});x0dx0ax0dx0akeyFrame.setSize(400,120);x0dx0akeyFrame.setLocation(300,300);x0dx0akeyFrame.setVisible(true);x0dx0akeyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);x0dx0a}x0dx0ax0dx0a//程序主函數x0dx0apublic static void main(String args[]) {x0dx0aTest2 t2=new Test2();x0dx0at2.passwordFrame();x0dx0a}x0dx0a}

閱讀全文

與java2圖形設計相關的資料

熱點內容
勁舞團輔助源碼論壇 瀏覽:277
春運在哪個app買機票便宜 瀏覽:686
南京程序員借錢 瀏覽:26
android拍照獲取圖片路徑 瀏覽:556
pdf文件轉換excel 瀏覽:546
湖南伺服器安全雲空間 瀏覽:607
文件夾是只讀怎麼辦 瀏覽:895
linuxtelnet退出命令 瀏覽:248
稅務局預交2演算法 瀏覽:533
前饋pid演算法 瀏覽:147
安卓怎麼使用投影儀 瀏覽:940
雲計算伺服器貴州雲主機 瀏覽:393
淘米APP是哪個公司 瀏覽:454
程序員玩車 瀏覽:476
安卓騰訊手游換成蘋果手機怎麼辦 瀏覽:528
主播皮克桃玩的什麼安卓區 瀏覽:567
linux單詞統計 瀏覽:909
小朋友參加密室大逃脫 瀏覽:218
程序員的三大忌 瀏覽:286
空調壓縮機機頭 瀏覽:767