最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Java練習(xí)——實(shí)驗(yàn)15 圖形用戶(hù)界面設(shè)計(jì)(2)

2023-01-26 23:07 作者:沐笙--ms  | 我要投稿


1.實(shí)驗(yàn)?zāi)康?/strong>

掌握焦點(diǎn)、鼠標(biāo)租鍵盤(pán)事件。

2.實(shí)驗(yàn)介紹

任何組件上都可以發(fā)生鼠標(biāo)事件,如鼠標(biāo)迸人組件退出組件、在組件上方單擊鼠標(biāo)、拖動(dòng)鼠標(biāo)等都觸發(fā)鼠標(biāo)事件,即導(dǎo)致MouseEvent類(lèi)自動(dòng)創(chuàng)建一個(gè)事件對(duì)象。事件源注冊(cè)監(jiān)視器的方法是addMouseListener( MouseListenerlistener)。 當(dāng)某個(gè)組件處于激活狀態(tài)時(shí),如果用戶(hù)敲擊鍵盤(pán)上一個(gè)鍵就導(dǎo)致這個(gè)組件觸發(fā)KeyEvent 事件。使用KeyListener接口處理鍵盤(pán)事件。組件T以觸發(fā)焦點(diǎn)事件,當(dāng)組件具有焦點(diǎn)監(jiān)視器后,如果組件從尤輸人焦點(diǎn)變成有輸入焦點(diǎn)或從有輸入焦點(diǎn)變成無(wú)輸人焦點(diǎn)郡會(huì)觸發(fā)FOCuSEVent事件。使用FocusListener接口處理焦點(diǎn)盲件。

3.實(shí)驗(yàn)內(nèi)容

華容道是大家很熟悉的一個(gè)傳統(tǒng)智力游戲。編寫(xiě)GUI 程序,用戶(hù)通過(guò)鍵盤(pán)柯鼠標(biāo)事件來(lái)實(shí)現(xiàn)曹操、關(guān)羽等人物的移動(dòng)。

4.?實(shí)驗(yàn)步驟與代碼

Person.java

package text15;

?

import java.awt.Color;

import java.awt.Font;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import javax.swing.JButton;

?

public class Person extends JButton implements FocusListener {

int number;

Color c = new Color(255, 245, 170);

Font font = new Font("宋體", Font.BOLD, 12);

Person(int number, String s) {

super(s);

setBackground(c);

setFont(font);

this.number = number;

c = getBackground();

addFocusListener(this);

}

?

@Override

public void focusGained(FocusEvent arg0) {

// TODO Auto-generated method stub

setBackground(Color.red);

}

?

@Override

public void focusLost(FocusEvent arg0) {

// TODO Auto-generated method stub

setBackground(c);

}

}

MainClass.java

package text15;

?

import java.awt.Color;

import java.awt.Font;

import java.awt.event.FocusEvent;

import java.awt.event.FocusListener;

import javax.swing.JButton;

?

public class Person extends JButton implements FocusListener {

int number;

Color c = new Color(255, 245, 170);

Font font = new Font("宋體", Font.BOLD, 12);

Person(int number, String s) {

super(s);

setBackground(c);

setFont(font);

this.number = number;

c = getBackground();

addFocusListener(this);

}

?

@Override

public void focusGained(FocusEvent arg0) {

// TODO Auto-generated method stub

setBackground(Color.red);

}

?

@Override

public void focusLost(FocusEvent arg0) {

// TODO Auto-generated method stub

setBackground(c);

}

}

Hua_Rong_Road.java

package text15;

?

import java.awt.Color;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

?

import javax.swing.JButton;

import javax.swing.JFrame;

?

// ?,,

public class Hua_Rong_Road extends JFrame implements ActionListener,

MouseListener, KeyListener {

Person person[] = new Person[10];

JButton left, right, above, below;

JButton restart = new JButton("重新開(kāi)始");

?

public Hua_Rong_Road() {

init();

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

setBounds(100, 100, 320, 500);

setVisible(true);

validate();

}

?

public void init() {

setLayout(null);

add(restart);

restart.setBounds(100, 320, 120, 35);

restart.addActionListener(this);

String name[] = { "曹操", "關(guān)羽", "張", "劉", "周", "黃", "兵", "兵", "兵", "兵" };

for (int k = 0; k < name.length; k++) {

person[k] = new Person(k, name[k]);

person[k].addMouseListener(this);

person[k].addKeyListener(this);

add(person[k]);

}

person[0].setBounds(104, 54, 100, 100);

person[1].setBounds(104, 154, 100, 50);

person[2].setBounds(54, 154, 50, 100);

person[3].setBounds(204, 154, 50, 100);

person[4].setBounds(54, 54, 50, 100);

person[5].setBounds(204, 54, 50, 100);

person[6].setBounds(54, 254, 50, 50);

person[7].setBounds(204, 254, 50, 50);

person[8].setBounds(104, 204, 50, 50);

person[9].setBounds(154, 204, 50, 50);

person[9].requestFocus();

left = new JButton();// 四個(gè)邊界

right = new JButton();

above = new JButton();

below = new JButton();

add(left);

add(right);

add(above);

add(below);

left.setBounds(49, 49, 5, 260);

right.setBounds(254, 49, 5, 260);

above.setBounds(49, 49, 210, 5);

below.setBounds(49, 304, 210, 5);

validate();

}

?

@Override

public void keyPressed(KeyEvent e) {

Person man = (Person) e.getSource();

if (e.getKeyCode() == KeyEvent.VK_DOWN)

go(man, below);

if (e.getKeyCode() == KeyEvent.VK_UP)

go(man, above);

if (e.getKeyCode() == KeyEvent.VK_LEFT)

go(man, left);

if (e.getKeyCode() == KeyEvent.VK_RIGHT)

go(man, right);

}

?

public void keyReleased(KeyEvent e) {

}

?

public void keyTyped(KeyEvent e) {

}

?

public void mousePressed(MouseEvent e) {

Person man = (Person) e.getSource();

int x = -1, y = -1;

x = e.getX();

y = e.getY();

int w = man.getBounds().width;

int h = man.getBounds().height;

if (y > h / 2)

go(man, below);

if (y < h / 2)

go(man, above);

if (x < w / 2)

go(man, left);

if (x > w / 2)

go(man, right);

}

?

public void mouseClicked(MouseEvent e) {

}

?

public void mouseEntered(MouseEvent e) {

}

?

public void mouseExited(MouseEvent e) {

}

?

public void mouseReleased(MouseEvent e) {

}

?

public void go(Person man, JButton direction) {

boolean move = true;

Rectangle manRect = man.getBounds();

int x = man.getBounds().x;

int y = man.getBounds().y;

if (direction == below)

y = y + 50;

else if (direction == above)

y = y - 50;

else if (direction == left)

x = x - 50;

else if (direction == right)

x = x + 50;

manRect.setLocation(x, y);

Rectangle dirctionRect = direction.getBounds();

for (int k = 0; k < 10; k++) {

Rectangle personRect = person[k].getBounds();

if ((manRect.intersects(personRect)) && (man.number != k))

move = false;

}

if (manRect.intersects(dirctionRect))

move = false;

if (move == true)

man.setLocation(x, y);

}

?

public void actionPerformed(ActionEvent e) {

dispose();

new Hua_Rong_Road();

}

}

?

?

5.實(shí)驗(yàn)結(jié)果

?



Java練習(xí)——實(shí)驗(yàn)15 圖形用戶(hù)界面設(shè)計(jì)(2)的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
新绛县| 榆中县| 石泉县| 金门县| 宜宾县| 惠来县| 庆阳市| 巴中市| 蕲春县| 武陟县| 德清县| 宁夏| 乌兰浩特市| 广宁县| 大邑县| 建阳市| 大洼县| 关岭| 宁乡县| 东宁县| 页游| 通榆县| 上饶市| 新平| 嘉祥县| 鱼台县| 东港市| 察隅县| 喀喇沁旗| 楚雄市| 文安县| 昌吉市| 南丰县| 随州市| 南昌市| 平舆县| 兴业县| 沙河市| 马鞍山市| 衡东县| 杭锦旗|