问题描述
- java swing 一个窗口打开新创口 加上go()程序就死掉了
- import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.*;public class Swing7 extends JFrame implements ActionListener {
JButton jb = new JButton();public Swing7() { this.setTitle(""Java——""); jb.setText(""确定""); jb.setMnemonic('a'); this.add(jb); this.setBounds(200 300 250 300); this.setVisible(true); jb.addActionListener(this); //由于Swing7实现了ActionListener接口,所以给jb添加的ActionListener就是Swing7实例。}public void actionPerformed(ActionEvent e) {// 实现ActionListener接口的actionPerformed接口。 yidengjiang frame=new yidengjiang(); frame.setVisible(true);}public static void main(String args[]) { Swing7 s = new Swing7();}
}
import java.awt.*;
import java.awt.event.*;
import java.util.Random;import javax.swing.*;
public class yidengjiang extends JFrame implements ActionListener {
/** * */private static final long serialVersionUID = -8560277451929244119L;JTextField tf = new JTextField();JButton b1 = new JButton(""开始"");JButton b2 = new JButton(""停止"");boolean isGo = false;String path = ""223.jpg"";ImageIcon background = new ImageIcon(path);JTextArea jiang1= new JTextArea();int choujiangcishu=1;public yidengjiang() { this.getContentPane().setLayout(null); Dimension srcDim = Toolkit.getDefaultToolkit().getScreenSize(); System.out.println(srcDim.width+"" ""+srcDim.height); this.setExtendedState(JFrame.MAXIMIZED_BOTH); this.getContentPane().add(tf); this.getContentPane().add(b1); 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 110)); tf.setHorizontalAlignment(JLabel.CENTER); tf.setOpaque(false); //tf.setBackground(Color.red);// 按钮红色背景 tf.setEditable(false);//不可编辑 tf.setBorder(BorderFactory.createEmptyBorder());//无边框 tf.setBounds(srcDim.width/2-srcDim.width/3/2 srcDim.height/2+srcDim.height/10/2 srcDim.width/3 srcDim.height/10); b1.setBounds(0000); b2.setBounds(0000); jiang1.setBounds(srcDim.width/30 srcDim.height/20800800); jiang1.setOpaque(false); jiang1.setFont(new Font(""Courier""Font.PLAIN22)); jiang1.setEditable(false); this.getContentPane().add(tf); this.getContentPane().add(b1); this.getContentPane().add(b2); this.getContentPane().add(jiang1); b1.setActionCommand(""start""); b1.addActionListener(this); b2.addActionListener(this); SymListener sy = new SymListener(); b1.registerKeyboardAction(syKeyStroke.getKeyStroke(KeyEvent.VK_SPACE 0) JComponent.WHEN_IN_FOCUSED_WINDOW); b2.registerKeyboardAction(syKeyStroke.getKeyStroke(KeyEvent.VK_SPACE 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""); go();} public void go() { while (true) { if (isGo == true) { String s = """"; Random rand = new Random(); for (int j = 1; j <= 1; j++) { int i = (int) (Math.random()*260000+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); } else { isGo = false; jiang1.setText(jiang1.getText()+""n""+tf.getText()); 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 yidengjiang();} */ 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; 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); } } }
}
解决方案
SWING是单线程的,需要将循环放到单独的线程中执行。修改成这个:
new Thread(){ public void run() {go();}; }.start();
解决方案二:
new Thread(){
public void run() {go();}; }.start();
解决方案三:
你的go方法中的while不是死循环吗
时间: 2024-11-17 16:17:30