2014-04-07 125 views
1

我試圖調試我的程序作業,但我甚至不能這樣做,因爲我不知道爲什麼我的按鈕無法正常工作。 任何幫助表示讚賞,謝謝! (我知道我是FindNext中扭曲了一步,但我不知道現在做些什麼,所以我只是調試它)Java按鈕不起作用

public class Window extends JFrame implements ActionListener { 
    private JButton findnext; 
    private JButton replace; 
    private JButton delete; 
    private JButton upper; 
    private JTextField from,to; 
    private JTextArea textArea; 
    final static Color found = Color.PINK; 
    final Highlighter hilit; 
    final Highlighter.HighlightPainter painter; 

    public Window() { 
     setTitle("Project 8"); 
     Toolkit tk = Toolkit.getDefaultToolkit(); 
     Dimension d = tk.getScreenSize(); 
     setSize((d.width/4)*3,d.height); 
     textArea = new JTextArea ("The apple ate the apple.",8,40); 
     textArea.setLineWrap(true); 
     Container contentPane = getContentPane(); 
     addWindowListener(new Close()); 
     contentPane.add(textArea); 
     JPanel panel = new JPanel(); 
     JButton findnext = new JButton("FindNext"); 
     panel.add(findnext); 
     from = new JTextField(8); 
     panel.add(from); 
     findnext.addActionListener(this); 
     JButton replace = new JButton("Replace"); 
     panel.add(replace); 
     to = new JTextField(8); 
     panel.add(to); 
     findnext.addActionListener(this); 
     JButton delete = new JButton("Delete"); 
     panel.add(delete); 
     findnext.addActionListener(this); 
     JButton upper = new JButton("Upper"); 
     panel.add(upper); 
     findnext.addActionListener(this); 
     contentPane.add(panel, "South"); 
     hilit = new DefaultHighlighter(); 
     painter = new DefaultHighlighter.DefaultHighlightPainter(found); 
     textArea.setHighlighter(hilit); 
    } 

    public void actionPerformed(ActionEvent evt) { 
     String f = from.getText(); 
     String t = to.getText(); 
     int n = textArea.getText().indexOf(f); 
     Object source = evt.getSource(); 

     if (source == findnext) { 
      hilit.removeAllHighlights(); 
      String text = textArea.getText(); 
      int index = text.indexOf(f,0); 
      if (index>0) { 
       try { 
        hilit.addHighlight(index, index+f.length(), DefaultHighlighter.DefaultPainter); 
       } 
       catch (BadLocationException e) { 
        ; 
       } 
      }else if (source == replace) { 
       if (n>=0 && f.length() > 0) { 
        textArea.replaceRange(to.getText(),n,n+f.length()); 
        ; 
       }else if (source == delete) { 
        textArea.setText(" "); 
       }else if (source == upper) { 
        f.toUpperCase() ; 
       } 
      } 
     } 
    } 
} 
+0

如果您使用的是Eclipse或其他一些IDE,我建議添加斷點來幫助調試程序。 – Coderchu

+0

另外我注意到你在編輯格式時沒有做任何事情。擺脫那個單一的分號。 – Coderchu

+0

我只是覺得那裏有一個臨時措施,因爲我對錯誤例外不太熟悉,而且熒光筆法需要它。 – user3335429

回答

1

你有陰影的問題。您聲明......

private JButton findnext; 
private JButton replace; 
private JButton delete; 
private JButton upper; 

但在你的構造做什麼...

JButton findnext = new JButton("FindNext"); 
//... 
JButton replace = new JButton("Replace"); 
//... 
JButton delete = new JButton("Delete"); 
//... 
JButton upper = new JButton("Upper"); 

這是重新聲明的變量。

這意味着,當你試圖做...

if (source == findnext) { 

它總是false

你也加入ActionListenerthis)到findnext按鈕四次......我想你的意思是將它添加到每個其他按鈕

請注意,AWT中已經有一個名爲Window的類,這可能會引起人們的混淆。它也鼓勵直接從像JFrame頂層容器延伸,而是應以JPanel開始和它添加到JFrame實例(或你喜歡什麼永遠容器)

1

試試這個: 在烏拉圭回合構造更新此行:

JButton findnext = new JButton("FindNext"); 
// 
JButton replace = new JButton("Replace"); 
// 
JButton delete = new JButton("Delete"); 
// 
JButton upper = new JButton("Upper"); 

使用這一個:

findnext = new JButton("FindNext"); 
// 
replace = new JButton("Replace"); 
// 
delete = new JButton("Delete"); 
// 
upper = new JButton("Upper");