dispose 模式 java_请java高手帮忙解答: dispose()与setVisible()的问题

展开全部

给你改好了,试试e69da5e887aa62616964757a686964616f31333264623765吧。

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Dialog;

import java.awt.Frame;

import java.awt.Label;

import java.awt.TextArea;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

public class project1_1 extends Dialog //implements ActionListener

{

Label L1 = new Label("It must be 4 figure");

Label L2 = new Label("Congratulation, you are right! ");

Label L3 = new Label("It must be figure");

Label L4 = new Label("There should not be the same number");

Button b0 = new Button("sure");

public void success() {

setTitle("success");

add(L2,BorderLayout.CENTER);

this.validate();

this.setVisible(true);

}

public void error() {

setTitle("Error");

add(L1,BorderLayout.CENTER);

this.validate();

this.setVisible(true);

}

public void flase() {

setTitle("False");

add(L3,BorderLayout.CENTER);

this.validate();

this.setVisible(true);

}

public void same() {

setTitle("Same");

add(L4,BorderLayout.CENTER);

this.validate();

this.setVisible(true);

}

public project1_1(Frame owner, TextArea ta1, String a) {

super(owner, "");

this.setLayout(new BorderLayout());

//add(b0);

//setLayout(null);

setResizable(false);

this.setSize(200, 200);

this.setLocation(owner.getLocationOnScreen().x+50,owner.getLocation().y+150);

setVisible(true);

//b0.addActionListener(this);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

project1_1.this.dispose();

}

});

}

/*public void actionPerformed(ActionEvent e){

project1_1.this.dispose();

}*/

}

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.util.Random;

import javax.swing.JOptionPane;

class caishuzi extends Frame implements ActionListener {

String templateStr=this.getRandomNumString();//原始数字串

private Label L1 = new Label("please input your guess");

TextArea tf1 = new TextArea("");

Label L2 = new Label("Here will show the result");

TextArea tf2 = new TextArea("");

Button b1 = new Button("Yes");

Button b2 = new Button("Exit");

Button b3 = new Button("clean");

Panel p1 = new Panel(new GridLayout(1, 3));

Panel p2 = new Panel();

Panel p3 = new Panel();

Panel p4 = new Panel();

public caishuzi(int a, int b, int c, int d) {

super("The small game of guess number");

setLayout(new GridLayout(5, 1));

setBounds(a, b, c, d);

add(L1);

add(tf1);

add(L2);

add(tf2);

p2.add(b1);

p3.add(b2);

p4.add(b3);

p1.add(p2);

p1.add(p4);

p1.add(p3);

add(p1);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

setVisible(true);

}

public void actionPerformed(ActionEvent e) {

if (e.getSource() == b1) {

// 输入数字个数不等于4个

if (tf1.getText().length() != 4) {

new project1_1(this, tf1, templateStr).error();

//JOptionPane.showMessageDialog(null, "");

return;

}

// 输入值非数字时

try {

int x1 = Integer.parseInt(tf1.getText());

} catch (Exception y) {

new project1_1(this, tf1, templateStr).flase();

return;

}

// 输入相同数字

int m = 0;

String text=tf1.getText();

for (int i = 0; i <3; i++) {

if(text.indexOf(text.charAt(i), i+1)>0)

m++;

/*for (int p = i + 1; p <= 3; p++)

if (tf1.getText().charAt(i) == tf1.getText().charAt(i + p)) {

m++;

}*/

}

if (m > 0) {

new project1_1(this, tf1, templateStr).same();

return;

}

// 输入正确数字时

if (tf1.getText().equals(templateStr)) {

new project1_1(this, tf1, templateStr).success();

}

else {

// int n=tf1.getText().compareTo(tf2.getText());

int count = 0;

int index = 0;

//System.out.println(templateStr);

for(int i=0;i<4;i++){

if(templateStr.indexOf(text.charAt(i))>=0){

count++;

if(templateStr.charAt(i)==text.charAt(i)){

count--;

index++;

}

}

}

tf2.append(index+"A"+count+"B ");

}

}

if (e.getSource() == b2) {

this.dispose(); // public void dispose()释放由此

// Window、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源。

}

if (e.getSource() == b3) {

tf1.setText("");

tf2.setText("");

}

}

private String getRandomNumString(){

StringBuffer sb=new StringBuffer();

Random rand=new Random();

sb.append(rand.nextInt(9));

for(int i=0;i<3;i++){

while(true){

int temp=rand.nextInt(9);

if(sb.toString().indexOf(temp+"")<0){

sb.append(temp);

break;

}

}

}

return sb.toString();

}

}

public class project1 {

public static void main(String agrs[]) {

new caishuzi(500, 50, 300, 500);

}

}

老实说,搞不懂你这程序怎么来的,感觉是改别人的,好多逻辑很乱。 还有一点就是,干嘛非要自己弄一个dialog呢,JOptionPane.showMessageDialog(null,"msg");多好,简单方便还好看点。

本回答被网友采纳

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值