So I have a simple text editor and at the moment most of its features are working, however adding scroll bars is where i'm having trouble. I tried looking up stuff with adding to the frame and going from there, but that just removes my ability to type or see anything. At the moment I'm adding with the content pane, but it doesn't seem to register the scrollbars. Also when it does there are not usable and the rest of the text area is uneditable.Here is the small bit of code that deals with that, could you help me find what's going wrong.
所以我有一个简单的文本编辑器,目前它的大多数功能都在工作,但是添加滚动条是我遇到麻烦的地方。我尝试通过添加到框架并从那里查找内容,但这只是删除了我键入或查看任何内容的能力。目前我正在添加内容窗格,但它似乎没有注册滚动条。此外,当它确实没有用,文本区域的其余部分是不可编辑的。这是处理的一小部分代码,你能帮我找出问题所在。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import java.util.Scanner;
import java.io.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.text.Highlighter.HighlightPainter;
import javax.swing.text.JTextComponent;
import java.net.URI;
public class MyTextEditor extends JFrame implements ActionListener
{
private JFrame frame = new JFrame();
private JPanel panel = new JPanel(new BorderLayout());
private JTextArea textArea = new JTextArea(0,0);
private JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
private MenuBar menuBar = new MenuBar();
private Menu file = new Menu();
private Menu edit = new Menu();
private Menu find = new Menu();
private Menu goTo = new Menu();
private Menu help = new Menu();
private Menu prefs = new Menu();
//File
private MenuItem openFile = new MenuItem();
private MenuItem saveFile = new MenuItem();
private MenuItem closeFile = new MenuItem();
private MenuItem newFile = new MenuItem();
//Edit
private MenuItem copy = new MenuItem();
private MenuItem paste = new MenuItem();
private MenuItem cut = new MenuItem();
private MenuItem search = new MenuItem();
private MenuItem replace = new MenuItem();
//GoTo
private MenuItem goToLine = new MenuItem();
//Help
private MenuItem documentation = new MenuItem();
//Prefs
private MenuItem toggleLines = new MenuItem();
public MyTextEditor()
{
//Set the basis for the text editor
this.setSize(750,800);
frame.setTitle("Zenith");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea.setFont(new Font("Consolas", Font.BOLD, 14));
textArea.setForeground(Color.GREEN);
textArea.setBackground(Color.BLACK);
textArea.setCaretColor(Color.WHITE);
//scrollPane.setBounds(20, 30, 100, 40);
textArea.getCaret().setVisible(true);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(20,50));
//scrollPane.add(textArea);
scrollPane.setVisible(true);
//scrollPane.getHorizontalScrollBar().isVisible();
//scrollPane.getVerticalScrollBar().isVisible();
//textArea.add(scrollPane,BorderLayout.EAST);
// Line numbers
final LineNumberingTextArea lineNTA = new LineNumberingTextArea(textArea);
DocumentListener documentListen = new DocumentListener()
{
public void insertUpdate(DocumentEvent documentEvent)
{
lineNTA.updateLineNumbers();
}
public void removeUpdate(DocumentEvent documentEvent)
{
lineNTA.updateLineNumbers();
}
public void changedUpdate(DocumentEvent documentEvent)
{
lineNTA.updateLineNumbers();
}
};
textArea.getDocument().addDocumentListener(documentListen);
lineNTA.setBackground(Color.BLACK);
lineNTA.setForeground(Color.WHITE);
lineNTA.setFont(new Font("Consolas", Font.BOLD, 13));
lineNTA.setEditable(false);
lineNTA.setVisible(true);
getContentPane().add(lineNTA,BorderLayout.WEST);
getContentPane().add(scrollPane);
getContentPane().add(textArea);
//getContentPane().setLayout(new BorderLayout());
//scrollPane.setRowHeaderView(lineNTA);
//frame.getContentPane().add(textArea);
//panel.add(lineNumber,BorderLayout.EAST);
//Numbers along the side
setMenuBar(this.menuBar);
menuBar.add(this.file);
menuBar.add(this.edit);
menuBar.add(this.find);
menuBar.add(this.goTo);
menuBar.add(this.help);
menuBar.add(this.prefs);
file.setLabel("File");
find.setLabel("Find");
edit.setLabel("Edit");
goTo.setLabel("Goto");
help.setLabel("Help");
prefs.setLabel("Preferences");
3
I see several issues with your code, but the main one being that you appear to be trying to add the JTextArea to the GUI two or maybe even three times:
我看到你的代码有几个问题,但主要的是你似乎试图将JTextArea添加到GUI两次甚至三次:
You first add it to the JScrollPane:
首先将其添加到JScrollPane:
private JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
And add that JScrollPane to the GUI
并将JScrollPane添加到GUI
getContentPane().add(scrollPane);
But then immediately after, you appear to add just the JTextArea without its JScrollPane to the GUI:
但是之后不久,您似乎只将JTextArea添加到GUI而没有JScrollPane:
getContentPane().add(textArea);
Understand that you can add a component to the GUI only once. If you try to add it a 2nd time, it is removed from the prior container, leaving it empty. Not only that, by adding a component to a BorderLayout-using container in this way (the content pane), you replace the JScrollPane from the GUI completely.
了解您只能将组件添加到GUI一次。如果您尝试第二次添加它,它将从之前的容器中删除,将其留空。不仅如此,通过以这种方式(内容窗格)向BorderLayout使用容器添加组件,您可以完全从GUI替换JScrollPane。
You also might be adding the JTextArea to the GUI a third time when you add it to your LineNumberingTextArea constructor:
当您将JTextArea添加到LineNumberingTextArea构造函数时,也可能第三次将JTextArea添加到GUI:
final LineNumberingTextArea lineNTA = new LineNumberingTextArea(textArea);
and then add the LineNumberingTextArea object to the GUI:
然后将LineNumberingTextArea对象添加到GUI:
getContentPane().add(lineNTA,BorderLayout.WEST);
We don't know what goes on inside of your LineNumberingTextArea, but if you're adding the JTextArea to the LineNumberingTextArea instance, and then as above are adding your LineNumberingTextArea instance to the GUI, again that makes three times that you're trying to add the JTextArea to a container, which is two times too many.
我们不知道你的LineNumberingTextArea里面发生了什么,但是如果你要将JTextArea添加到LineNumberingTextArea实例,然后如上所述将你的LineNumberingTextArea实例添加到GUI,那么再次使你想要尝试三次将JTextArea添加到容器中,这是两倍太多。
Again, you can only add a component to the GUI once.
同样,您只能将一个组件添加到GUI一次。
Other side issues:
其他方面的问题:
您正在尝试设置组件的大小或首选大小,这是您希望避免的。相反,让组件和布局管理器将自己调整到最佳尺寸。
private JTextArea textArea = new JTextArea(0,0);
. Much better to use this constructor as a more robust way of having the JTextArea size itself appropriately by passing in more reasonable values.您将JTextArea行和列属性设置为太小的值0,0:private JTextArea textArea = new JTextArea(0,0);.使用这个构造函数更好的方法是通过传递更合理的值来更好地使JTextArea大小本身。
您还要以默认方式多次向BorderLayout使用容器(contentPane)添加组件,这意味着您将使用新添加的组件替换先前添加的任何组件,这可能会让您更加困惑。
你正在创建两个JFrame - 为什么?一个是你创建MyTextEditor实例的时候,因为它是从JFrame扩展的。这是获取所有组件的JFrame。另一个是私人领域:
private JFrame frame = new JFrame();
and you call methods on it:
你在上面调用方法:
frame.setTitle("Zenith");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but never add anything to it or display it. Since this will serve no purpose other than to confuse, you should get rid of one JFrame or the other. Again this suggests that you might want to start over and start cleaner and with a well-defined purpose.
但永远不要添加任何东西或显示它。因为除了混淆之外没有任何其他目的,你应该摆脱一个JFrame或另一个。这再次暗示您可能希望重新开始并以明确的目的开始清洁。
For example, a simple GUI demo:
例如,一个简单的GUI演示:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
public class SimpleScrollPaneGui extends JPanel {
private static final int ROWS = 25;
private static final int COLUMNS = 60;
private static final Color TA_BACKGRND = Color.BLACK;
private static final Color TA_FOREGRND = Color.GREEN;
private static final Color TA_CARET_COLOR = Color.WHITE;
private static final Font TA_FONT = new Font("Consolas", Font.BOLD, 14);
// first off size your JTextArea by setting visible rows and columns
private JTextArea textArea = new JTextArea(ROWS, COLUMNS);
public SimpleScrollPaneGui() {
// set the text area properties
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBackground(TA_BACKGRND);
textArea.setForeground(TA_FOREGRND);
textArea.setCaretColor(TA_CARET_COLOR);
textArea.setFont(TA_FONT);
// create jscrollpane, passing in jtextarea into its viewport:
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setLayout(new BorderLayout());
// add **just the jscrollpane to the GUI
add(scrollPane);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
private static void createAndShowGui() {
SimpleScrollPaneGui mainPanel = new SimpleScrollPaneGui();
JFrame frame = new JFrame("Simple ScrollPane GUI");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2016/03/09/acc101da13c1beb120ba2ec7e3699aa.html。