本文目錄一覽:
JAVA語言的GUI實例
GUI做的計算器,可以查考一下,組件、事件等
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame{
private Container container;//框架
private JTextField tf;//定義文本框
private Panel panel;
private String cmd;
private double result;//運算結果
private boolean start;//運算開始判斷
Calculator(){
super(“計算器”);
container = getContentPane();
container.setLayout(new BorderLayout());
//添加文本框
tf = new JTextField(“0.0”);
container.add(tf,BorderLayout.NORTH);
tf.setHorizontalAlignment(JTextField.RIGHT);
tf.setEditable(false);
//嵌套容器
panel = new Panel();
container.add(panel);
start = true;
result = 0;
//最後運算等號
cmd = “=”;
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
//調用創建Button方法
addButton(“1”,insert);
addButton(“2”,insert);
addButton(“3”,insert);
addButton(“0”,insert);
addButton(“*”,command);
addButton(“Back”,insert);
addButton(“4”,insert);
addButton(“5”,insert);
addButton(“6”,insert);
addButton(“+”,command);
addButton(“/”,command);
addButton(“Clear”,insert);
addButton(“7”,insert);
addButton(“8”,insert);
addButton(“9”,insert);
addButton(“-“,command);
addButton(“.”,insert);
addButton(“=”,command);
setSize(400,200);
}
private void addButton(String str,ActionListener listener){
//添加Button方法(操作類型、註冊監聽器)
JButton button=new JButton(str);
button.addActionListener(listener);
panel.setLayout(new GridLayout(3,6));
panel.add(button);
}
private class InsertAction implements ActionListener{
//插入,insert
public void actionPerformed(ActionEvent event){
String input=event.getActionCommand();
if (start)
{
tf.setText(“”);
start=false;
}
if(input.equals(“Back”))
{
String str=tf.getText();
if(str.length()0)
tf.setText(str.substring(0,str.length()-1));
}
else if(input.equals(“Clear”))
{
tf.setText(“0”);
start=true;
}
else
tf.setText(tf.getText()+input);
}
}
private class CommandAction implements ActionListener{
//計算,command
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
if(start)
{
cmd=command;
}
else
{
calculate(Double.parseDouble(tf.getText()));
cmd=command;
start=true;
}
}
}
public void calculate(double x){
//加減乘除運算
if (cmd.equals(“+”)) result+=x;
else if (cmd.equals(“-“)) result-=x;
else if (cmd.equals(“*”)) result*=x;
else if (cmd.equals(“/”)) result/=x;
else if (cmd.equals(“=”)) result= x;
tf.setText(“”+ result);
}
public static void main(String []args){
Calculator mycalculator=new Calculator();
mycalculator.setLocation(300,300);
mycalculator.setVisible(true);
}
}
求java gui例子
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
class mine implements MouseListener, ActionListener, ItemListener, Runnable {
HashMap imgmap; //用來存放初始化之後的圖片分布
String lastsrc = “”; //最後一次點擊的圖片
int rightcount = 1 //連續點擊對了的次數
,
wincount = 3; //勝利條件,即正確多少次就能贏
JButton b_mine[][]; //就是那些按紐
JButton b_ls; //一個臨時變數,用來找出點的哪個的下標
JPanel p1 //放菜單、時間等等
,
p2; //放圖片按牛
JLabel l2; //用來存放遊戲時間
JFrame f;
Thread thread1; //用來計算遊戲時間的線程
MenuItem item10 //關於
,
item11; //why
MenuBar mb; //菜單
Menu m_games //遊戲菜單
,
m_guanyu; //關於菜單
CheckboxMenuItem item2 //初級菜單
,
item3 //中級菜單
,
item4; //高級菜單
Container con;
int x
,
y
,
n; //時間計數
Font font1;
int icon_size = 80 //按牛尺寸
,
Difficulty = 1 //難度
,
Width = 5 //橫向幾個按牛
,
Height = 5 //縱向幾個按牛
,
Xpos = 300
,
Ypos = 100;
Dialog a; //彈出框
public mine() {
f = new JFrame(“記憶力”);
con = f.getContentPane();
con.setLayout(null);
menuCreate(); //生成菜單
p1 = new JPanel();
p1.setLayout(null);
p2 = new JPanel();
l2 = new JLabel(“”, Label.RIGHT);
font1 = new Font(“Helvetica”, Font.BOLD, 20);
l2.setFont(font1);
Border border2 = new EtchedBorder(EtchedBorder.RAISED, new Color(144, 80, 44), new Color(144, 80, 44));
l2.setBorder(BorderFactory.createLoweredBevelBorder());
l2.setBackground(new Color(100, 50, 100));
l2.setForeground(Color.red);
l2.setOpaque(true);
p2.setBorder(border2);
p1.add(l2); //把計數器放上
con.add(p1);
con.add(p2);
f.setLocation(Xpos, Ypos);
layout_init(Width, Height, icon_size); //初始化布局
button_init(Width, Height, 2); //初始化按牛
f.setVisible(true); //顯示界面
f.setResizable(false); //程序不可改變大小
f.addWindowListener(new WindowAdapter() //關閉窗口
{
public void windowClosing(WindowEvent e) {
f.setVisible(false);
System.exit(0);
}
});
try {
Thread.sleep(3000);
}
catch (Exception e) {
}
}
//****************窗口添加控制項和控制項初始化**************************//\
public void layout_init(int width, int height, int icon_size) //生成最初的版面
{
this.icon_size = icon_size;
l2.setBounds(width * icon_size – 42, 3, 40, 25);
p1.setBounds(0, 0, width * icon_size + 4, 30);
p2.setBounds(0, 32, width * icon_size + 4, height * icon_size + 4);
p2.setLayout(new GridLayout(height, width));
f.setSize(width * icon_size + 10, height * icon_size + 100);
}
public void button_init(int width, int height, int typecount) //添加按鈕
{
l2.setText(“000”);
imgmap = new HashMap();
wincount = width – 2;
this.Width = width;
this.Height = height;
b_mine = new JButton[height][width];
for (int i = 0; i height; i++)
for (int j = 0; j width; j++) {
b_mine[i][j] = new JButton();
p2.add(b_mine[i][j]);
b_mine[i][j].addMouseListener(this); //給按牛加滑鼠監聽
imgmap.put(i + “_” + j, (int) (Math.random() * typecount) + “.gif”); //初始化圖片,隨機放在按牛上
}
}
public void button_remove(int width, int height) //清除按扭
{
for (int i = 0; i height; i++)
for (int j = 0; j width; j++) {
b_mine[i][j].removeMouseListener(this);
p2.remove(b_mine[i][j]);
}
}
public void itemStateChanged(ItemEvent e) { //級別變化會調用這個
if (e.getSource() == item2) {
if (thread1 != null) {
thread1.yield();
thread1 = null;
}
item2.setState(true);
item3.setState(false);
item4.setState(false);
Difficulty = 1;
button_remove(Width, Height);
button_init(5, 5, 2);
layout_init(5, 5, icon_size);
f.setVisible(true);
}
if (e.getSource() == item3) {
if (thread1 != null) {
thread1.yield();
thread1 = null;
}
item2.setState(false);
item3.setState(true);
item4.setState(false);
Difficulty = 2;
button_remove(Width, Height);
button_init(6, 6, 3);
layout_init(6, 6, icon_size);
f.setVisible(true);
}
if (e.getSource() == item4) {
if (thread1 != null) {
thread1.yield();
thread1 = null;
}
item2.setState(false);
item3.setState(false);
item4.setState(true);
Difficulty = 3;
button_remove(Width, Height);
button_init(7, 7, 4);
layout_init(7, 7, icon_size);
f.setVisible(true);
}
layout_init(Width, Height, icon_size);
icon_size = 80;
f.setVisible(true);
}
public void mousePressed(MouseEvent e) {
if (thread1 == null) {
thread1 = new Thread(this);
thread1.start();
}
//***************找出所點擊的按鈕的兩個下標值*****************//
b_ls = (JButton) e.getSource();
for (int i = 0; i Height; i++)
for (int j = 0; j Width; j++) {
if (b_mine[i][j] == e.getSource()) {
x = j;
y = i;
break;
}
}
}
public void mouseReleased(MouseEvent e) {
if (e.getModifiers() == 16) //左鍵
{
String thissrc = (String) imgmap.get(y + “_” + x); //這次點的圖片
b_mine[y][x].setIcon(new ImageIcon(“D:\\java\\source\\baidujava\\classes\\” + thissrc));
if (!thissrc .equals(lastsrc)) { //點錯了
lastsrc = thissrc;
for (int ti = 0; ti Width; ti++) {
for (int tj = 0; tj Height; tj++) {
if (ti == y tj == x) continue;
b_mine[ti][tj].setIcon(null);
}
}
rightcount = 1;
} else { //點對了
rightcount++;
}
if (rightcount == wincount) { //點對的次數如果等於勝利條件
JLabel t = new JLabel();
t.setText(“ok,You win.用了” + l2.getText() + “秒”);
thread1 = null;
button_remove(Width, Height);
button_init(Width, Height, Width – 2);
layout_init(Width, Width, icon_size);
JOptionPane.showConfirmDialog(f, t, “你贏”, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
}
}
public void moveMouseListener() { //換級別的時候去掉圖片按牛的監聽
for (int i = 0; i Height; i++)
for (int j = 0; j Width; j++)
b_mine[i][j].removeMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
//*****************菜單定義部分********************//
public void menuCreate() {
mb = new MenuBar();
m_games = new Menu(“遊戲”);
m_guanyu = new Menu(“關於”);
item2 = new CheckboxMenuItem(“初級”);
item3 = new CheckboxMenuItem(“中級”);
item4 = new CheckboxMenuItem(“高級”);
item10 = new MenuItem(“關於…”);
item10.addActionListener(this);
item11 = new MenuItem(“why…”);
item11.addActionListener(this);
m_games.add(item2);
m_games.add(item3);
m_games.add(item4);
m_guanyu.add(item10);
m_guanyu.add(item11);
mb.add(m_games);
mb.add(m_guanyu);
f.setMenuBar(mb);
if (Difficulty == 1)
item2.setState(true);
else if (Difficulty == 2)
item3.setState(true);
else if (Difficulty == 3)
item4.setState(true);
item2.addItemListener(this);
item3.addItemListener(this);
item4.addItemListener(this);
}
public void run() { //計時器
n = 0;
String s;
while (thread1 != null) {
if (n 10)
s = “00”;
else if (n 100)
s = “0”;
else
s = “”;
l2.setText(s + String.valueOf(++n));
try {
thread1.sleep(1000);
}
catch (Exception e) {
}
}
}
public void actionPerformed(ActionEvent e) {
JLabel t = new JLabel();
if (e.getSource() == item11) {
t.setText(“這是why”);
JOptionPane.showConfirmDialog(f, t, “你贏”, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}else if (e.getSource() == item10) {
t.setText(“這是關於”);
JOptionPane.showConfirmDialog(f, t, “你贏”, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
}
}
public class clearmine {
public static void main(String args[]) {
new mine();
}
}
一個翻圖遊戲的. 把gif圖片放到同一目錄下.命名為1.gif,2.gif等等.運行看效果.注釋很全
簡單的GUI編程java編寫
/**
* 這是一個可運行的程序,直接把代碼複製到eclipse中即可. 還有問題留言。
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class FrameDemo extends JFrame{
//定義需要的組件.
private JPanel panel;//面板組件》
private JTextField tf_input;//供用戶進行輸入的文本框》
private JLabel lb_num;//統計字元個數.
private JButton totalButton;//統計字元個數按鈕.
/**
* 無參構造方法.
* 作用: 初始化組件.
*/
public FrameDemo() {
//初始化組件.
panel = new JPanel();
tf_input = new JTextField(18);
lb_num = new JLabel();
totalButton = new JButton(“統計”);
//將組件添加到panel中.
panel.add(tf_input);
panel.add(lb_num);
panel.add(totalButton);
//設置窗口屬性》
add(panel);//添加panel
setVisible(true);//設置可見》
setSize(300,300);//設置大小。
setLocationRelativeTo(null);//居中.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設置關閉動作.
//給按鈕添加點擊監聽。
totalButton.addActionListener(new ActionListener() {//匿名內部類.
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String input = tf_input.getText();//獲取用戶輸入.
int num = input.length();//計算長度》
String numStr = num+””;//將int —- String,因為JLabel中只能接收String類型的》
lb_num.setText(numStr);//設置到JLabel中進行顯示》
}
});
}
/**
* 啟動程序
* @param args
*/
public static void main(String[] args) {
new FrameDemo();
}
}
java中GUI是什麼意思啊解釋一下
常常用Controler來表示,一個類.
一般初學的時候,都是在一個監聽介面的方法實現中對某另一個類進行操作.
比如一點地址欄的回車,下面的狀態欄會顯示IP地址等.
按照面向對象的發消息機制,有控制模塊的程序不再是直接對其他類進行操作,而是在觸發事件的時候向控制模塊發消息,由控制模塊啟動相應類的進程,向其發消息,以改變其狀態.這樣一來各個組件之間不必互相認識,只要通過控制中心聯繫就可以了.
例如上面那個例子,點完回車後,地址欄的向控制器發一個消息,控制器啟動響應的狀態欄對象的進程,再向其發送一個消息,使其調用自身的改變狀態方法.
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/232585.html