組件java
窗口數據結構
架構
面板dom
文本框ide
列表框函數
按鈕工具
圖片佈局
監聽事件測試
鼠標優化
鍵盤事
破解工具
GUI的核心技術:Swing AWT 界面不美觀 須要jre環境
爲了瞭解MVC架構 瞭解監聽。
包含不少類和接口
元素:窗口,按鈕,文本框
java.awt
是一個頂級窗口
import java.awt.*;
//GUI第一個界面
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("個人第一個界面");
//須要設置可見性
frame.setVisible(true);
//設置窗口大小
frame.setSize(400,400);
//設置背景顏色
frame.setBackground(new Color(22,44,66));
//設置窗口初始位置
frame.setLocation(200,200);
//設置大小固定
frame.setResizable(false);
}
}
嘗試回顧封裝
import java.awt.*;
public class TestFrame1 {
public static void main(String[] args) {
MyFrame m1 = new MyFrame(100,100,200,200,Color.BLACK);
MyFrame m2 = new MyFrame(300,100,200,200,Color.red);
MyFrame m3 = new MyFrame(100,300,200,200,Color.yellow);
MyFrame m4 = new MyFrame(300,300,200,200,Color.BLUE);
}
}
class MyFrame extends Frame{
static int id = 0; //可能須要多個窗口,須要一個計數器
public MyFrame(int x,int y,int w,int h,Color color){
super("Myframe"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}
Panel沒法單獨顯示,必須添加到某個容器中
解決了關閉事件
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
//設置佈局
frame.setLayout(null);
//座標和顏色
frame.setBounds(400,400,500,500);
frame.setBackground(new Color(13, 24, 56));
//panel設置座標,至關於frame
panel.setBounds(100,100,300,300);
panel.setBackground(new Color(144, 11, 44));
//frame.add();
frame.add(panel);
//設置可見性
frame.setVisible(true);
//監聽事件,監聽窗口關閉事件 System.exit(0);
//適配器模式
frame.addWindowListener(new WindowAdapter() {
流式佈局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestButton {
public static void main(String[] args) {
Frame frame = new Frame();
//組件-按鈕
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
//設置爲流式佈局
//frame.setLayout(new FlowLayout());
//frame.setLayout(new FlowLayout(FlowLayout.LEFT) );
frame.setLayout(new FlowLayout(FlowLayout.RIGHT) );
frame.setVisible(true);
frame.setBounds(200,200,400,400);
//把按鈕添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.addWindowListener(new WindowAdapter() {
東西南北中
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button east = new Button("East");
Button west = new Button("West");
Button south = new Button("South");
Button north = new Button("North");
Button center = new Button("Center");
frame.add(east,BorderLayout.EAST);
frame.add(west,BorderLayout.WEST);
frame.add(south,BorderLayout.SOUTH);
frame.add(north,BorderLayout.NORTH);
frame.add(center,BorderLayout.CENTER);
frame.setSize(400,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
表格佈局
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame();
Button btn1 = new Button("btn1");
Button btn2 = new Button("btn2");
Button btn3 = new Button("btn3");
Button btn4 = new Button("btn4");
Button btn5 = new Button("btn5");
Button btn6 = new Button("btn6");
frame.setLayout(new GridLayout(3,2));
frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);
frame.pack();//java函數
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(400,400);
frame.setLocation(200,200);
frame.setBackground(new Color(1,4,6));
frame.setLayout(new GridLayout(2,1));
//new四個面板
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
p1.add(new Button("east-1"),BorderLayout.EAST);
p1.add(new Button("west-1"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("east-2"),BorderLayout.EAST);
p3.add(new Button("west-2"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
Button button = new Button();
//由於addActionListener()須要一個ActionListener,因此咱們須要構造一個ActionListener
MyActionListener ac = new MyActionListener();
button.addActionListener(ac);
windowClose(frame);//關閉窗口
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
//關閉窗體事件
public static void windowClose(Frame frame){
frame.addWindowListener(new WindowAdapter() {
多個按鈕實現同一監聽事件
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;public class TestActionEvent2 {
public static void main(String[] args) {
//兩個按鈕實現同一監聽
Frame frame = new Frame("開始-中止");
Button b1 = new Button("start");
Button b2 = new Button("stop");}
MyActionListener1 ac = new MyActionListener1(); b1.addActionListener(ac); b2.addActionListener(ac); //能夠顯示的定義觸發會返回的命令,不顯示走默認的值 //多個按鈕使用一個監聽類 b2.setActionCommand("b2-->stop"); frame.add(b1,BorderLayout.NORTH); frame.add(b2,BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); }
class MyActionListener1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//e.getActionCommand()得到按鈕信息
System.out.println("按鈕被點擊了-->"+e.getActionCommand());
}
}
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestText01 {
public static void main(String[] args) {
new MyFrame();
//啓動
}
}
class MyFrame extends Frame{
public MyFrame(){
TextField textField = new TextField();
add(textField);
//監聽這個文本框輸入的文字
MyActionListener03 mal = new MyActionListener03();
//按下enter,就會觸發文本框事件
textField.addActionListener(mal);
//設置替換編碼
textField.setEchoChar('*');
setVisible(true);
pack();
}
}
class MyActionListener03 implements ActionListener{
優化前
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
new MyCalculator();
}
}
//計算器類
class MyCalculator extends Frame{
public MyCalculator() {
//3個文本框
TextField text1 = new TextField(10);
TextField text2 = new TextField(10);
TextField text3 = new TextField(20);
//1個按鈕
Button button = new Button("=");
//點擊事件
button.addActionListener(new MyCalculatorListener(text1,text2,text3));
//1個標籤
Label label = new Label("+");
//佈局
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
}
}
//監聽事件類
class MyCalculatorListener implements ActionListener{
private TextField text1,text2,text3;
//獲取三個變量
public MyCalculatorListener(TextField text1, TextField text2, TextField text3) {
this.text1 = text1;
this.text2 = text2;
this.text3 = text3;
}
優化爲面向對象
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
MyCalculator myCalculator = new MyCalculator();
myCalculator.loadFrame();
}
}
//計算器類
class MyCalculator extends Frame{
//屬性
TextField text1,text2,text3;
//方法
public void loadFrame(){
//3個文本框
text1 = new TextField(10);//字符數
text2 = new TextField(10);
text3 = new TextField(20);
//1個按鈕
Button button = new Button("=");
//1個標籤
Label label = new Label("+");
//點擊事件
button.addActionListener(new MyCalculatorListener(this));
//佈局
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
}
}
//監聽事件類
class MyCalculatorListener implements ActionListener{
//獲取計算器這個對象,在一個類中組合另外一個類
MyCalculator calculator=null;
public MyCalculatorListener(MyCalculator calculator) {
this.calculator=calculator;
}
內部類好處是能夠更好的訪問外部類的方法和屬性
更好的包裝
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestCalc {
public static void main(String[] args) {
MyCalculator myCalculator = new MyCalculator();
myCalculator.loadFrame();
}
}
//計算器類
class MyCalculator extends Frame{
//屬性
TextField text1,text2,text3;
//方法
public void loadFrame(){
//3個文本框
text1 = new TextField(10);//字符數
text2 = new TextField(10);
text3 = new TextField(20);
//1個按鈕
Button button = new Button("=");
//1個標籤
Label label = new Label("+");
//點擊事件
button.addActionListener(new MyCalculatorListener());
//佈局
setLayout(new FlowLayout());
add(text1);
add(label);
add(text2);
add(button);
add(text3);
setVisible(true);
pack();
}
//監聽事件類
//內部類
private class MyCalculatorListener implements ActionListener{
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,300,400,500);
setVisible(true);
}
//畫筆
實現畫點
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
//測試鼠標監聽事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("畫圖");
}
}
class MyFrame extends Frame{
//畫畫須要畫筆須要監聽鼠標當前的位置,須要集合存儲這個點
ArrayList points;
public MyFrame(String title) {
super(title);
setBounds(200,200,300,500);
//存鼠標點擊的點
points=new ArrayList<>();
//鼠標監聽器,是針對這個窗口
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new MyWindow();
}
}
class MyWindow extends Frame{
public MyWindow() {
setVisible(true);
setBounds(100,200,300,400);
setBackground(Color.BLUE);
//addWindowListener(new MyWindowListener());
this.addWindowListener(
//匿名內部類
new WindowAdapter() {
//關閉窗口
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
System.exit(0);
}
//激活窗口
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
});
}
}
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new keyFrame();
}
}
class keyFrame extends Frame{
public keyFrame() {
setBounds(1,2,100,200);
setVisible(true);
addKeyListener(new KeyAdapter() {
import javax.swing.*;
import java.awt.*;
public class TestJFrame {
//init()初始化
public void init(){
JFrame jframe = new JFrame("第一個JFrame窗口");
jframe.setVisible(true);
jframe.setBackground(Color.BLUE);
jframe.setBounds(100,200,200,200);
//設置文字JLabel
JLabel jLabel = new JLabel("歡迎光臨");
jframe.add(jLabel);
//關閉事件
jframe.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
//創建一個窗口
new TestJFrame().init();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class TestDialog extends JFrame {
public TestDialog() {
this.setVisible(true);
this.setBounds(200,300,400,500);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//容器
Container container = this.getContentPane();
//絕對佈局
container.setLayout(null);
//按鈕
JButton button = new JButton("點擊彈出一個對話框");//建立
button.setBounds(30,20,100,50);
//點擊按鈕的時候,彈出一個彈窗
button.addActionListener(new ActionListener() {
圖標Icon
import javax.swing.*;
import java.awt.*;
//圖標,須要實現類,Frame繼承
public class TestIcon extends JFrame implements Icon {
private int height;
private int width;
public TestIcon(){//無參構造
}
public TestIcon(int height,int width){//有參構造
this.height=height;
this.width=width;
}
public void init(){
TestIcon testIcon = new TestIcon(100,100);
//圖標放在標籤上,也能夠放在按鈕上
JLabel label = new JLabel("testIcon", testIcon, SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
圖片Image
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame{
public ImageIconDemo() {
//獲取圖片的地址
JLabel label = new JLabel("圖片");
URL url = ImageIconDemo.class.getResource("1.jpg");
ImageIcon imageIcon = new ImageIcon(url);
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.add(label);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100,200,50,50);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
JPanel
import javax.swing.*;
import java.awt.*;
public class TestJPanel extends JFrame {
public TestJPanel() {
Container container = this.getContentPane();
container.setLayout(new GridLayout(2,1,10,10));//後面參數意思是間距
JPanel jPanel = new JPanel(new GridLayout(2,2,5,5));
jPanel.add(new Button("1"));
jPanel.add(new Button("1"));
jPanel.add(new Button("1"));
jPanel.add(new Button("1"));
container.add(jPanel);
this.setVisible(true);
this.setSize(200,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestJPanel();
}
}
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll() {
TextArea textArea = new TextArea("歡迎光臨");//文本域
Container container = this.getContentPane();
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,150,250);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
圖片按鈕
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJButton extends JFrame {
public TestJButton() {
Container container = this.getContentPane();
//將圖片變爲一個圖標
URL resource = TestJButton.class.getResource("1.jpg");
Icon icon = new ImageIcon(resource);
//把圖標放在按鈕上
JButton jButton = new JButton();
jButton.setIcon(icon);
jButton.setToolTipText("圖片按鈕");
container.add(jButton);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJButton();
}
}
單選按鈕
import javax.swing.*;
import java.awt.*;
public class TestJButton1 extends JFrame {
public TestJButton1() {
Container container = this.getContentPane();
//單選框
JRadioButton radioButton1 = new JRadioButton("radioButton1");
JRadioButton radioButton2 = new JRadioButton("radioButton2");
JRadioButton radioButton3 = new JRadioButton("radioButton3");
//因爲單選框只能選一個,分組
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);
group.add(radioButton3);
container.add(radioButton1,BorderLayout.NORTH);
container.add(radioButton2,BorderLayout.CENTER);
container.add(radioButton3,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJButton1();
}
}
複選按鈕
import javax.swing.*;
import java.awt.*;
public class TestJButton2 extends JFrame {
public TestJButton2() {
Container container = this.getContentPane();
//複選框
Checkbox checkbox1 = new Checkbox("checkbox1");
Checkbox checkbox2 = new Checkbox("checkbox1");
container.add(checkbox1,BorderLayout.NORTH);
container.add(checkbox2,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(100,100,200,200);
}
public static void main(String[] args) {
new TestJButton2();
}
}
下拉框
import javax.swing.*;
import java.awt.*;
public class TestComboBox extends JFrame {
public TestComboBox() {
Container container = this.getContentPane();
JComboBox comboBox = new JComboBox();
comboBox.addItem(null);
comboBox.addItem("猴子");
comboBox.addItem("大象");
comboBox.addItem("長頸鹿");
container.add(comboBox);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBox();
}
}
列表框
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class TestComboBox1 extends JFrame {
public TestComboBox1() {
Container container = this.getContentPane();
//生成列表的內容
//String [] strings = {"1","2","3"};
//列表放入的內容
Vector vector = new Vector();
JList jList = new JList(vector);
vector.add("yier");
vector.add("sas");
container.add(jList);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestComboBox1();
}
}
應用場景:選擇地區,或者一些單選項
列表:展現信息,通常是動態擴容。
文本框
import javax.swing.*;
import java.awt.*;
public class Test01 extends JFrame {
public Test01() {
Container container = this.getContentPane();
TextField textField = new TextField("heheh",10);
container.add(textField);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Test01();
}
}
密碼框
import javax.swing.*;
import java.awt.*;
public class Test01 extends JFrame {
public Test01() {
Container container = this.getContentPane();
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
container.add(passwordField);
this.setVisible(true);
this.setSize(300,100);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new Test01();
}
}
文本域
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public TestJScroll() {
TextArea textArea = new TextArea("歡迎光臨");//文本域
Container container = this.getContentPane();
//scroll面板
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
this.setVisible(true);
this.setBounds(100,100,150,250);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new TestJScroll();
}
}
幀 30,60幀,拆開細分就是一些靜態得圖片
須要知識;鍵盤監聽,定時器Timer
import javax.swing.*;
//遊戲主啓動類
public class StartGame {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(100,100,900,730);
frame.setResizable(false);//窗口大小不可變
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//正常遊戲面板都在這
frame.add(new GamePanel());
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
//遊戲得面板
public class GamePanel extends JPanel implements KeyListener, ActionListener {
//定義蛇的數據結構
int length;//蛇的長度
int[] snakeX = new int[600];//蛇的X座標 25*25
int[] snakeY = new int[500];//蛇的Y座標 25*25
String fx;
//食物的座標
int foodx;
int foody;
int score;//fens
Random random= new Random();
//遊戲當前的狀態,中止-->開始
boolean isStart = false;//默認中止
boolean isFail = false;//遊戲失敗狀態
//定時器 以毫秒爲單位 1000ms=1s
Timer timer = new Timer(100,this);//100毫秒執行一次
//構造器
public GamePanel() {
init();
//得到焦點和鍵盤事件
this.setFocusable(true);//得到焦點事件
this.addKeyListener(this);//得到鍵盤監聽事件
}
//初始化方法
public void init(){
length=3;
snakeX[0] =100;snakeY[0]= 100;//頭部的座標
snakeX[1] =75;snakeY[1] = 100;//第一個身體座標
snakeX[2] =50;snakeY[2] = 100;//第二個身體座標
fx="R";//初始化方向向右
//把食物隨機分配在界面上
foodx=25+25*random.nextInt(34);
foody=75+25*random.nextInt(24);
timer.start();//遊戲一開始定時器就啓動
score=0;
}
//繪製面板,咱們遊戲中因此得東西都是用這個畫筆畫