我想知道游标在JTextArea中的列号和行号.即.在记事本中,当我在第一行而不是状态栏时显示Ln 1,Col 1.
提前致谢...
我JScrollPane
跟JTextArea
里面,我试图从右侧设置的JTextArea的方向向左所以里面的文字就会从右边开始,滚动条会出现在左边
我尝试过以下但是它们并没有影响方向的方向:
txt.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
txt.setAlignmentX(JTextArea.RIGHT_ALIGNMENT);
Run Code Online (Sandbox Code Playgroud)
编辑:
两个答案camickr和trashgod提供的工作正常但不在我的程序中我使用我的JTextArea作为对象消息并将其传递给OptionPane.
EDIT2:
我发现,setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
如果我将它应用于JOptionPane内容,则无效.是否有替代方案解决此问题?
与我的代码类似:
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TextArea extends JPanel
{
private JTextArea txt = new JTextArea();
public TextArea()
{
setLayout(new GridLayout());
txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JScrollPane scroll = new JScrollPane(txt);
scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
setPreferredSize(new Dimension(200,200));
this.add(scroll);
}
private void display()
{
Object[] options = {this};
JOptionPane pane = new JOptionPane();
int option = pane.showOptionDialog(null, null, "Title", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
}
public static void main(String[] args) …
Run Code Online (Sandbox Code Playgroud) 我目前正在使用Swing的控制台窗口.它基于JTextArea,就像一个通用命令行.在一行中键入命令,然后按Enter键.在下一行中,显示输出,在该输出下,您可以编写下一个命令.
现在我想,你只能用你的命令编辑当前行.上面的所有行(旧命令和结果)都应该是不可编辑的.我怎样才能做到这一点?
如果我像这样使用带有MigLayout的JTextArea:
MigLayout thisLayout = new MigLayout("", "[][grow]", "[]20[]");
this.setLayout(thisLayout);
{
jLabel1 = new JLabel();
this.add(jLabel1, "cell 0 0");
jLabel1.setText("jLabel1");
}
{
jTextArea1 = new JTextArea();
this.add(jTextArea1, "cell 0 1 2 1,growx");
jTextArea1.setText("jTextArea1");
jTextArea1.setLineWrap(false);
}
Run Code Online (Sandbox Code Playgroud)
然后,当调整窗口大小时,JTextArea会完美地缩小和缩小.当我将linewrap设置为true时,当我再次缩小窗口时,JTextArea不会缩小.我非常感谢任何帮助.谢谢
马塞尔
我有两个文件.一个扩展JFrame,另一个扩展JPanel.每当我改变帧的大小时,无论是最大化,拖动还是其他什么,我都希望ScrollPane能够适应帧的当前大小.除此之外,还有一个顶级菜单栏和一个底栏,但为了简单起见,我把它们留了下来.
基本上,我希望它像记事本一样工作.
现在,我在框架上使用ComponentListener,在另一个类中调用setSize方法.setSize方法只是:
public void resize(int x, int y)
{
textA.setPreferredSize(new Dimension(x, y-50));
areaScrollPane.setPreferredSize(new Dimension(x,y-50));
}
Run Code Online (Sandbox Code Playgroud)
另外,供参考:
public void componentResized(ComponentEvent e)
{
textA.resize(panel.getWidth(),panel.getHeight());
}
Run Code Online (Sandbox Code Playgroud)
仅供参考,它扩展了JPanel,因为我将其添加到框架中:
panel = (JPanel) this.getContentPane();
panel.setLayout(new BorderLayout());
panel.add(textA, BorderLayout.CENTER);
Run Code Online (Sandbox Code Playgroud)
那么最好的方法是什么?谢谢!
编辑:这是滚动窗格文件.它在我的主要部分称为textA.
public class TextArea extends JPanel
{
JTextArea textA=new JTextArea(500,500);
JScrollPane areaScrollPane = new JScrollPane(textA);
Toolkit toolkit = Toolkit.getDefaultToolkit ();
Dimension dim = toolkit.getScreenSize();
Dimension dim2=(new Dimension((int)(dim.getWidth()),(int)(dim.getHeight()-120)));
public TextArea()
{
//textA.setLineWrap(true);
//textA.setWrapStyleWord(true);
textA.setEditable(true);
textA.setForeground(Color.WHITE);
textA.setBackground(Color.DARK_GRAY);
this.setFont(null);
areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
areaScrollPane.setMinimumSize(new Dimension(300,300));
areaScrollPane.setSize(new Dimension(800,800));
textA.setPreferredSize(dim2);
areaScrollPane.setPreferredSize(dim2);
areaScrollPane.setMaximumSize(dim2);
add(areaScrollPane); …
Run Code Online (Sandbox Code Playgroud) 我使用Swing库创建了一个Java程序.现在我想将我的控制台输出重定向到JFrame或JPanel.
我正在尝试为程序创建GUI面板,我想要通常打印到我的命令提示符的所有内容,以打印到TextArea对象.我的GUI面板大部分都是格式化的,我无法将文本打印到TextArea,这里是我的文件:
package guipanel;
import javax.swing.*;
import java.awt.*;
import java.io.*;
/**
*
* @author Dan
*/
public class GUIPanel extends JFrame {
public GUIPanel() {
initComponents();
}
private void setOutputStream(boolean catchErrors) {
System.setOut(aPrintStream);
setVisible(true);
requestFocus();
if (catchErrors) {
System.setErr(aPrintStream);
}
}
private void addTabs(JTabbedPane jTabbedPane1) {
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
jTabbedPane1.add("Main", textArea1);
jTabbedPane1.add("Commands", jPanel);
jTabbedPane1.add("Rules", jPanel1);
jTabbedPane1.add("Links", jPanel2);
jTabbedPane1.add("Information", jPanel3);
jTabbedPane1.add("Shutdown", jPanel4);
setOutputStream(true);
}
@SuppressWarnings("unchecked") …
Run Code Online (Sandbox Code Playgroud) 当我尝试使用更改JTextArea的颜色时
textArea.setText("<html> <font color=\"red\"> Hi </font></html>")
Run Code Online (Sandbox Code Playgroud)
,文本显示基本上是整个文本写在setText方法中.但类似的事情适用于JLabel,JButton等.
我怎样才能为JTextArea做到这一点?
我想增加JTextField和JTextArea的间距/填充/插入.有效增加下图中两条红线之间的间距:
这是一段可运行的代码,显示了我的"问题".
我有一个被JTextArea
包裹的JScrollPane
.当我更改文本时JTextArea
,JScrollPane
滚动会自动滚动到文本的末尾,我不希望这样.
这是我的要求:
(即使文本比水平方式更多,应用程序和用户都不应该能够水平滚动.垂直方向,只有用户才能滚动.)
我不知道如何"修复"这个问题:应该使用JTextArea
或JScrollPane
方法修复吗?
请注意,AFAICT根本不重复:JTextPane阻止在父JScrollPane中滚动
这是一个有趣的例子,它每隔200毫秒放入新文本JTextArea
,你可以看到JScrollPane
总是自动滚动到文本的末尾.
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public final class TextInScrollPane extends JFrame {
private static final Random r = new Random( 42 );
public static void main( final String[] args ) {
final JFrame f = new JFrame();
f.setDefaultCloseOperation( EXIT_ON_CLOSE ); …
Run Code Online (Sandbox Code Playgroud)