我正在制作一个文本编辑器,它在文本的第一行中找到一个字符串,并在整个文本中突出显示它.问题是它还突出显示位于注释行中的事件(以"#"开头).到目前为止这是我的代码:
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class TextArea {
public static void main(String[] args) {
JFrame frame = new JFrame("textArea");
frame.setSize(500,500);
final JTextArea textArea = new JTextArea() ;
frame.add(textArea);
frame.setVisible(true);
textArea.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void changedUpdate(DocumentEvent e) {
try {
String keyWords = findKeyWord();
findOccurrences(keyWords);
} catch (BadLocationException ex) {
Logger.getLogger(TextArea.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void insertUpdate(DocumentEvent e) {
try { …
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序上使用警报对话框,但当用户触摸它时它会一直隐藏.这是我的代码:
public class DialogMessageEnd extends DialogFragment
{
String winner;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
Snooker_Scoreboard ss = new Snooker_Scoreboard();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(false);
builder.setMessage(ss.winnerPlayer + " won the match ("+ss.frame1ToPass+"-"+ss.frame2ToPass+")!")
.setPositiveButton("New Match!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getContext(),PlayerSelection.class);
startActivity(i);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我用过
builder.setCancelable(false);
Run Code Online (Sandbox Code Playgroud)
但它仍然无法解决问题.你能帮助我吗?谢谢
我试图从2d数组交换两列,这样
a = array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Run Code Online (Sandbox Code Playgroud)
变为:
b = array([[1, 3, 2],
[4, 6, 5],
[7, 9, 8]])
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?