自己的作品界面---类似360杀毒软件的界面
(参数设置界面)
不多说,直接上代码。代码略长,请耐心看。
package com.ids.windows;
import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.IAxis;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.traces.Trace2DLtd;
import info.monitorenter.gui.chart.views.ChartPanel;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import com.ids.datahandle.UserData;
import com.ids.mitjudge.Judge;
/**
*
*/
public class windows {
/**
*
*/
public windows() {
}
public static void main(String[] args) {
MainPanel main =new MainPanel();
main.setVisible(true);
main.setAlwaysOnTop(true);
}
}
class MainPanel extends JFrame implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = -8303249428878144366L;
/** 容器 */
private Container con = null;
/** 图片获取 */
private Toolkit tkit = null;
/** 各种标签 */
private JLabel minLabel, closeLabel, mssfhLabel, mcpsjLabel,
mbdcsLabel,zdsmLabel, csszLabel;
/** 各种面板 */
private JPanel bdcsPanel, headPanel, listPanel,showDataPanel,changPanel;
public JTextArea showData;
private int operType = 1;
Font font=new Font("楷体",Font.BOLD,14);
Trace2DLtd trace;
Chart2D chart ;
ChartPanel chartPanel;
Judge judge;
JLabel jl, jl1, tell,add,sub,add1,sub1,add2,sub2;
private JLabel start, stop,chang2;
JFileChooser fc = new JFileChooser();
UserData ud;
int changData=0;
String path=null;
/** 构造函数,生成整个窗口 */
public MainPanel() {
super();
initSelf();
combinElems();
}
/** 窗口初始化 */
public void initSelf() {
this.setUndecorated(true);
con = this.getContentPane();
con.setLayout(new BorderLayout());
this.setSize(642, 482);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("IDS");// 窗口命名
tkit = new Toolkit();// 图片处理对象
this.setIconImage(tkit.getImage("ico.gif"));// 标志
}
public void combinElems() {
headPanel = new JPanel(new BorderLayout());
headPanel.setOpaque(false);//透明
new Dragger(this, headPanel);
JPanel h1Panel = new JPanel();
h1Panel.setOpaque(false);//透明
h1Panel.setPreferredSize(new Dimension(642, 17));
h1Panel.setLayout(new FlowLayout(2, 0, 0));
minLabel = new JLabel(tkit.getImgageIcon("h1_min_01.gif"));// 缩小系统到托盘
minLabel.setToolTipText("最小化系统");
minLabel.addMouseListener(this);
closeLabel = new JLabel(tkit.getImgageIcon("h1_close_01.gif"));// 退出系统
closeLabel.setToolTipText("关闭系统");
closeLabel.addMouseListener(this);
JLabel name=new JLabel();
name.setText("基于改进的DCA算法与FCM算法的网络入侵智能报警系统 ");
name.setFont(new Font("仿宋",Font.BOLD,12));
name.setForeground(Color.black);
h1Panel.add(name);
h1Panel.add(minLabel);
h1Panel.add(closeLabel);
JPanel h2Panel=new JPanel();
h2Panel.setOpaque(false);//透明
h2Panel.setPreferredSize(new Dimension(642, 85));
h2Panel.setLayout(null);
JLabel iconLabel=new JLabel(tkit.getImgageIcon("ico.gif"));
iconLabel.setBounds(40, 0, 85, 85);
JLabel context=new JLabel();
context.setText("IDS-让您的网络更安全");
context.setFont(new Font("楷体",Font.BOLD,20));
context.setBounds(140, 25, 400, 40);
context.setForeground(Color.black);
h2Panel.add(iconLabel);// 软件标志
h2Panel.add(context);
headPanel.add(BorderLayout.NORTH, h1Panel);// 把顶层面板加到容器中
headPanel.add(BorderLayout.CENTER, h2Panel);// 把中间面板加到容器中
JPanel bPanel = new JPanel(new BorderLayout());
bPanel.setOpaque(false);//透明
JPanel btmpPanel = new JPanel();
btmpPanel.setPreferredSize(new Dimension(400, 37));
btmpPanel.setLayout(new FlowLayout(0, 0, 0));
btmpPanel.setOpaque(false);//透明
//网络监测
// 本地检测
mbdcsLabel = new JLabel(tkit.getImgageIcon("m_bdcs_00.gif"));// 样本检测背景
mbdcsLabel.addMouseListener(this);
mbdcsLabel.setToolTipText("网络监测");
mssfhLabel = new JLabel(tkit.getImgageIcon("m_ssfh_01.gif"));// 网络检测背景
mssfhLabel.addMouseListener(this);
mssfhLabel.setToolTipText("样本检测");
mcpsjLabel = new JLabel(tkit.getImgageIcon("m_cpsj_01.gif"));// 流量分析背景
mcpsjLabel.addMouseListener(this);
mcpsjLabel.setToolTipText("流量分析");
csszLabel = new JLabel(tkit.getImgageIcon("m_cssz_01.gif"));// 参数设置背景
csszLabel.addMouseListener(this);
csszLabel.setToolTipText("参数设置");
JLabel JLabel9=new JLabel(tkit.getImgageIcon("9.gif"));
JLabel JLabel91=new JLabel(tkit.getImgageIcon("9.gif"));
JLabel JLabel8=new JLabel(tkit.getImgageIcon("8.gif"));
JLabel JLabel81=new JLabel(tkit.getImgageIcon("8.gif"));
JLabel JLabel82=new JLabel(tkit.getImgageIcon("8.gif"));
btmpPanel.add(JLabel9);//
btmpPanel.add(mssfhLabel);
btmpPanel.add(JLabel8);// 大选项卡小背景
btmpPanel.add(mbdcsLabel);
btmpPanel.add(JLabel81);
btmpPanel.add(mcpsjLabel);
btmpPanel.add(JLabel82);
btmpPanel.add(csszLabel);
btmpPanel.add(JLabel91);//
JPanel mainPanel=new JPanel();
mainPanel.setOpaque(false);//透明
mainPanel.setPreferredSize(new Dimension(642, 343));
zdsmLabel = new JLabel(tkit.getImgageIcon("select.gif"));
zdsmLabel.setOpaque(false);
zdsmLabel.addMouseListener(this);
zdsmLabel.setVisible(false);
start = new JLabel(tkit.getImgageIcon("start.gif"));
start.addMouseListener(this);
stop = new JLabel(tkit.getImgageIcon("stop.gif"));
stop.addMouseListener(this);
chang2 = new JLabel(tkit.getImgageIcon("chang2.gif"));
chang2.addMouseListener(this);
chang2.setVisible(false);
showData=new JTextArea();
showData.setPreferredSize(new Dimension(280, 270));
showData.setOpaque(false);//透明
showData.setLineWrap(true);//激活自动换行功能
showData.setWrapStyleWord(true);//激活断行不断字功能
showData.setForeground(Color.black);
showData.setFont(font);
showData.setText("检测结果将在这里以文字形式显示;\n" +
"检测结果将在右边图表以图形形式显示。\n当前功能:网络检测!\n");
showDataPanel=new JPanel();
showDataPanel.setOpaque(false);
showDataPanel.setPreferredSize(new Dimension(300, 290));
showDataPanel.setBorder(BorderFactory.createEtchedBorder());//加框
showDataPanel.add(showData,"center");
/** 插入图表 */
chart = new Chart2D();
trace = new Trace2DLtd(300);
trace.setColor(Color.red);
chart.setFont(new Font("楷体",Font.BOLD,10));
chart.addTrace(trace);
chart.setOpaque(false);
chart.setName("检测分析结果");
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new Dimension(300, 290));
chartPanel.setBorder(BorderFactory.createEtchedBorder());
/** 图表就在chartPanel里面,还能控制大小什么的 **/
bdcsPanel = new JPanel(new FlowLayout(1, 60, 0));
bdcsPanel.setOpaque(false);
bdcsPanel.add(zdsmLabel);
bdcsPanel.add(start);
bdcsPanel.add(stop);
//bdcsPanel.add(chang1);
bdcsPanel.add(chang2);
listPanel = new JPanel();
listPanel.setOpaque(false);
listPanel.setPreferredSize(new Dimension(642, 90));//110
listPanel.add(bdcsPanel);
//参数设置
changPanel=new JPanel();
changPanel.setOpaque(false);
changPanel.setLayout(null);
changPanel.setPreferredSize(new Dimension(300, 290));
changPanel.setBorder(BorderFactory.createEtchedBorder());
changPanel.setVisible(false);//参数设置
add=new JLabel();
add.setText("+0.1");
add.setBorder(BorderFactory.createEtchedBorder());
add.setFont(new Font("楷体",Font.BOLD,14));
add.setForeground(Color.black);
add.setHorizontalAlignment(SwingConstants.CENTER);
add.addMouseListener(this);
add.setBounds(35,30, 100, 25);
sub=new JLabel();
sub.setText("-0.1");
sub.setBorder(BorderFactory.createEtchedBorder());
sub.setFont(new Font("楷体",Font.BOLD,14));
sub.setForeground(Color.black);
sub.setHorizontalAlignment(SwingConstants.CENTER);
sub.addMouseListener(this);
sub.setBounds(165,30, 100, 25);
add1=new JLabel();
add1.setText("+0.01");
add1.setBorder(BorderFactory.createEtchedBorder());
add1.setFont(new Font("楷体",Font.BOLD,14));
add1.setForeground(Color.black);
add1.setHorizontalAlignment(SwingConstants.CENTER);
add1.addMouseListener(this);
add1.setBounds(35,85, 100, 25);
sub1=new JLabel();
sub1.setText("-0.01");
sub1.setBorder(BorderFactory.createEtchedBorder());
sub1.setFont(new Font("楷体",Font.BOLD,14));
sub1.setForeground(Color.black);
sub1.setHorizontalAlignment(SwingConstants.CENTER);
sub1.addMouseListener(this);
sub1.setBounds(165,85, 100, 25);
add2=new JLabel();
add2.setText("+0.001");
add2.setBorder(BorderFactory.createEtchedBorder());
add2.setFont(new Font("楷体",Font.BOLD,14));
add2.setForeground(Color.black);
add2.setHorizontalAlignment(SwingConstants.CENTER);
add2.addMouseListener(this);
add2.setBounds(35,140, 100, 25);
sub2=new JLabel();
sub2.setText("-0.001");
sub2.setBorder(BorderFactory.createEtchedBorder());
sub2.setFont(new Font("楷体",Font.BOLD,14));
sub2.setForeground(Color.black);
sub2.setHorizontalAlignment(SwingConstants.CENTER);
sub2.addMouseListener(this);
sub2.setBounds(165,140, 100, 25);
changPanel.add(add);//
changPanel.add(sub);//
changPanel.add(add1);//
changPanel.add(sub1);//
changPanel.add(add2);//
changPanel.add(sub2);//
mainPanel.add(listPanel);
mainPanel.add(showDataPanel);
mainPanel.add(chartPanel);
mainPanel.add(changPanel);
bPanel.add(BorderLayout.NORTH, btmpPanel);
bPanel.add(BorderLayout.CENTER, mainPanel);
SkinPanel bigPanel = new SkinPanel("5.gif", 642,482);
bigPanel.setOpaque(false);
bigPanel.setLayout(new BorderLayout());
bigPanel.add(BorderLayout.NORTH, headPanel);
bigPanel.add(BorderLayout.CENTER, bPanel);
con.add(bigPanel);
judge = new Judge(trace,showData);
ud=new UserData();
}
@Override
public void mouseClicked(MouseEvent e) {
// System.out.println(e.getSource());
if (closeLabel == e.getSource()) {
System.out.println("退出!");
System.exit(0);
} else if (minLabel == e.getSource()) {
System.out.println("最小化!");
this.setState(JFrame.ICONIFIED);
} else if (0 != operType && mbdcsLabel == e.getSource()) {
System.out.println("样本分析!");
zdsmLabel.setVisible(true);//选择文件
start.setVisible(true);//开始检测
stop.setVisible(true);//停止检测
showData.setVisible(true);//文字输出
chartPanel.setVisible(true);//图标输出
changPanel.setVisible(false);//参数设置
//chang1.setVisible(false);
chang2.setVisible(false);
trace.removeAllPoints();
showData.setText("检测结果将在这里以文字形式显示;\n" +
"检测结果将在右边图表以图形形式显示。\n当前功能:本地检测!\n");
operType = 0;
} else if (1 != operType && mssfhLabel == e.getSource()) {
System.out.println("网络分析!");
zdsmLabel.setVisible(false);//选择文件
start.setVisible(true);//开始检测
stop.setVisible(true);//停止检测
showData.setVisible(true);//文字输出
chartPanel.setVisible(true);//图标输出
changPanel.setVisible(false);//参数设置
//chang1.setVisible(false);
chang2.setVisible(false);
trace.removeAllPoints();
showData.setText("检测结果将在这里以文字形式显示;\n" +
"检测结果将在右边图表以图形形式显示。\n当前功能:网络监测!\n");
operType = 1;
} else if (2 != operType && mcpsjLabel == e.getSource()) {
System.out.println("流量分析!");
zdsmLabel.setVisible(false);//选择文件
start.setVisible(true);//开始检测
stop.setVisible(true);//停止检测
showData.setVisible(true);//文字输出
chartPanel.setVisible(true);//图标输出
changPanel.setVisible(false);//参数设置
//chang1.setVisible(false);
chang2.setVisible(false);
trace.removeAllPoints();
showData.setText("检测结果将在这里以文字形式显示;\n" +
"检测结果将在右边图表以图形形式显示。\n当前功能:流量分析!\n");
operType = 2;
} else if (3 != operType && csszLabel == e.getSource()) {
System.out.println("参数设置!");
zdsmLabel.setVisible(false);//选择文件
start.setVisible(false);//开始检测
stop.setVisible(false);//停止检测
showData.setVisible(true);//文字输出
chartPanel.setVisible(false);//图标输出
changPanel.setVisible(true);//参数设置
//chang1.setVisible(true);
chang2.setVisible(true);
showData.setText("当前功能:参数设置!\n请先选择需要设置的参数然后" +
"\n点击右边按钮调整系统参数;\n" +
"调整节结果会在这里显示。\n");
changData=0;
operType = 3;
} else if (zdsmLabel == e.getSource()) {
System.out.println("选择文本!");
// tell.setText("当前功能:样本检测!");
int intRetVal = fc.showOpenDialog(this);
if (intRetVal == JFileChooser.APPROVE_OPTION) {
path=fc.getSelectedFile().getPath();
showData.setText("您选择的样本文件是:" + fc.getSelectedFile().getPath()+"\n");
}
zdsmLabel.setVisible(false);
bdcsPanel.setVisible(true);
} else if ((0 == operType)&&stop == e.getSource()) {
System.out.println("本地检测程序停止!");
showData.setText("本地检测程序已停止!");
judge.JudgeEnd();
} else if ((1 == operType)&&stop == e.getSource()) {
System.out.println("网络检测停止!");
showData.setText("网络检测程序已停止!");
judge.JudgeEnd();
} else if (0 == operType && start == e.getSource()) {
System.out.println("本地检测开始!");
showData.setText("本地检测程序已运行,请稍后!\n");
judge.MitlJudgeBegin(path);
} else if (1 == operType && start == e.getSource()) {
System.out.println("网络监测开始!");
showData.setText("网络监测程序已运行,程序会每1分钟输出一次分析结果,请稍后!\n");
judge.DosBegin();
} else if (2 == operType && start == e.getSource()) {
System.out.println("流量分析开始");
showData.setText("流量分析程序已运行,程序会每1秒钟输出一次分析结果,请稍后!\n");
judge.gfdBegin();
}else if (2 == operType&&stop == e.getSource()) {
System.out.println("流量分析停止!");
showData.setText("流量分析程序已停止!\n");
judge.gfdEnd();
}
else if(chang2 == e.getSource()){
System.out.println("设置异常阈值!");
showData.setText("当前异常阈值是:"+ud.msav+"\n");
changData=2;
}
else if(add == e.getSource()){//+100
addChang(+0.100000000000000);
}
else if(sub == e.getSource()){
subChang(-0.100000000000000000);
}
else if(add1 == e.getSource()){//+10
addChang(+0.01000000000000000000000000);
}
else if(sub1 == e.getSource()){
subChang(-0.01000000000000000000000);
}
else if(add2 == e.getSource()){//+1
addChang(+0.00100000000000000000000000000000);
}
else if(sub2 == e.getSource()){
subChang(-0.00100000000000000000000);
}
}
private void addChang(double x){
if(changData==0){
showData.setText("请先选择需要设置的参数,\n" +
"然后点击右边按钮调整系统参数;\n" +
"调整节结果会在这里显示。\n");
}
else if(changData==2){
ud.msav+=x;
showData.setText("异常阈值+"+x+";\n当前异常阈值是:"+ud.msav+".\n");
}
System.out.println("设置异常阈值!+"+x);
}
private void subChang(double x){
if(changData==0){
showData.setText("请先选择需要设置的参数;\n" +
"然后点击右边按钮调整系统参数;\n" +
"调整节结果会在这里显示。\n");
}
else if(changData==2){
if(ud.msav-x<0){
showData.setText("异常阈值不能小于0\n");
}
else{
ud.msav-=x;
showData.setText("异常阈值-"+x+";\n当前异常阈值是:"+ud.msav+".\n");
}
}
System.out.println("设置异常阈值!-"+x);
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
/** 根据给定图片及大小,格式化一个面板并渲染 */
class SkinPanel extends JPanel {
private static final long serialVersionUID = -1142694233560441425L;
private static String RES_PATH = "/img/";
private Image skinImg;
private int width, height;
public SkinPanel(String resName, int width, int height) {
this.width = width;
this.height = height;
try {
System.out.println(resName);
skinImg = ImageIO.read(this.getClass().getResource(
RES_PATH + resName));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 复写渲染方法
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (0 != width) {
g.drawImage(skinImg, 0, 0, width, height, null);
} else {
g.drawImage(skinImg, 0, 0, null);
}
}
}
class Toolkit {
private static String RES_PATH = "/img/";
/** 根据图像名称获得图像 */
public Image getImage(String resName) {
Image img = null;
try {
System.out.println(resName);
img = ImageIO.read(this.getClass().getResource(RES_PATH + resName));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
/** 根据图片名称,生成图标 */
public ImageIcon getImgageIcon(String resName) {
return new ImageIcon(getImage(resName));
}
}
class Dragger {
private Window fWindow;
private Component fComponent;
private int dx;
private int dy;
/**
* 让指定的Component通过鼠标拖动来移动Window
*/
public Dragger(Window window, Component component) {
fWindow = window;
fComponent = component;
fComponent.addMouseListener(createMouseListener());
fComponent.addMouseMotionListener(createMouseMotionListener());
}
private MouseListener createMouseListener() {
return new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Point clickPoint = new Point(e.getPoint());
SwingUtilities.convertPointToScreen(clickPoint, fComponent);
dx = clickPoint.x - fWindow.getX();
dy = clickPoint.y - fWindow.getY();
fWindow.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseReleased(MouseEvent e) {
fWindow.setCursor(Cursor.getDefaultCursor());
}
};
}
private MouseMotionAdapter createMouseMotionListener() {
return new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
Point dragPoint = new Point(e.getPoint());
SwingUtilities.convertPointToScreen(dragPoint, fComponent);
fWindow.setLocation(dragPoint.x - dx, dragPoint.y - dy);
}
};
}
}