小编oip*_*psl的帖子

使用多个正则表达式扫描文件

我有一些代码接收URL,读取文件并搜索与给定正则表达式匹配的字符串,并将任何匹配添加到arrayList,直到它到达文件末尾.如何修改我的代码,以便在阅读文件时,我可以在同一个传递中检查其他与其他正则表达式匹配的字符串,而不是必须多次读取文件检查每个不同的正则表达式?

    //Pattern currently being checked for
    Pattern name = Pattern.compile("<a id=.dg__ct(.+?)_hpl1.>(.+?)</a>");

    //Pattern I want to check for as well, currently not implemented
    Pattern date = Pattern.compile("[0-9]{2}/[0-9]{2}/[0-9]{4}");

    Matcher m;
    InputStream inputStream = null;
    arrayList = new ArrayList<String>();
    try {
        URL url = new URL(
                "URL to be read");
        inputStream = (InputStream) url.getContent();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        InputStreamReader isr = new InputStreamReader(inputStream);
        BufferedReader buf = new BufferedReader(isr);
        String str = null;
        String s = null;

        try { …
Run Code Online (Sandbox Code Playgroud)

java regex

3
推荐指数
1
解决办法
4997
查看次数

设置键绑定以执行与我的动作侦听器中相同的操作

我有一个附加到ActionListener的JButton,但我还想为按钮添加一个快捷键以使用户更友好.比如,用户可以单击按钮,程序执行某些功能"f",或者用户也可以按键盘上的"Enter"执行相同的功能f.所以这就是我的代码的主旨

private JButton button;

public static void main(String[] args){
    Action buttonListener = new AbstractAction() {
         public void actionPerformed(ActionEvent e) {
                //Perform function f    
         }
    };

button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"),
                        "test");
button.getActionMap().put("test",
                         buttonListener);

button.addActionListener(new OtherListener());
}

private class OtherListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        //Perform function f
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来有点乏味,不得不添加一个Action和一个ActionListener来做同样的事情.也许我没有看到它,但有没有办法减少代码,所以我可以消除Action并只使用actionListener?我在考虑在getActionMap().put()方法中切换buttonListener参数,但该方法只接受Action类型.

java swing event-handling

2
推荐指数
1
解决办法
3087
查看次数

我的Thread.sleep()出现问题

我正在创建一个简单的视频扑克程序,现在我正在处理用户指定了他想要持有的牌之后执行的操作,并在抽奖后用新牌替换丢弃的牌.我有一个动作,我想在所有替换之间逐个更换卡片,但是使用下面的代码,它将睡眠500毫秒乘以我必须更换的卡片数量然后替换全部卡片一次,而不是按我的要求一次更换一张.任何帮助是极大的赞赏!

Action drawAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            int deckPos = 5;

            if((holdValFirst.getText()).equals("HELD")){}
            else{                   
                holdFirst.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif"));
                deckPos++;
                try
                {
                    Thread.sleep(500);              
                }catch (InterruptedException ie){
                    System.out.println(ie.getMessage());
                }
            }
            if((holdValSecond.getText()).equals("HELD")){}
            else{                   
                holdSecond.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif"));
                deckPos++;
                try
                {
                    Thread.sleep(500);              
                }catch (InterruptedException ie){
                    System.out.println(ie.getMessage());
                }               
            }
            if((holdValThird.getText()).equals("HELD")){}
            else{
                holdThird.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif"));
                deckPos++;
                try
                {
                    Thread.sleep(500);              
                }catch (InterruptedException ie){
                    System.out.println(ie.getMessage());
                }                   
            }
            if((holdValFourth.getText()).equals("HELD")){}
            else{                   
                holdFourth.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif"));
                deckPos++;  
                try
                {
                    Thread.sleep(500);              
                }catch (InterruptedException ie){
                    System.out.println(ie.getMessage());
                }               
            }
            if((holdValFifth.getText()).equals("HELD")){}
            else{                                       
                holdFifth.setIcon(new ImageIcon(((deck.getDeck())[deckPos]).getCardName()+".gif"));
                deckPos++;                                  
            }               
        }
    };
Run Code Online (Sandbox Code Playgroud)

java multithreading thread-sleep

1
推荐指数
1
解决办法
573
查看次数