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(0,0,0,0);
b2.setBounds(0,0,0,0);
jiang1.setBounds(srcDim.width/30, srcDim.height/20,800,800);
jiang1.setOpaque(false);
jiang1.setFont(new Font("Courier",Font.PLAIN,22));
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(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);
//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();