2 summer820228 summer820228 于 2015.07.21 21:58 提问

tf.setBounds(80, 200,200,200); 竖座标超过80 程序不运行

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

import javax.swing.*;

public class Three extends JFrame implements ActionListener, Runnable {

/**
 * 
 */
private static final long serialVersionUID = -8560277451929244119L;
JTextField tf = new JTextField();
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
JButton b3 = new JButton("END");
boolean isGo = false;
String path = "three.jpg";
ImageIcon background = new ImageIcon(path);
JTextArea jiang1= new JTextArea();
int choujiangcishu=1;

public Three() {
    this.getContentPane().setLayout(null);
    Dimension srcDim = Toolkit.getDefaultToolkit().getScreenSize();  

    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    this.getContentPane().add(tf);


    ImageIcon background = new ImageIcon(path);// 背景图片
    JLabel jlbg = new JLabel(background);// 把背景图片显示在一个标签里面
    jlbg.setBounds(0, 0, srcDim.width, srcDim.height);// 把标签的大小位置设置为图片刚好填充整个面板
    JPanel imagePanel = (JPanel) this.getContentPane();
    imagePanel.setOpaque(false);//设置透明
    this.getLayeredPane().add(jlbg, new Integer(Integer.MIN_VALUE));

    /*tf.setFont(new Font("Courier", Font.PLAIN, 35));
    tf.setHorizontalAlignment(JLabel.LEFT);
    tf.setOpaque(false);
    tf.setBackground(Color.red);// 按钮红色背景
    tf.setEditable(false);//不可编辑
    //tf.setBorder(BorderFactory.createEmptyBorder());//无边框
    tf.setBounds(80, 80, srcDim.width/6, srcDim.height/22);
    */
    tf.setFont(new Font("Courier", Font.PLAIN, 35));
    tf.setHorizontalAlignment(JLabel.LEFT);
    tf.setOpaque(false);
    tf.setBackground(Color.red);// 按钮红色背景
    tf.setEditable(false);//不可编辑
    //tf.setBorder(BorderFactory.createEmptyBorder());//无边框
*************** tf.setBounds(80, 200,200,200);



    b1.setBounds(1,100,10,10);
    b2.setBounds(1,200,10,10);
    b3.setBounds(1,300,10,10);




    this.getContentPane().add(tf);
    this.getContentPane().add(b1);
    this.getContentPane().add(b2);
    this.getContentPane().add(b3);



    b1.setActionCommand("start");
    b2.setActionCommand("end");
    b3.setActionCommand("over");

    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);

    SymListener sy = new SymListener();
    b1.registerKeyboardAction(sy,KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); 
    b2.registerKeyboardAction(sy,KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); 
    b3.registerKeyboardAction(sy,KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); 
    //jButton.setMnemonic(KeyEvent.VK_ENTER);
    b2.setEnabled(false);

    super.setUndecorated(true);// 无标题栏
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    this.setLocation(0, 0);
    Cursor cu = new Cursor(Cursor.HAND_CURSOR);
    this.setCursor(cu);
    this.setVisible(true);
    tf.setText("888888");
    operdata geshu=new operdata();
    go(geshu.getCount());

}    

public void go(int p) {
    while (true) {
        if (isGo == true) {
            String s = "";

            for (int j = 1; j <= 1; j++) {
                int i = (int) (Math.random()*p+1);//设定奖项总数
                if (i < 10) {
                    s = s + "00000" + i;
                } else if(i<100 & i>=10) 
                {
                    s = s + "0000" + i;
                }else if(i<1000 & i>=100) 
                {
                    s = s + "000" + i;
                }else if(i<10000 & i>=1000) 
                {
                    s = s + "00" + i;
                }else if(i<100000 & i>=10000) 
                {
                    s = s + "0" + i;
                }else
                {
                    s = s +  i;
                }

            }
            tf.setText(s);

        }

        try {
            Thread.sleep(10);
        } catch (java.lang.InterruptedException e) {
            e.printStackTrace();
        }

    }


}

public void actionPerformed(ActionEvent e) {
/*  String s = e.getActionCommand();

    if (s.equals("start")) {
        isGo = true;
        b1.setEnabled(false);
        b2.setEnabled(true);
    } 
    if (s.equals("end")){
        isGo = false;
        operdata aaa =new operdata();
        String bb= aaa.getuserdata(tf.getText());
        jiang1.setText(jiang1.getText()+"\n"+""+(choujiangcishu++)+":  "+tf.getText()+bb);

        b2.setEnabled(false);
        b1.setEnabled(true);
    }*/
}

public static void main(String[] args)  {
    //Dimension srcDim = Toolkit.getDefaultToolkit().getScreenSize(); 
   // ImgUtil image =new ImgUtil();
    //image.createThumb("3.png", "223.jpg",(int)srcDim.getWidth(), (int)srcDim.getHeight());
    new Three();

} 
class SymListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    Object obj = e.getSource();

    if (obj == b1) {
            isGo = true;
            b1.setEnabled(false);
            b2.setEnabled(true);
    } else if (obj == b2) {
        isGo=false;
        b2.setEnabled(false);
        b1.setEnabled(true);


    }else if (obj == b3) {
        dispose();

    }
    }
    }
@Override
public void run() {
    // TODO Auto-generated method stub

}

//@Override
/*public void run() {

    this.yunyingzhongxingo();
}*/

}

5个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.07.21 22:08

代码拷贝进去运行试了一下,没报错。最好把你的错误贴出来

summer820228
summer820228 我的程序也不报错 这一个抽奖程序 JTextField里面的数字是变动的 可竖座标设成200后 JTextField的数字就没变化了
2 年多之前 回复
summer820228
summer820228   2015.07.21 22:27

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

import javax.swing.*;

public class Three extends JFrame implements ActionListener{

/**
 * 
 */
private static final long serialVersionUID = -8560277451929244119L;
JTextField tf = new JTextField();
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
JButton b3 = new JButton("END");
boolean isGo = false;


int choujiangcishu=1;

public Three() {
    this.getContentPane().setLayout(null);
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);

    tf.setBounds(80,91, 200, 200);
    b1.setBounds(1,100,10,10);
    b2.setBounds(1,200,10,10);
    b3.setBounds(1,300,10,10);

    this.getContentPane().add(tf);
    this.getContentPane().add(b1);
    this.getContentPane().add(b2);
    this.getContentPane().add(b3);



    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);

    SymListener sy = new SymListener();
    b1.registerKeyboardAction(sy,KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); 
    b2.registerKeyboardAction(sy,KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); 
    b3.registerKeyboardAction(sy,KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); 

    b2.setEnabled(false);

    //super.setUndecorated(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    this.setLocation(0, 0);
    Cursor cu = new Cursor(Cursor.HAND_CURSOR);
    this.setCursor(cu);
    this.setVisible(true);
    tf.setText("888888");
    go();

}    

public void go() {
    while (true) {
        if (isGo == true) {
            String s = "";

            for (int j = 1; j <= 1; j++) {
                int i = (int) (Math.random()*10000+1);//设定奖项总数
                if (i < 10) {
                    s = s + "00000" + i;
                } else if(i<100 & i>=10) 
                {
                    s = s + "0000" + i;
                }else if(i<1000 & i>=100) 
                {
                    s = s + "000" + i;
                }else if(i<10000 & i>=1000) 
                {
                    s = s + "00" + i;
                }else if(i<100000 & i>=10000) 
                {
                    s = s + "0" + i;
                }else
                {
                    s = s +  i;
                }

            }
            tf.setText(s);

        }

        try {
            Thread.sleep(10);
        } catch (java.lang.InterruptedException e) {
            e.printStackTrace();
        }

    }


}

public void actionPerformed(ActionEvent e) {

}

public static void main(String[] args)  {

    new Three();

} 
class SymListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    Object obj = e.getSource();

    if (obj == b1) {
            isGo = true;
            b1.setEnabled(false);
            b2.setEnabled(true);
    } else if (obj == b2) {
        isGo=false;
        b2.setEnabled(false);
        b1.setEnabled(true);


    }else if (obj == b3) {
        dispose();

    }
    }
    }

}

    tf.setBounds(80,90, 200, 200);  按空格数字会随机变         tf.setBounds(80,91, 200, 200);   数字就不随机变了。    求解啊!
tongyi55555
tongyi55555   2015.07.21 23:15

关键问题是你的JButton增加的事件响应中没有对应的执行代码,你加的this指的是Three类,而这个类中是没有具体的actionPerformed实现的,你改成这个就行了:

b1.addActionListener(sy);
        b2.addActionListener(sy);
        b3.addActionListener(sy);
tongyi55555
tongyi55555   2015.07.21 23:19

另外你说的90可以运行,91不可以运行,其实是不存在的,90和91都可以运行,只要你启动后,鼠标点击一下tf区域即可启动了。而不能启动的原因是你的focus定位在了b1或b3上,不建议你使用这个方式,改成我上面的代码就不会因为焦点的问题而出错了。

代码的运行跟你的布局位置是没有关系的。

danielinbiti
danielinbiti   Ds   Rxr 2015.07.21 23:43
 修改1
 搜索//b1.registerKeyboardAction(sy,KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);这句,并注释
 修改为
  b1.getInputMap(JComponent.WHEN_FOCUSED).put(
         KeyStroke.getKeyStroke((char)KeyEvent.VK_SPACE), "PressKeyAction");
   b1.getActionMap().put("PressKeyAction", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            isGo = true;
               b1.setEnabled(false);
               b2.setEnabled(true);
        }
       });
其他按钮可以同样修改

 修改2
    this.setVisible(true);
    tf.setText("888888");//搜索这句

    b1.requestFocus();//增加焦点
Csdn user default icon
上传中...
上传图片
插入图片