JAVA GUI實驗

GUI的API

問題一:

實現這麼一個功能:採用圖形界面實現兩個內容的交換

在這裏插入圖片描述

首先我們需要一個窗口farme,一個畫布panel,2個文本編輯框,1個按鈕,和一個監聽器。然後對文本框和按鈕的位置進行編輯,再按照要求實現在監聽器裏實現功能。

//package com.company;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import  java.lang.String;
import java.util.Scanner;


public class Main {
    static  JFrame frame=new JFrame("Testchange");
    static  JPanel panel=new JPanel();
    static JTextField ustext=new JTextField(20);
    static JTextField us2=new JTextField(20);
    static JButton chag=new JButton("change");
    public static void main(String[] args)  {

       frame.setSize(350,200);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.add(panel);
       sovle();
       frame.setVisible(true);
   }
   public static void sovle(){
       panel.setLayout(null);
       ustext.setBounds(10,20,100,25);
       us2.setBounds(120,20,100,25);
       panel.add(ustext);
       panel.add(us2);
       chag.setBounds(230,20,80,25);
       Mylistener myl=new Mylistener();
       chag.addActionListener(myl);
       panel.add(chag);
       System.out.print(us2.getText());


   }
    static class Mylistener implements ActionListener{
        // @Override
        public void actionPerformed(ActionEvent e) {
            String x=e.getActionCommand();
            if(x=="change"){
                 String t1=us2.getText();
                 String t2=ustext.getText();
                 us2.setText(t2);
                 ustext.setText(t1);
            }

        }
    }

}


在這裏插入圖片描述
在這裏插入圖片描述

問題二:

採用圖形界面設計如下圖2所示的界面。並能夠實現當點擊“+”按鈕時,標籤給出點擊次數標題

有了上一個的基礎,那麼這個也就很簡單,一個不可修改的文本框,1個按鈕,1個監聽器,1個窗口,1個畫布。在監聽器中實現功能(我們可以騷一點調個利於眼睛的背景色,改個字體啥的)

//package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import  java.lang.String;
import java.util.Scanner;


public class Main {
    static  int cnt=0;
    static  JFrame frame=new JFrame("ZY2");
    static  JPanel panel=new JPanel();
    static JTextField ustext=new JTextField(20);
    static JTextField us2=new JTextField(20);
    static JButton chag=new JButton("+");
    public static void main(String[] args)  {

       frame.setSize(350,200);
       panel.setBackground(Color.YELLOW);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.add(panel);
       sovle();
       frame.setVisible(true);
   }
   public static void sovle(){
       panel.setLayout(null);
       ustext.setBounds(10,20,150,25);
       //us2.setBounds(120,20,100,25);
       ustext.setEditable(false);
       ustext.setFont(new Font("宋體",Font.BOLD,12));
       ustext.setText("點擊加號計數");
       panel.add(ustext);
      // panel.add(us2);
       chag.setBounds(170,20,50,25);
       Mylistener myl=new Mylistener();
       chag.addActionListener(myl);
       panel.add(chag);
       System.out.print(us2.getText());


   }
    static class Mylistener implements ActionListener{
        // @Override
        public void actionPerformed(ActionEvent e) {
            String x=e.getActionCommand();
            if(x=="+"){
                cnt++;
                 String t1="你一共點擊了"+cnt+"次";
                 ustext.setText(t1);
            }

        }
    }

}

在這裏插入圖片描述
在這裏插入圖片描述

問題三:我們來做個簡單的註冊頁(僅界面)

package com.company;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import  java.lang.String;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {
    static  String check="12333";
    static  String check2;
    static  JFrame frame=new JFrame("ZY3");
    static  JPanel panel=new JPanel();
    static  JLabel lname=new JLabel("用戶名");
    static  JLabel lpw=new JLabel("密碼");
    static  JLabel lemail=new JLabel("郵箱");
    static  JLabel lck=new JLabel("驗證碼");
    static JPasswordField pw=new JPasswordField(20);
    static JTextField ustext=new JTextField(20);
    static JTextField us2=new JTextField(20);
    static JTextField us3=new JTextField(20);
    static JButton chag=new JButton("獲取驗證碼");
    static JButton cof=new JButton("註冊");
    public static void main(String[] args)  {

       frame.setSize(500,500);
      // panel.setBackground(Color.YELLOW);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.add(panel);
       sovle();
       frame.setVisible(true);
   }
   public static void sovle(){
        lpw.setBounds(5,50,55,25);
        lname.setBounds(5,20,55,25);
        lemail.setBounds(5,80,55,25);
        lck.setBounds(5,110,55,25);
       ustext.setBounds(65,20,150,25);
       pw.setBounds(65,50,150,25);
       us2.setBounds(65,80,150,25);
       us3.setBounds(65,110,150,25);
       chag.setBounds(220,80,120,25);
       cof.setBounds(220,110,120,25);
      check2=ustext.getText();
       ustext.setEditable(true);
      // ustext.setFont(new Font("宋體",Font.BOLD,12));
        pw.setEchoChar('*');

        panel.add(us2);
        panel.add(us3);
        panel.add(lck);
        panel.add(lemail);
        panel.add(pw);
        panel.add(lname);
        panel.add(lpw);
       panel.setLayout(null);
       panel.add(ustext);
       Mylistener myl=new Mylistener();
       chag.addActionListener(myl);
       cof.addActionListener(myl);
       panel.add(chag);
       panel.add(cof);
       System.out.print(us2.getText());


   }
    static class Mylistener implements ActionListener{
        // @Override
        public void actionPerformed(ActionEvent e) {
            String x=e.getActionCommand();
            String str=us2.getText();
            // System.out.print(str);
            Pattern fa=Pattern.compile("^(\\w)+\\w+@[A-Za-z0-9]+\\.com$");
            Matcher son=fa.matcher(str);
            StringBuffer s1 = new StringBuffer();
            Random r=new Random();

            if(x=="獲取驗證碼"){
                for(int i=0;i<5;i++)
                {
                    int r1=r.nextInt(26);
                    //System.out.print(r1+" ");
                    s1.append((char)('a'+r1));
                }
                check=s1.toString();

                if(son.matches())
                JOptionPane.showMessageDialog(frame,s1,"驗證碼",JOptionPane.INFORMATION_MESSAGE);
                else
                    JOptionPane.showMessageDialog(frame,"郵箱錯誤","驗證碼",JOptionPane.WARNING_MESSAGE);
            }
            else{
                if(!check2.equals(ustext.getText())&&!check2.equals(us2.getText())&&!check2.equals(new String(pw.getPassword()))&&!check2.equals(us3.getText())){
                   // System.out.print(ustext.getText()+"   "+us3.getText());
                    if(check.equals(us3.getText()))
                        JOptionPane.showMessageDialog(frame,"註冊成功","註冊",JOptionPane.INFORMATION_MESSAGE);
                    else
                        JOptionPane.showMessageDialog(frame,"驗證碼錯誤","註冊",JOptionPane.WARNING_MESSAGE);

                }
                else{
                    JOptionPane.showMessageDialog(frame,"請填入完整信息","註冊",JOptionPane.WARNING_MESSAGE);
                }
            }

        }
    }

}


在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章