JFrame 相关应用 包含:ActionListener、OutputStream应用、ReentrantLock公平锁应用、SwingUtilities应用
贴代码:
public class MainInterface extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
Boolean df = true;
private JButton BUTTONPDM;
private JButton BUTTONSDM;
private JButton BUTTONSJLX;
private JButton JFileChooserFile;
String tsnr = " 导出中..... ";
String tsnrer = " 导出失败!";
String tsnryes = " 导出成功!";
String pdm = "PDM";
String sdm = "SDM";
String sjlx = "SJLX";
String file = "FILE";
JLabel L2 = new JLabel(" 导出请点击 按钮!");
JLabel L3 = new JLabel();
JPanel mJPanel1 = new JPanel();
JTextArea textArea = new JTextArea("导出记录:\n", 20, 43);
JScrollPane scroll = new JScrollPane(textArea);
// 默认路径
JLabel Lfile = new JLabel(Modeskey.pash);
// 网络检查部分 定义
static String network = "网络检查中";
static JLabel networkJl = new JLabel(network, JLabel.RIGHT);
static Boolean isnetft = false;// 初始化 定义 不允许导出。等带网络检查
public static void main(String[] args) {
// 启动 检查
MainInterface.isNetworkrun();
// 启动
new MainInterface();
}
public MainInterface() {
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
updateTextArea(String.valueOf((char) b));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
updateTextArea(new String(b, off, len));
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setOut(new PrintStream(out, false));
System.setErr(new PrintStream(out, false));
this.setTitle("EXPORT PDM OR SDM v0.1"); // 标题
this.setSize(569, 671); // 窗口大小
this.setLocation(200, 300); // 窗口出现的位置
this.setLayout(new FlowLayout(FlowLayout.CENTER)); // 设置管理器
JLabel L1 = new JLabel("尊敬的:" + System.getenv().get("USERNAME") + " 欢迎您使用 EXPORT PDM OR SDM ", JLabel.LEFT);
mJPanel1.setLayout(new GridLayout(0, 2, 20, 10));
mJPanel1.setPreferredSize(new Dimension(450, 200));
mJPanel1.setBorder(new EmptyBorder(10, 0, 10, 0));
BUTTONPDM = new JButton(pdm); // 新建按钮
BUTTONSDM = new JButton(sdm); // 新建按钮
BUTTONSJLX = new JButton(sjlx);
JFileChooserFile = new JButton(file); // 新建按钮
this.add(L1);
mJPanel1.add(new JLabel("PDM导出", JLabel.CENTER));
mJPanel1.add(BUTTONPDM);
mJPanel1.add(new JLabel("SDM导出", JLabel.CENTER));
mJPanel1.add(BUTTONSDM);
mJPanel1.add(new JLabel("数据流向EXCEL导出", JLabel.CENTER));
mJPanel1.add(BUTTONSJLX);
mJPanel1.add(new JLabel("路径选择", JLabel.CENTER));
mJPanel1.add(JFileChooserFile);
mJPanel1.add(new JLabel("路径:", JLabel.CENTER));
mJPanel1.add(Lfile);
mJPanel1.add(L2);
mJPanel1.add(networkJl);
this.add(mJPanel1);
textArea.setBounds(20, 20, 100, 500); // 设置 JTextArea 宽100,高500
scroll.setBounds(20, 20, 100, 200); // 设置 JScrollPane 宽100,高200
textArea.setLineWrap(true); // 激活自动换行功能
textArea.setWrapStyleWord(true); // 激活断行不断字功能
textArea.setCaretColor(Color.black); // 字体颜色
textArea.setEditable(false); // 静止修改 可复制
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); // 滚动条
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); // 滚动条
this.add(scroll);
BUTTONPDM.addActionListener(this);
BUTTONSDM.addActionListener(this);
JFileChooserFile.addActionListener(this);
BUTTONSJLX.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置直接关闭窗口
this.setVisible(true); // 是否显示窗口
}
// 按钮监听
public void actionPerformed(ActionEvent e) {
try {
if (!isnetft) {
L2.setText("请稍后再试,网络异常将无法使用");
} else {
L3.setText("");
//TODO 具体 方法 自行定义
if (e.getSource() == BUTTONPDM) {
this.threadrun(pdm);
}
if (e.getSource() == BUTTONSDM) {
this.threadrun(sdm);
}
if (e.getSource() == JFileChooserFile) {
jfilechooser();
}
if (e.getSource() == BUTTONSJLX) {
this.threadrun(sjlx);
}
}
} catch (Exception e1) {
L2.setText(tsnrer);
L3.setText(e1.getMessage());
this.add(L3);
}
}
public static ReentrantLock reentrantLock = new ReentrantLock(true);// 公平锁
String dqddcx = "";// 当前导出项
// 多线程
public void threadrun(final String name) {
Thread thread = new Thread(new Runnable() {
public void run() {
if (reentrantLock.isLocked()) {
if (!tsxxqr(name, dqddcx + "导出中需请等待。当前导出需等待上一次导出完成。")) {
return;
} else {
System.out.println("===" + name + " 需等待标识:" + Modeskey.ID + " 导出完成才能进行导出,请等待...");
}
}
System.out.println("===" + name + " 导出进程开始=== 当前进程唯一标识:" + Thread.currentThread().getId());
try {
reentrantLock.lock();
Thread.sleep(1000);
Modeskey.ID = Thread.currentThread().getId();
Modeskey.getfilename();
// 按钮 start
if (name.equals(pdm)) {
dqddcx = pdm;
xixxxg(pdm);
//TODO 具体 方法 自行定义
} else if (name.equals(sdm)) {
dqddcx = sdm;
xixxxg(sdm);
//TODO 具体 方法 自行定义
} else if (name.equals(sjlx)) {
dqddcx = sjlx;
xixxxg(sjlx);
//TODO 具体 方法 自行定义
}
// 按钮 stop
hd(true, name, null);
} catch (Exception e) {
hd(false, name, e);
} finally {
reentrantLock.unlock();
}
System.out.println("===" + name + " 导出进程结束=== 当前进程唯一标识:" + Modeskey.ID);
}
});
thread.start();
}
// 回调
public void hd(Boolean bool, String name, Exception e) {
if (bool) {
L2.setText(name + tsnryes);
} else {
L2.setText(name + tsnrer);
}
}
// 显示信息修改
public void xixxxg(String name) {
if ("".equals(dqddcx)) {
L2.setText(name + tsnr);
} else {
L2.setText(dqddcx + tsnr);
}
}
// 确认 提示
public void tsxx(String name, String text) {
JOptionPane.showMessageDialog(new JFrame().getContentPane(), text, "提示 ", JOptionPane.INFORMATION_MESSAGE);
}
// 错误 提示
public void tsxxerr(String name, String text) {
JOptionPane.showMessageDialog(new JFrame().getContentPane(), text, "提示 ", JOptionPane.ERROR_MESSAGE);
}
// 选择 提示
public Boolean tsxxqr(String name, String text) {
int isDelete = JOptionPane.showConfirmDialog(new JFrame().getContentPane(), text, "提示 ",
JOptionPane.YES_NO_CANCEL_OPTION);
if (isDelete == JOptionPane.YES_OPTION) {
return true;
} else {
return false;
}
}
private void updateTextArea(final String text) {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
textArea.append(text);
textArea.paintImmediately(textArea.getBounds());
}
});
} catch (Exception ex) {
System.out.println("异常 请检查 -" + text + "-异常 抛出-" + ex.getMessage());
} finally {
// System.out.println(" 请检查 -"+text);
}
}
// 导出 文件 路径选择
public void jfilechooser() {
try {
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showDialog(new JLabel(), "选择文件夹");
if (returnVal == JFileChooser.APPROVE_OPTION) {
Modeskey.pash = chooser.getSelectedFile().getAbsolutePath();// 这个就是你选择的文件夹的路径
Lfile.setText(Modeskey.pash);
}
} catch (Exception e) {
System.out.println("====发生错误====" + e.getMessage());
}
}
// 网络检查
private static void isNetworkrun() {
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
try {
network = "网络检查中...";
// setnetworkjl();//不需要 实时更新 可以删除
if (!IsnNetwork.isConnect(Datadbs.ip)) {
network = "服务:" + Datadbs.ip + "-异常";
}
if (!IsnNetwork.isConnect(PullJdbcData.ip)) {
network = "服务:" + Datadbs.ip + "-异常";
}
if ("网络检查中...".equals(network)) {
network = "网络正常";
isnetft = true;
} else {
isnetft = false;
}
setnetworkjl();
Thread.sleep(5000);// 检查时间
} catch (InterruptedException e) {
System.out.println("网络检查 出现异常" + e.getMessage());
break;
}
}
}
});
t.start();
}
//SwingUtilities.invokeLater 应用
public static void setnetworkjl() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
networkJl.setText(network);
}
});
}
}
网络检查代码 付下:
//网络检查
public class IsnNetwork {
/**
* 判断网络是否正常
*
* @return
*/
public static boolean isConnect(String ipPath) {
// 定义其返回的状态,默认为false,网络不正常
boolean connect = false;
/**
* 用Runtime.getRuntime().exec()来调用系统外部的某个程序, 他会生成一个新的进程去运行调用的程序。
* 此方法返回一个java.lang.Process对象, 该对象可以得到之前开启的进程的运行结果, 还可以操作进程的输入输出流。
*/
Runtime runtime = Runtime.getRuntime();
Process process;
try {
process = runtime.exec("ping " + ipPath);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "GBK");
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
// 关闭
is.close();
isr.close();
br.close();
if (null != sb && !sb.toString().equals("")) {
String logString = "";
if (sb.toString().indexOf("TTL") > 0) {
// 网络畅通
connect = true;
} else {
// 网络不畅通
connect = false;
System.out.println("返回值为:" + sb);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return connect;
}
public static void main(String[] args) {
System.out.println(IsnNetwork.isConnect("119.119.119.119"));
}
}