我想通过Jframe中的JButton删除单行。 但我不知道怎么... 我试过媒体链接:通过TextArea中的JButton删除单行
public void button1_ActionPerformed(ActionEvent evt) {
int count = 1;
count = TextArea1.getLineCount();
但它不工作... 我欣赏每一个怎样的帮助:)) 或任何knews解决这个问题的另一种方法?
我想通过Jframe中的JButton删除单行。 但我不知道怎么... 我试过媒体链接:通过TextArea中的JButton删除单行
public void button1_ActionPerformed(ActionEvent evt) {
int count = 1;
count = TextArea1.getLineCount();
但它不工作... 我欣赏每一个怎样的帮助:)) 或任何knews解决这个问题的另一种方法?
您需要使用GetText()来获取TextArea中的内容,然后删除该行。一旦你修改了文本,你可以使用SetText()重新放回。
当然,这可以在一行中完成,但分离步骤有助于易读性。
它几乎可以正常工作,但我想删除一行,并且不能使用getText(),因为如果您有 两件事在该地区: asdf1 asdf2 您可以将文本设置为(“”) 但比不仅asdf1是越来越删除,但也asdf2 对不起我的英语不好而难过偷你的时间:d – user3272186
在你举例asdf1和asdf2在同一行上。如果你只想摆脱一个你可以使用String.split()来分离它们。有关详细信息,请参阅http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java。 – OlivierLi
别担心,你不是在偷我的时间。如果您觉得我的答案解决了您的问题,您可以使用复选标记来接受它。这将足够支付! – OlivierLi
答案取决于“线条”的定义。例如,如果您使用的是包装的JTextArea
,其中单个连续的文本行环绕视图,则可以将一行视为从视图的一侧运行到另一侧的文本。
在这种情况下,你需要深入研究的模型和计算文本的基础上,考虑到偏移量和基本除去两个点之间的内容,例如...
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.JTextComponent;
import javax.swing.text.Utilities;
public class TestDeleteLine {
public static void main(String[] args) {
new TestDeleteLine();
}
private JTextArea ta;
public TestDeleteLine() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
ta = new JTextArea(20, 40);
ta.setWrapStyleWord(true);
ta.setLineWrap(true);
JButton deleteLine = new JButton("Delete current line");
deleteLine.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
int offset = ta.getCaretPosition();
int rowStart = Utilities.getRowStart(ta, offset);
int rowEnd = Utilities.getRowEnd(ta, offset);
Document document = ta.getDocument();
int len = rowEnd - rowStart + 1;
if (rowStart + len > document.getLength()) {
len--;
}
String text = document.getText(rowStart, len);
document.remove(rowStart, len);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(ta));
frame.add(deleteLine, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
现在,如果你不关心的换行和简单的想要删除整条生产线(从一个新行到另一个),你可以使用...
public int getLineByOffset(int offset) throws BadLocationException {
Document doc = ta.getDocument();
if (offset < 0) {
throw new BadLocationException("Can't translate offset to line", -1);
} else if (offset > doc.getLength()) {
throw new BadLocationException("Can't translate offset to line", doc.getLength() + 1);
} else {
Element map = doc.getDefaultRootElement();
return map.getElementIndex(offset);
}
}
public int getLineStartOffset(int line) throws BadLocationException {
Element map = ta.getDocument().getDefaultRootElement();
if (line < 0) {
throw new BadLocationException("Negative line", -1);
} else if (line >= map.getElementCount()) {
throw new BadLocationException("No such line", ta.getDocument().getLength() + 1);
} else {
Element lineElem = map.getElement(line);
return lineElem.getStartOffset();
}
}
public int getLineEndOffset(int line) throws BadLocationException {
Element map = ta.getDocument().getDefaultRootElement();
if (line < 0) {
throw new BadLocationException("Negative line", -1);
} else if (line >= map.getElementCount()) {
throw new BadLocationException("No such line", ta.getDocument().getLength() + 1);
} else {
Element lineElem = map.getElement(line);
return lineElem.getEndOffset();
}
}
public int[] getLineOffsets(int line) throws BadLocationException {
int[] offsest = new int[2];
offsest[0] = getLineStartOffset(line);
offsest[1] = getLineEndOffset(line);
return offsest;
}
计算线路开始和结束位置,计算出文本的长度,并从Document
,这看起来更像是将其删除...
int offset = ta.getCaretPosition();
int line = getLineByOffset(offset);
int[] lineOffsets = getLineOffsets(line);
int len = lineOffsets[1] - lineOffsets[0] - 1;
Document document = ta.getDocument();
String text = document.getText(lineOffsets[0], len);
document.remove(lineOffsets[0], len);
这只会计数变量的值。你忘了粘贴一些代码? – OlivierLi
我不这么认为......并且我不知道如何修复该程序:P 它也显示了get line,但我不确定它是如何工作的 – user3272186
定义“line” - 这是一条物理线路,用“\ n”分隔,新行或文本行出现在文本区域中? – MadProgrammer