[英]Java button doesn't work
我正在尝试调试程序以进行家庭作业,但是我什至无法做到这一点,因为我不知道为什么我的按钮不起作用。 任何帮助表示赞赏,谢谢! (我知道我的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
您还将ActionListener
( this
)添加到findnext
按钮四次...我想您的意思是将其添加到其他每个按钮中
当心,AWT中已经有一个叫做Window
的类,它可能会引起人们的困惑。 不建议直接从顶级容器(如JFrame
进行扩展,而应以JPanel
开头并将其添加到JFrame
的实例(或您喜欢的任何容器)中
尝试以下操作:在ur构造函数中更新以下行:
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.