java學習應用:視覺化視窗模擬基礎聊天視窗(模仿QQ)
阿新 • • 發佈:2019-02-09
最近在由於學校有個機器人大賽,所以舉辦方進行了幾節java的教學,接下來算是我對最近一些學習的總結,記錄一下,有待提高!
一、java視覺化視窗JFrame的簡單運用,先看看一些簡單的效果(真的很基礎):
1、調出登入介面
2、輸入賬號密碼(沒有資料庫,只好自己定了)、
2.1、密碼錯誤
2.2、密碼正確
3、點開聊天介面
4、傳送
5、下面是程式碼,以後加油完善,先記錄下
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class myChat { public void loginUI() { //creat a new frame for loginUI JFrame login_frame = new JFrame(); login_frame.setSize(300,200); // we must set size of frame,otherwise ,the frame will be 0 x 0 login_frame.setTitle("myChat"); //Title login_frame.setLocationRelativeTo(null);//locate the frame to the mid of screen login_frame.setLayout(new FlowLayout());//Set linear distribution JLabel usrname = new JLabel("usrename"); JLabel psw = new JLabel("password"); JTextField f1 = new JTextField(18); JPasswordField f2 = new JPasswordField(18); JButton login = new JButton("login"); // set a button called "login" JButton reset = new JButton("reset"); // set a button called "reset" //Adding components,flow the list login_frame.add(usrname); login_frame.add(f1); login_frame.add(psw); login_frame.add(f2); login_frame.add(login); login_frame.add(reset); //Listener-click ActionListener listener = new ActionListener() { //the abstract method of ActionListener public void actionPerformed(ActionEvent event) { String textName = f1.getText(); String textPassword = f2.getText(); //if we want to go to next step ,we must close. login_frame.setVisible(false); //check the password and username if(textName.equals("admin") && textPassword.equals("123")) { mainUI(); // right -> goto next step } // error -> point error else { JFrame login_Error = new JFrame();//set a frame to point the error message login_Error.setSize(200,100); login_Error.setLocationRelativeTo(null); JLabel reLoginMessage = new JLabel("username or password error !"); //add the message JButton sureButton = new JButton("sure"); login_Error.add(reLoginMessage); login_Error.add(sureButton); ActionListener errorListener = new ActionListener() { public void actionPerformed(ActionEvent event) { login_Error.setVisible(false); //let the error frame hide loginUI(); //let the login frame dispaly; } }; //set the listener for sureButton sureButton.addActionListener(errorListener); login_Error.setVisible(true); } } }; //set an ActionListener for the button login.addActionListener(listener); login_frame.setVisible(true); } //mainUI public void mainUI() { //name list String [] nameStd = {"one","two","three","four","five","six","seven","eight","nine","ten"}; JFrame main_frame = new JFrame(); main_frame.setTitle("main_frame"); main_frame.setLayout(new FlowLayout()); main_frame.setSize(260,600); main_frame.setLocation(900,50); for(int i=0;i<10;i++) { String friend_name=nameStd[i]; JButton now = new JButton(friend_name); now.setPreferredSize(new Dimension(200,50)); main_frame.add(now); // chose someone to talk ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event) { chatUI(friend_name); } }; now.addActionListener(listener); } main_frame.setVisible(true); } //chatUI public void chatUI(String talkWith){ JFrame chat_frame = new JFrame(); chat_frame.setTitle(talkWith); chat_frame.setSize(450,480); chat_frame.setLocationRelativeTo(null); chat_frame.setLayout(new FlowLayout()); JTextArea show_area = new JTextArea(15,35); JTextArea input_area = new JTextArea(7,35); JButton send = new JButton("send"); // if we click "send" ,the imformation of input_are will turn to show_area ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent event) { show_area.append(input_area.getText()+"\r\n"); show_area.setLineWrap(true); input_area.setText(""); } }; // send imformation send.addActionListener(listener); chat_frame.add(show_area); chat_frame.add(input_area); chat_frame.add(send); chat_frame.setVisible(true); } //main method public static void main(String[] agrs) { myChat qq = new myChat(); qq.loginUI(); } }
英語比較差,還在練習中………