生活随笔
收集整理的這篇文章主要介紹了
课后练习----实现窗口的切换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、運用事件處理相關知識,完成兩個窗口之間的切換,例如:登陸窗口------》注冊窗口
2、對本次作業進行總結,在編程過程中遇到哪些問題,如何解決,有哪些收獲?
源代碼:
package bbb;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class myframe implements ActionListener {JFrame f1,f2;JPanel p1,p2;JButton b1,b2;JLabel l1,l2,l3;JTextField t1,t2;//文本框public myframe() {f1=new JFrame("LYX");f2=new JFrame("LYX");p1=new JPanel();p2=new JPanel();p1.setLayout(null);//絕對布局p2.setLayout(null);b1=new JButton("注冊");b2=new JButton("登錄");l1=new JLabel("賬戶"); l1.setFont(new Font("楷體",Font.BOLD,18));//自定義標簽字體l2=new JLabel("密碼");l2.setFont(new Font("楷體",Font.BOLD,18));l3=new JLabel("登錄成功!");l3.setForeground(Color.red);t1=new JTextField();t2=new JTextField();b1.addActionListener(this);b2.addActionListener(this);f1.add(p1);p1.add(b1);p1.add(b2);p1.add(l1);p1.add(l2);p1.add(t1);p1.add(t2);b1.setBounds(150,150,60,40);b2.setBounds(270,150,60,40);l1.setBounds(80,50,40,30);l2.setBounds(80,100,40,30);t1.setBounds(120,50,300,30);t2.setBounds(120,100,300,30);f1.setVisible(true);f1.setSize(500,300);}public static void main(String[] args) {new myframe();
}public void actionPerformed(ActionEvent e) {f1.setVisible(false);p2.setBackground(new Color(211,252,4));//自定義面板顏色f2.setVisible(true); f2.add(p2);p2.add(l3);l3.setBounds(225,75,150,60);f2.setLocation(600,0);f2.setSize(500,300);}
}
運行結果:
心得體會:
窗口的學習是循序漸進的,每次都覺得特別新穎、好玩、有成就感,這次編程中為了美觀,設計組件、窗口具體的位置是個麻煩的地方,還有程序的部分地方都是相似的,所以我選擇了復制粘貼,但忘記改組件名稱了,花了很長時間才發現問題。編程過程中也總會遺留一些細節,希望今后真正地學懂每個知識點,而不是死記硬背@-@
轉載于:https://www.cnblogs.com/leiyaxi/p/10859277.html
總結
以上是生活随笔為你收集整理的课后练习----实现窗口的切换的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。