Java 按钮不起作用



我正在尝试调试我的程序以进行家庭作业,但我什至无法做到这一点,因为我不知道为什么我的按钮不起作用。任何帮助不胜感激,谢谢!(我知道我的findnext现在很糟糕,但我不知道还能做什么,所以我现在只是调试它)

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() ;
                }
            }
        }
    }
}

您有一个阴影问题。 您声明...

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(或你喜欢的任何容器)的实例中。

试试这个:在您的构造函数中更新以下行:

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");

相关内容

  • 没有找到相关文章