1:主类
package screen;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.UIManager;
public class NewScreen {
/**
* @author Skewen
* @address ChengDu
* @category JavaSwing
* @date 2010-5-16
*/
private static final long serialVersionUID = 1L;
private Container con = null;
private JLabel testLabel = null;
private JLabel autoLabel = null;
private JLabel neusoft = null;
private JLabel helpLabel=null;
private JFrame frame = null;
private Color[] colors = { Color.WHITE, Color.BLACK, Color.RED, Color.blue,
Color.GREEN, Color.YELLOW, Color.darkGray, Color.ORANGE };
private int i = 0;
private Timer timer = null;
private Boolean mark = true;
private Font font = null;
private Cursor cursor = null;
private Dimension dimension = null;
private JLabel[] labels = new JLabel[4];
public NewScreen() {
frame = new JFrame("显示器测试");
con = frame.getContentPane();
font = new Font("宋体", Font.PLAIN, 25);
cursor = new Cursor(Cursor.HAND_CURSOR);
dimension = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) dimension.getWidth();
int height = (int) dimension.getHeight();
testLabel = new JLabel("Exit");
autoLabel = new JLabel("AutoTest");
neusoft = new JLabel("Author:skewen");
neusoft.setForeground(Color.black);
helpLabel=new JLabel("Help");
autoLabel.setToolTipText("按回车键开始自动测试!");
labels[0] = testLabel;
labels[1] = autoLabel;
labels[2] = neusoft;
labels[3] = helpLabel;
testLabel.setFont(font);
autoLabel.setFont(font);
neusoft.setFont(font);
helpLabel.setFont(font);
testLabel.setCursor(cursor);
autoLabel.setCursor(cursor);
neusoft.setCursor(cursor);
helpLabel.setCursor(cursor);
testLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
helpLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(frame, "1:在屏幕任意位置单击可以进行测试!\n2:点击AutoTest或者按Enter键可自动对屏幕检测!\n" +
"3:点击Stop AutoTest或者按Space键可停止自动对屏幕检测!\n4:点击Exit或者按Esc键可退出系统!", "帮助",JOptionPane.INFORMATION_MESSAGE);
}
});
autoLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (mark) {
timer.start();
autoLabel.setText("Stop AutoTest");
autoLabel.setToolTipText("按空格键终止自动测试!");
mark = false;
} else {
timer.stop();
autoLabel.setText("AutoTest");
autoLabel.setToolTipText("按回车键开始自动测试!");
mark = true;
}
}
});
neusoft.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
try {
Runtime.getRuntime().exec("cmd.exe /c start " + "http://skewen.iteye.com");
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
frame.setLayout(null);
con.add(testLabel);
con.add(autoLabel);
con.add(neusoft);
con.add(helpLabel);
helpLabel.setBounds(100, height - 100,60, 50);
testLabel.setBounds(width - 120, height - 100, 100, 50);
autoLabel.setBounds(width / 2 - 100, height / 2 - 25, 180, 50);
neusoft.setBounds(width / 2 - 100, height - 100, 200, 50);
con.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
i = (i % 7);
con.setBackground(colors[i + 1]);
testLabel.setForeground(colors[i]);
autoLabel.setForeground(colors[i]);
neusoft.setForeground(colors[i]);
helpLabel.setForeground(colors[i]);
i++;
}
});
timer = new Timer(1300, new MyListener(con, colors, labels));
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (mark) {
timer.start();
autoLabel.setText("Stop AutoTest");
mark = false;
}
} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (!mark) {
timer.stop();
autoLabel.setText("AutoTest");
mark = true;
}
}
}
});
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new NewScreen();
}
}
2:用于自动测试的一个事件类:
package screen;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
public class MyListener implements ActionListener {
/**
* @author Skewen
* @address ChengDu
* @category JavaSwing
* @date 2010-5-16
*/
private Container con=null;
private Color[] colors =null;
int i = 0;
private JLabel[] labels;
public MyListener(Container con,Color[] colors,JLabel[] labels){
this.con=con;
this.colors=colors;
this.labels=labels;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
i = i % 7;
con.setBackground(colors[i+1]);
labels[0].setForeground(colors[i]);
labels[1].setForeground(colors[i]);
labels[2].setForeground(colors[i]);
labels[3].setForeground(colors[i]);
i++;
}
}