java的gui的简单例子(java中gui设计)

本文目录一览:

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/n/232585.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-11 12:52
下一篇 2024-12-11 13:05

相关推荐

  • Java JsonPath 效率优化指南

    本篇文章将深入探讨Java JsonPath的效率问题,并提供一些优化方案。 一、JsonPath 简介 JsonPath是一个可用于从JSON数据中获取信息的库。它提供了一种DS…

    编程 2025-04-29
  • java client.getacsresponse 编译报错解决方法

    java client.getacsresponse 编译报错是Java编程过程中常见的错误,常见的原因是代码的语法错误、类库依赖问题和编译环境的配置问题。下面将从多个方面进行分析…

    编程 2025-04-29
  • Java Bean加载过程

    Java Bean加载过程涉及到类加载器、反射机制和Java虚拟机的执行过程。在本文中,将从这三个方面详细阐述Java Bean加载的过程。 一、类加载器 类加载器是Java虚拟机…

    编程 2025-04-29
  • Java腾讯云音视频对接

    本文旨在从多个方面详细阐述Java腾讯云音视频对接,提供完整的代码示例。 一、腾讯云音视频介绍 腾讯云音视频服务(Cloud Tencent Real-Time Communica…

    编程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介绍

    本文将详细介绍Java Milvus SearchParam withoutFields的相关知识和用法。 一、什么是Java Milvus SearchParam without…

    编程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java语言中的一个版本,于2014年3月18日发布。本文将从多个方面对Java 8中某一周的周一进行详细的阐述。 一、数组处理 Java 8新特性之一是Stream…

    编程 2025-04-29
  • Python简单数学计算

    本文将从多个方面介绍Python的简单数学计算,包括基础运算符、函数、库以及实际应用场景。 一、基础运算符 Python提供了基础的算术运算符,包括加(+)、减(-)、乘(*)、除…

    编程 2025-04-29
  • Java判断字符串是否存在多个

    本文将从以下几个方面详细阐述如何使用Java判断一个字符串中是否存在多个指定字符: 一、字符串遍历 字符串是Java编程中非常重要的一种数据类型。要判断字符串中是否存在多个指定字符…

    编程 2025-04-29
  • VSCode为什么无法运行Java

    解答:VSCode无法运行Java是因为默认情况下,VSCode并没有集成Java运行环境,需要手动添加Java运行环境或安装相关插件才能实现Java代码的编写、调试和运行。 一、…

    编程 2025-04-29
  • Python满天星代码:让编程变得更加简单

    本文将从多个方面详细阐述Python满天星代码,为大家介绍它的优点以及如何在编程中使用。无论是刚刚接触编程还是资深程序员,都能从中获得一定的收获。 一、简介 Python满天星代码…

    编程 2025-04-29

发表回复

登录后才能评论