如何使用 java swing 只打开一个查找和替换对话框?
How to open only a single find and replace dialog box using java swing?
我使用 Java AWT 和 Swing API 创建了一个记事本应用程序。我能够打开、保存和执行所有操作,我还创建了一个 Jdialog 来在我的记事本应用程序中显示查找和替换对话框。
My question is that when i click edit->Find, i get the dialog box as many times i click find button, How to get it only one time?
final JDialog frDialog = new JDialog();
frDialog.setLayout(new GridLayout(3,4));
//frDialog.setModal(true);
frDialog.setVisible(true);
frDialog.requestFocus();
我不想使用setModal方法,我是新手,所以谁能给我推荐一个更好的防止对话框重复的方法?
提前致谢。
您可以创建一个模式 JDialog
,其中包含捕获搜索和替换字符串所需的选项。并且还为匹配大小写、正则表达式等选项提供复选框。为 Find Next
、Replace
、Replace All
和 Cancel
添加 JButton
按钮。为这些按钮编写适当的逻辑,最后显示记事本的 actionPerformed
方法中的对话框。这应该为您提供一个很好的起点来完成您正在寻找的东西。
Update:
用这个来抢先一步:
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Snippet {
public static void main(String[] args) {
JFrame notepadFrame = createFrame();
JDialog frDialog = new JDialog(notepadFrame);
frDialog.setLayout(new GridLayout(3,4));
JTextField txtFind = new JTextField();
JTextField txtReplace = new JTextField();
JButton btnFind = new JButton("Find");
JButton btnReplace = new JButton("Replace");
JButton btnReplaceAll = new JButton("Replace All");
frDialog.add(new JLabel("Find: "));
frDialog.add(txtFind);
frDialog.add(new JLabel(""));
frDialog.add(btnFind);
frDialog.add(new JLabel("Replace with: "));
frDialog.add(txtReplace);
frDialog.add(new JLabel(""));
frDialog.add(btnReplace);
frDialog.add(new JLabel(""));
frDialog.add(new JLabel(""));
frDialog.add(new JLabel(""));
frDialog.add(btnReplaceAll);
frDialog.pack();
frDialog.setVisible(true);
show(notepadFrame);
}
public static JFrame createFrame(){
JFrame frame = new JFrame("Notepad Frame");
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JTextArea());
return frame;
}
public static void show(JFrame frame) {
frame.setVisible(true);
}
}
希望对您有所帮助!
我使用 Java AWT 和 Swing API 创建了一个记事本应用程序。我能够打开、保存和执行所有操作,我还创建了一个 Jdialog 来在我的记事本应用程序中显示查找和替换对话框。
My question is that when i click edit->Find, i get the dialog box as many times i click find button, How to get it only one time?
final JDialog frDialog = new JDialog();
frDialog.setLayout(new GridLayout(3,4));
//frDialog.setModal(true);
frDialog.setVisible(true);
frDialog.requestFocus();
我不想使用setModal方法,我是新手,所以谁能给我推荐一个更好的防止对话框重复的方法?
提前致谢。
您可以创建一个模式 JDialog
,其中包含捕获搜索和替换字符串所需的选项。并且还为匹配大小写、正则表达式等选项提供复选框。为 Find Next
、Replace
、Replace All
和 Cancel
添加 JButton
按钮。为这些按钮编写适当的逻辑,最后显示记事本的 actionPerformed
方法中的对话框。这应该为您提供一个很好的起点来完成您正在寻找的东西。
Update:
用这个来抢先一步:
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Snippet {
public static void main(String[] args) {
JFrame notepadFrame = createFrame();
JDialog frDialog = new JDialog(notepadFrame);
frDialog.setLayout(new GridLayout(3,4));
JTextField txtFind = new JTextField();
JTextField txtReplace = new JTextField();
JButton btnFind = new JButton("Find");
JButton btnReplace = new JButton("Replace");
JButton btnReplaceAll = new JButton("Replace All");
frDialog.add(new JLabel("Find: "));
frDialog.add(txtFind);
frDialog.add(new JLabel(""));
frDialog.add(btnFind);
frDialog.add(new JLabel("Replace with: "));
frDialog.add(txtReplace);
frDialog.add(new JLabel(""));
frDialog.add(btnReplace);
frDialog.add(new JLabel(""));
frDialog.add(new JLabel(""));
frDialog.add(new JLabel(""));
frDialog.add(btnReplaceAll);
frDialog.pack();
frDialog.setVisible(true);
show(notepadFrame);
}
public static JFrame createFrame(){
JFrame frame = new JFrame("Notepad Frame");
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JTextArea());
return frame;
}
public static void show(JFrame frame) {
frame.setVisible(true);
}
}
希望对您有所帮助!