java GUI 例子有错!求指正!

java GUI 例子有错!求指正!,第1张

很简单的问题

你的

public class SimpleGUI implements ActionListener{

JButton button;

中的button光声明没有分配空间

所以报NullPointerException错误

为什么呢?

public void go(){

JFrame frame=new JFrame();

JButton button =new JButton("click me");

因为你的这里又JButton button声明了一个button变量,并且这个变量被默认为方法内部的私有变量

你framegetContentPane()add(BorderLayoutSOUTH,button);这里的button

和你 buttonsetText("Yes!");里面的button不是一回事儿,你后面的这个button没有分配空间

只需要将你的

public void go(){

JFrame frame=new JFrame();

JButton button =new JButton("click me");

buttonaddActionListener(this);

改为

public void go(){

JFrame frame=new JFrame();

button =new JButton("click me");

buttonaddActionListener(this);

就可以了,这样就会对你

public class SimpleGUI implements ActionListener{

JButton button;

中的这个全局变量初始化

function CalendarTable;

% calendar 日历

% Example:

% CalendarTable;

S=datestr(now);

[y,m,d]=datevec(S);

% d is day

% m is month

% y is year

DD={'Sun','Mon','Tue','Wed','Thu','Fri','Sat'};

close all

figure;

for k=1:7;

uicontrol(gcf,'style','text',

'unit','normalized','position',[002+k01,055,008,006],

'BackgroundColor',06[1,1,1],'ForegroundColor','b',

'String',DD(k),'fontsize',16,'fontname','times new roman');

end

h=1;

ss='b';

qq=eomday(y,m);

for k=1:qq;

n=datenum(y,m,k);

[da,w] = weekday(n);

if k==d;

ss='r';

end

uicontrol(gcf,'style','push',

'unit','normalized','position',[002+da01,055-h008,008,006],

'BackgroundColor',06[1,1,1],'ForegroundColor',ss,

'String',num2str(k));

ss='b';

if da==7;

h=h+1;

end

end

uicontrol(gcf,'style','push',

'unit','normalized','position',[06,066,012,008],

'BackgroundColor',06[1,1,1],'ForegroundColor',ss,

'String','clock','fontsize',18,'fontname','times new roman');

Tq=uicontrol(gcf,'style','push',

'unit','normalized','position',[074,066,017,008],

'BackgroundColor',06[1,1,1],'ForegroundColor',[01,09,09],

'fontsize',18,'fontname','times new roman');

sq='The calendar';

uicontrol(gcf,'style','push',

'unit','normalized','position',[014,086,037,008],

'BackgroundColor',06[1,1,1],'ForegroundColor',[01,09,09],

'fontsize',18,'fontname','times new roman','string',sq);

try

while 1

set(Tq,'String',datestr(now,13));

pause(1);

end

end

作为一个初学gui图形界面的学生,感觉这个台历太强大了!哈哈。。。恩,还点谦虚的去学呀,好多东东要去钻研呀。。。!!

import javaxswing;

import javaxswingborderBorder;

import javaxswingborderEtchedBorder;

import javaawt;

import javaawtevent;

import javautilHashMap;

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; //d出框

public mine() {

f = new JFrame("记忆力");

con = fgetContentPane();

consetLayout(null);

menuCreate(); //生成菜单

p1 = new JPanel();

p1setLayout(null);

p2 = new JPanel();

l2 = new JLabel("", LabelRIGHT);

font1 = new Font("Helvetica", FontBOLD, 20);

l2setFont(font1);

Border border2 = new EtchedBorder(EtchedBorderRAISED, new Color(144, 80, 44), new Color(144, 80, 44));

l2setBorder(BorderFactorycreateLoweredBevelBorder());

l2setBackground(new Color(100, 50, 100));

l2setForeground(Colorred);

l2setOpaque(true);

p2setBorder(border2);

p1add(l2); //把计数器放上

conadd(p1);

conadd(p2);

fsetLocation(Xpos, Ypos);

layout_init(Width, Height, icon_size); //初始化布局

button_init(Width, Height, 2); //初始化按牛

fsetVisible(true); //显示界面

fsetResizable(false); //程序不可改变大小

faddWindowListener(new WindowAdapter() //关闭窗口

{

public void windowClosing(WindowEvent e) {

fsetVisible(false);

Systemexit(0);

}

});

try {

Threadsleep(3000);

}

catch (Exception e) {

}

}

//窗口添加控件和控件初始化//\

public void layout_init(int width, int height, int icon_size) //生成最初的版面

{

thisicon_size = icon_size;

l2setBounds(width icon_size - 42, 3, 40, 25);

p1setBounds(0, 0, width icon_size + 4, 30);

p2setBounds(0, 32, width icon_size + 4, height icon_size + 4);

p2setLayout(new GridLayout(height, width));

fsetSize(width icon_size + 10, height icon_size + 100);

}

public void button_init(int width, int height, int typecount) //添加按钮

{

l2setText("000");

imgmap = new HashMap();

wincount = width - 2;

thisWidth = width;

thisHeight = 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();

p2add(b_mine[i][j]);

b_mine[i][j]addMouseListener(this); //给按牛加鼠标监听

imgmapput(i + "_" + j, (int) (Mathrandom() 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);

p2remove(b_mine[i][j]);

}

}

public void itemStateChanged(ItemEvent e) { //级别变化会调用这个

if (egetSource() == item2) {

if (thread1 != null) {

thread1yield();

thread1 = null;

}

item2setState(true);

item3setState(false);

item4setState(false);

Difficulty = 1;

button_remove(Width, Height);

button_init(5, 5, 2);

layout_init(5, 5, icon_size);

fsetVisible(true);

}

if (egetSource() == item3) {

if (thread1 != null) {

thread1yield();

thread1 = null;

}

item2setState(false);

item3setState(true);

item4setState(false);

Difficulty = 2;

button_remove(Width, Height);

button_init(6, 6, 3);

layout_init(6, 6, icon_size);

fsetVisible(true);

}

if (egetSource() == item4) {

if (thread1 != null) {

thread1yield();

thread1 = null;

}

item2setState(false);

item3setState(false);

item4setState(true);

Difficulty = 3;

button_remove(Width, Height);

button_init(7, 7, 4);

layout_init(7, 7, icon_size);

fsetVisible(true);

}

layout_init(Width, Height, icon_size);

icon_size = 80;

fsetVisible(true);

}

public void mousePressed(MouseEvent e) {

if (thread1 == null) {

thread1 = new Thread(this);

thread1start();

}

//找出所点击的按钮的两个下标值//

b_ls = (JButton) egetSource();

for (int i = 0; i < Height; i++)

for (int j = 0; j < Width; j++) {

if (b_mine[i][j] == egetSource()) {

x = j;

y = i;

break;

}

}

}

public void mouseReleased(MouseEvent e) {

if (egetModifiers() == 16) //左键

{

String thissrc = (String) imgmapget(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();

tsetText("ok,You win用了" + l2getText() + "秒");

thread1 = null;

button_remove(Width, Height);

button_init(Width, Height, Width - 2);

layout_init(Width, Width, icon_size);

JOptionPaneshowConfirmDialog(f, t, "你赢", JOptionPaneOK_CANCEL_OPTION, JOptionPaneQUESTION_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("关于");

item10addActionListener(this);

item11 = new MenuItem("why");

item11addActionListener(this);

m_gamesadd(item2);

m_gamesadd(item3);

m_gamesadd(item4);

m_guanyuadd(item10);

m_guanyuadd(item11);

mbadd(m_games);

mbadd(m_guanyu);

fsetMenuBar(mb);

if (Difficulty == 1)

item2setState(true);

else if (Difficulty == 2)

item3setState(true);

else if (Difficulty == 3)

item4setState(true);

item2addItemListener(this);

item3addItemListener(this);

item4addItemListener(this);

}

public void run() { //计时器

n = 0;

String s;

while (thread1 != null) {

if (n < 10)

s = "00";

else if (n < 100)

s = "0";

else

s = "";

l2setText(s + StringvalueOf(++n));

try {

thread1sleep(1000);

}

catch (Exception e) {

}

}

}

public void actionPerformed(ActionEvent e) {

JLabel t = new JLabel();

if (egetSource() == item11) {

tsetText("这是why");

JOptionPaneshowConfirmDialog(f, t, "你赢", JOptionPaneOK_CANCEL_OPTION, JOptionPaneQUESTION_MESSAGE);

}else if (egetSource() == item10) {

tsetText("这是关于");

JOptionPaneshowConfirmDialog(f, t, "你赢", JOptionPaneOK_CANCEL_OPTION, JOptionPaneQUESTION_MESSAGE);

}

}

}

public class clearmine {

public static void main(String args[]) {

new mine();

}

}

一个翻图游戏的 把gif放到同一目录下命名为1gif,2gif等等运行看效果注释很全

其实很简单啦!你只需要在这个界面里放多个或者一个pushbutton,然后在pushbotton的callback里写下调用程序即可。比如:

h=gcf;

filename;

close(h);

然后保存,运行这个按钮界面,点击按钮就可以出现“filename”,filename为你的其他界面的名称!!简单吧!!

public class Test extends JFrame{

JFrame frame=null;

JPanel panel=null;

JTextArea label2;

public Test(){

init();

}

public void init(){

thissetTitle("input");

thissetBounds(300, 300, 200,200);

setDefaultCloseOperation(EXIT_ON_CLOSE);

thissetContentPane(new Panel());

setResizable(false);

setVisible(true);

frame=new JFrame("show");

framesetBounds(600, 300, 200,200);

framesetDefaultCloseOperation(EXIT_ON_CLOSE);

framesetResizable(false);

panel=new JPanel();

label2=new JTextArea();

label2setLineWrap(true);

label2setBounds(0, 0, 200, 200);

paneladd(label2);

frameadd(panel);

}

class Panel extends JPanel{

JTextArea text;

JLabel label ;

public Panel(){

setLayout(null);

text=new JTextArea();

textsetBounds(0, 0, 200, 200);

textsetLineWrap(true);

add(text);

textaddKeyListener(new KeyListener() {

public void keyTyped(KeyEvent e) {

}

public void keyReleased(KeyEvent e) {

}

public void keyPressed(KeyEvent e) {

if(egetKeyCode()==KeyEventVK_ENTER){

String str=textgetText();

label2setText(str);

framesetVisible(true);

}

}

});

}

}

public static void main(String[] args){

new Test();

}

}

结果:

以上就是关于java GUI 例子有错!求指正!全部的内容,包括:java GUI 例子有错!求指正!、gui页面设计的一些例子、求java gui例子等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9875188.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存