「java中gui编程例子」如何理解尽管java支持gui编程,但其不是Java的强项
本篇文章给大家谈谈java中gui编程例子,以及如何理解尽管java支持gui编程,但其不是Java的强项对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、求java gui例子
- 2、编写一个Java GUI
- 3、JAVA 编写一个java图形GUI程序,比较大小数并输出
- 4、JAVA语言的GUI实例
- 5、编写一个java GUI程序(其实帮我改改)
- 6、java 用GUI写一个程序
求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等等.运行看效果.注释很全
编写一个Java GUI
试一下下面的代码
(如果点击按钮后没有任何变化,将窗口最小化一下就有了)
没有出现这个问题的话,也请告诉我一下~
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class painting extends JFrame implements ActionListener{
private JButton round,rectangle,ellipse,beeline;
private JLabel xaxis,yaxis,remain,information;
private JTextField xTF,yTF;
private BorderLayout layout;
private Container cp;
private JPanel pCenter;
VectorObject v=new VectorObject(); //定义一个集合类用于存储按钮对象
public painting(){ //构造方法 ------------------框架初始化-------------------
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("painting");
setSize(400,500);
layout = new BorderLayout();
cp = getContentPane();
cp.setLayout(layout);
round= new JButton("画圆");
rectangle= new JButton("画矩形");
ellipse= new JButton("画椭圆");
beeline= new JButton("画直线");
xaxis=new JLabel("x坐标");
yaxis=new JLabel("y坐标");
remain=new JLabel("右下角坐标(400,500) ");
xTF=new JTextField("0",5);
yTF=new JTextField("0",5);
JPanel pUp= new JPanel();//第一个面板 在上部
pUp.add(remain);
pUp.add(xaxis);//置两个文本框
pUp.add(xTF);
pUp.add(yaxis);
pUp.add(yTF);
cp.add(pUp, "North");
//pCenter=new JPanel();//第二个面板 在中部
//pCenter.add(information);//置显示说明与画图区
//cp.add(pCenter,"Center");
JPanel pDown= new JPanel();//第三个面板 在下部
pDown.add(round);// 置四个按钮
pDown.add(rectangle);
pDown.add(ellipse);
pDown.add(beeline);
cp.add(pDown, "South");
round.addActionListener(this); //置按钮监听--------------按钮行为监听与响应-------------
rectangle.addActionListener(this);
ellipse.addActionListener(this);
beeline.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {//监听响应
v.add(e.getSource());//将按钮情况存入v中
}
public void paint(Graphics g) { //--------------绘图响应-------------
super.paint(g);
int xx=Integer.parseInt(xTF.getText());//获取位置值
int yy=Integer.parseInt(yTF.getText());
int size=0;
Object o;
//while(v.size()!=size){//当用户点击按钮选择某一种图形时,v的大小就会比size值大1,当绘图完成后,v.size又等于size;效果就是:出现点击 即刻处理
o=v.lastElement();
if(o == round) {g.drawOval(xx,yy,50,50);}
else if (o == rectangle){g.drawRect(xx,yy,100,50);}
else if (o == ellipse) {g.drawOval(xx,yy,100,50);}
else if(o == beeline) {g.drawLine(xx,yy,xx+100,yy);}
size++;
}
}
public static void main(String[] args){ // ------------程序入口-------------
JFrame frame = new painting();
frame.setVisible(true);
}
}
JAVA 编写一个java图形GUI程序,比较大小数并输出
1.首先定义三个输入框
JTextField firstNumber;
JTextField secontdNumber;
JTextField maxNumber;
2.然后怎样生成并把它们放到界面上我就不多说了。
3.接下来生成最大数按钮maxNumberButton和关闭按钮closeButton,并放在界面上。
然后让maxnNumberButton监听鼠标单击事件
maxNumberButton.addMouseListener(new MouseAdapter() {
if (SwingUtilities.isLeftMouseButton(e)) {//判断是否鼠标左键按下
//在这里获得第一个和第二个数并比较获得最大的数,当然,你还可以首先判断是否已经输入了两个数,判断输入的两个值是否是数值等等的。你也可以在外边实现一个方法,直接返回最大值。然后是把最大值显示出来。
//从输入框直接获得的是string,你要自己转换成数值,具体怎样做我就不说了。默认你已经获得了两个值,并得到最大值max,顺便说一下float和double是不能直接大于小于这样比较的。
maxNumber.setText("");//首先把之前显示的清除
maxNumber.setText(max);
}
});
关闭按钮处理类似
closeButton.addMouseListener(new MouseAdapter() {
if (SwingUtilities.isLeftMouseButton(e)) {
System.exit(0);
}
});
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程序(其实帮我改改)
把frame=new subJFrame("DrawShapes");改成frame=new JFrame("DrawShapes");
程序基本没问题,在public void paint(Graphics g)中加上如下程序就可以了。
public void paint(Graphics g){
switch(i){
case 1: g.drawOval(20,20,40,40);break;
case 2: g.drawRect(20,20,40,40);break;
case 3: g.drawOval(20,30,40,50);break;
case 4: g.drawLine(20,20,40,40);break;
}
}
java 用GUI写一个程序
使用Font类
下边是例子
---------------------------------------------------------------------------------------------
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class FontApp extends JFrame {
public FontApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("a String size 18");
lblNewLabel.setFont(new Font(null, Font.ITALIC, 18));
lblNewLabel.setBounds(12, 30, 232, 29);
getContentPane().add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("b String size 14");
lblNewLabel_1.setFont(new Font(null, Font.BOLD, 14));
lblNewLabel_1.setBounds(12, 97, 232, 29);
getContentPane().add(lblNewLabel_1);
setVisible(true);
}
public static void main(String[] args) {
new FontApp();
}
}
关于java中gui编程例子和如何理解尽管java支持gui编程,但其不是Java的强项的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。