码迷,mamicode.com
首页 > 编程语言 > 详细

dom4j来解析xml,通过URL获取服务器端返回的字符串,java swing 实现的页面

时间:2015-09-18 12:11:49      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:dom4j   xml   java swing   

1)使用dom4j来解析xml字符串内容

import org.dom4j.DocumentHelper;

String domString = getDomString();

Document document = DocumentHelper.parseText(domString);

List<Element> list = document.selectNodes("table/tr/td/div/div/span");

String str = list.get(0).getText().substring(3).trim();


2)通过URL获取服务器端返回的字符串

import cp.common.IOUtils;

private static String getDomString() throws IOException{

InputStream in = null;

try {

URL url = new URL("http://10.12.123.40/userinfo.aspx");

URLConnection uc = url.openConnection();

uc.connect();

in = (InputStream)url.getContent();

byte[] buff = new byte[in.available()];

in.read(buff, 0, in.available());

String str = new String(buff, "UTF-8");

str = str.substring(StringUtils.indexOf(str, "table") - 1);

str = str.replace("TOPLEVEL", "").replace("&nbsp;", "").replace("┆", "");

return str;

} finally {

IOUtils.closeQuietly(in);

}

}


3) java swing 实现的页面

public class App extends JFrame {

    public static final int FRAME_WIDTH = 600;

    public static final int FRAME_HEIGHT = 450;

    private MainPanel mainPanel;

    private ConfigPanel configPanel;

    private CardLayout cardManager;

    public App() {

        setTitle("stgcp " + Version.getVersion());

        setIconImage(new ImageIcon(getClass().getClassLoader().getResource("tools.png")).getImage());

        setSize(FRAME_WIDTH, FRAME_HEIGHT);

        setLocationRelativeTo(null);

        cardManager = new CardLayout();

        getContentPane().setLayout(cardManager);

        setMinimumSize(new Dimension(FRAME_WIDTH + 40, FRAME_HEIGHT));


        mainPanel = new MainPanel();

        getContentPane().add(mainPanel, "mainPanel");

        configPanel = new ConfigPanel();

        getContentPane().add(configPanel, "configPanel");

        mainPanel.getEventDispatcher().setConfigPanel(configPanel);


        pack();

        //setResizable(false);

        setVisible(true);

        setDefaultCloseOperation(EXIT_ON_CLOSE);


        ActionListener al = new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                if ("showSettings".equals(e.getActionCommand())) {

                    cardManager.show(App.this.getContentPane(), "configPanel");

                    configPanel.refreshConfigValues();

                } else if ("mainPanel".equals(e.getActionCommand())) {

                    cardManager.show(App.this.getContentPane(), "mainPanel");

                } else if ("showHelp".equals(e.getActionCommand())) {

                    JOptionPane.showMessageDialog(App.this, Version.getTips(), "提示", JOptionPane.INFORMATION_MESSAGE);

                }

            }

        };


        mainPanel.registerCardLayoutChangeListener(al);

        configPanel.registerCardLayoutChangeListener(al);


        addWindowListener(new WindowAdapter() { // 窗口关闭事件

            public void windowClosing(WindowEvent e) {

                System.exit(0);

            };

            public void windowIconified(WindowEvent e) { // 窗口最小化事件

                App.this.setVisible(false);

                App.this.miniTray();

            }

        });


        new Thread(new Runnable() {

            @Override

            public void run() {

                String name = UserInfoUtil.getUserRealName();

                if (StringUtils.isNotBlank(name)) {

                    App.this.setTitle(App.this.getTitle() + " - 你好," + name);

                }

            }

        }).start();

    }


    private void miniTray() {

        ImageIcon trayImg = new ImageIcon(getClass().getClassLoader().getResource("tools.png"));

        PopupMenu pop = new PopupMenu();

        MenuItem show = new MenuItem("Show");

        MenuItem exit = new MenuItem("Exit");


        final SystemTray tray = SystemTray.getSystemTray();

        final TrayIcon trayIcon = new TrayIcon(trayImg.getImage(), "stgcp", pop);

        trayIcon.setImageAutoSize(true);


        show.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                tray.remove(trayIcon);

                App.this.setVisible(true);

                App.this.setExtendedState(JFrame.NORMAL);

                App.this.toFront();

            }

        });


        exit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                tray.remove(trayIcon);

                System.exit(0);

            }

        });


        pop.add(show);

        pop.add(exit);


        trayIcon.addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {

                if (e.getClickCount() == 2) {

                    tray.remove(trayIcon);

                    App.this.setVisible(true);

                    App.this.setExtendedState(JFrame.NORMAL);

                    App.this.toFront();

                }

            }

        });


        try {

            tray.add(trayIcon);

        } catch (AWTException e1) {

            e1.printStackTrace();

        }

    }


    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override

            public void run() {

                Font biggerFont = new Font("宋体", Font.PLAIN, 14);

                Font font1 = new Font("宋体", Font.PLAIN, 12);

                Font font2 = new Font("宋体", Font.PLAIN, 12);

                UIManager.put("Panel.font", font2);

                UIManager.put("PopupMenu.font", font2);

                UIManager.put("Button.font", font1);

                UIManager.put("Label.font", font1);

                UIManager.put("EditorPane.font", font2);

                UIManager.put("TextField.font", biggerFont);

                UIManager.put("PasswordField.font", biggerFont);

                UIManager.put("TextPane.font", font1);

                UIManager.put("swing.boldMetal", false);

                new App();

            }

        });

    }

}


dom4j来解析xml,通过URL获取服务器端返回的字符串,java swing 实现的页面

标签:dom4j   xml   java swing   

原文地址:http://6817977.blog.51cto.com/6807977/1695922

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!