- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将光标放置在用户给定的行号上。为此,我编写了代码。但是,当我输入行号 2 时,光标放置在第一个字符之后,而不是第二行的开头。请帮助我...谢谢。这是代码:
public class GoToAction extends javax.swing.JFrame {
int i=0;
JTextPane textPane;
int lineCount;
public GoToAction() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
tabbedPane = new javax.swing.JTabbedPane();
jMenuBar1 = new javax.swing.JMenuBar();
file = new javax.swing.JMenu();
create = new javax.swing.JMenuItem();
goTo = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
file.setText("File");
create.setText("Create");
create.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
createActionPerformed(evt);
}
});
file.add(create);
goTo.setText("GoTo");
goTo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goToActionPerformed(evt);
}
});
file.add(goTo);
jMenuBar1.add(file);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void createActionPerformed(java.awt.event.ActionEvent evt) {
final JInternalFrame internalFrame = new JInternalFrame("");
i++;
internalFrame.setName("Document"+i);
internalFrame.setClosable(true);
internalFrame.setAutoscrolls(true);
textPane=new JTextPane();
textPane.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
internalFrame.add(textPane);
tabbedPane.add(internalFrame);
internalFrame.setSize(internalFrame.getMaximumSize());
internalFrame.pack();
internalFrame.setVisible(true);
}
private void goToActionPerformed(java.awt.event.ActionEvent evt) {
do {
try {
String str = (String)JOptionPane.showInputDialog(this,"Line number:\t","Goto line",JOptionPane.PLAIN_MESSAGE,null,null,null);
if( str == null ) {
break;
}
int lineNumber = Integer.parseInt(str);
lineCount=getLineCount();
if ( lineNumber > lineCount ) {
JOptionPane.showMessageDialog(this,"Line number out of range","EPAD Goto line",JOptionPane.ERROR_MESSAGE);
continue;
}
for ( int i = 0 ; i < lineCount; i++ ){
if ( i+1 == lineNumber ) {
textPane.setCaretPosition(i);
return;
}
}
} catch ( Exception e ) {
}
} while ( true );
}
public int getLineCount(){
lineCount=0;
Scanner sc=new Scanner(textPane.getText());
while(sc.hasNextLine()){
String line = sc.nextLine();
lineCount++;
}
return lineCount;
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(GoToAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(GoToAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(GoToAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(GoToAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new GoToAction().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JMenuItem create;
private javax.swing.JMenu file;
private javax.swing.JMenuItem goTo;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JTabbedPane tabbedPane;
// End of variables declaration
}
最佳答案
查看 Text Utilities 中的 gotoStartOfLine(...)
方法以获得更有效的解决方案,利用文本组件使用的 Document
的 Element
结构。
关于java - 使用 JTextPane GoTo 指定行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180839/
作为入门编程类(class)的家庭作业,我必须设计并实现一个生成随机数 (1-100) 的程序,然后为玩家提供 7 次猜测以正确猜出该数字。我想出了这个算法: 但是,我不知道如何将算法的这种概念表示转
这个问题在这里已经有了答案: 关闭 14 年前。 此问题与Goto Still Considered Harmful 重复.如果您想进一步讨论这个问题,请使用原始问题。 为什么 GOTO 是糟糕的编
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭3年前。 Improve th
我想知道在这种情况下您不应该使用 goto 而不是使用 while(variable) 是否有原因: :start result=askUser("do you want 1,2,or 3?") sw
是的,我知道 Esdger Dijkstra 说你不应该使用 goto ,但他不是上帝。我认为只要不过分使用无条件分支就可以了。过度使用继承和过度使用分支语句一样有可能创建不可读的代码。 无论如何,现
在 Visual Studio 中 Go To Definition 和 Go To Implementation 有什么区别? 版本:Visual Studio 2015 Update 1 最佳答案
Section 6.8.1 of C11或 C99 ,或 C89 的第 3.6.1 节所有这些似乎都表明 default 和 case x(其中 x 是一些 constant-expression)是
所以我下载了 sublime text 3,据称“Sublime Text 现在扫描项目中的文件,并构建哪些文件包含哪些符号的索引。这支持项目中的 Goto Definition 和 Goto Sym
当goto语句下面放置标签时,编译器如何找到标签的地址进行跳转? C 编程是自上而下的方法,对吗?如果标号位于 goto 语句之上,则可以理解,当标号到达 goto 语句时,其地址就已知。请大家解释一
谁能找出 VBA 中“On error goto -1”和“on error goto 0”之间的区别吗?我尝试过 google 和 msdn,但没有成功。 最佳答案 On Error GoTo 0
每当我运行 code下面它发生在我身上,我在使用 if 存在行时犯了一个错误,因为无论目录是否存在,它都表现为该行从未存在过...... echo off echo echo (c) Ryan L
我知道去函数声明的方法(使用 ctags + Ctrl + ])。但是,如果我不想转至函数声明,而是转至函数定义,该怎么办。 例如: // foo.c #include "foo.h" ... if
我正在尝试用防御性编程来完成这个小程序,但我很难处理这个避免 Loop-Goto 的问题,因为我知道这是糟糕的编程。我尝试过 while 和 do...while 循环,但在一种情况下我没有问题。当我
我正在尝试创建一个宏,允许用户单击按钮,Excel 工作表将执行 1:移至下一个/上一个工作表或 2:复制现有工作表,然后移至复制的工作表。 目前我陷入了处理错误处理情况的困境,并且不知道如何解决它。
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭5 个月前。 Improv
当他们谈论更喜欢汇编而不是更高级的语言时,我正在和某人通电话。他们提到喜欢 goto 语句,我回答说它会导致意大利面条代码。他们回复了“goto $”之类的内容。我说 goto 会散发出代码气味。他们
我有一个简单的数字根计算(即将整数中的所有数字相加,如果总数超过一位数字,则重复该过程,直到最终得到一位数字的答案)。 我的第一个冲动是简单地计算初始字符串中的数字之和,测试结果是否多于一位数字,如果
我对 C# 还很陌生(一周前开始学习),在批处理和表达式 2 方面有一些经验,并且我一直在开发基于文本的游戏,试图了解更多信息。我一开始使用了 goto 语句,但根据我发现的几乎每个人的说法,goto
#include int main() { int i; goto l; for(i = 0; i < 5; i++) { l:printf("Hi\n");
我必须编写一个 C 程序(不使用递归、else、for、while、do {} while 并且使用 if 的构造不得包含“return”)来从数组中查找最大数字。所以现在这是我的代码: #inclu
我是一名优秀的程序员,十分优秀!