gpt4 book ai didi

java - 在Java(JDK 7)中制作类似记事本的应用程序,无法使jtextarea自动滚动。有人能帮我吗?

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:52 25 4
gpt4 key购买 nike

这是所有代码,您可以将其复制并粘贴到您的 Java 编程软件中。我是在 Eclipse 中开发的。粘贴您修改/添加的行。

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.DefaultCaret;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Notepad extends JFrame implements ActionListener {

/**
*
*/
private static final long serialVersionUID = 1L;
private JMenuBar menuBar;
private JMenu menuFile;
private JMenu menuEdit;
private JMenuItem open;
private JMenuItem save;
private JMenuItem exit;
private JMenuItem copy;
private JMenuItem paste;

private JTextArea page;

Notepad(){

super("Notepad");
init();
this.setSize(1024, 768);
this.setVisible(true);
}//end Notepad

private void init(){

//create text area
page = new JTextArea();
page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());
page.setEditable(true);
page.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
page.setLineWrap(true);

//create menus
menuFile = new JMenu("File");
menuEdit = new JMenu("Edit");

//create menu items
open = new JMenuItem("Open");
save = new JMenuItem("Save");
exit = new JMenuItem("Exit");
copy = new JMenuItem("Copy");
paste = new JMenuItem("Paste");

//add listener to menu items
exit.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
save.addActionListener(this);
open.addActionListener(this);

//add items to menus
menuFile.add(open);
menuFile.add(save);
menuFile.add(exit);

menuEdit.add(copy);
menuEdit.add(paste);

//create menu bar
menuBar = new JMenuBar();

//adds menus to the bar
menuBar.add(menuFile);
menuBar.add(menuEdit);


//add stuff to the window
this.setJMenuBar(menuBar);
this.add(page);


}//end init

//this method opens a file
private void openFile(){

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

int result = fileChooser.showOpenDialog(this);

if(result == JFileChooser.CANCEL_OPTION)
return;

File fileName = fileChooser.getSelectedFile();

if (fileName == null || fileName.getName().equals("")){
JOptionPane.showMessageDialog(this, "Invalid File Name", "This is an" + " invalid file name.", JOptionPane.ERROR_MESSAGE);
}//end if

else{
JOptionPane.showMessageDialog(this, fileName.getName());
BufferedReader input;
try{
input = new BufferedReader(new FileReader(fileName.getAbsolutePath()));
String line;

while((line = input.readLine()) != null){
page.append(line+"\n");
}//end while
input.close();
}//end try
catch(Exception e){
JOptionPane.showMessageDialog(this, "Error", e.toString(), JOptionPane.ERROR_MESSAGE);
}//end catch
}//end else
}//end openFile

//this method saves our data to a file
private void saveFile(){

JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

int result = fileChooser.showSaveDialog(this);

if(result == JFileChooser.CANCEL_OPTION)
return;

File fileName = fileChooser.getSelectedFile();

if (fileName == null || fileName.getName().equals("")){
JOptionPane.showMessageDialog(this, "Invalid File Name", "This is an" + " invalid file name.", JOptionPane.ERROR_MESSAGE);
}//end if
else{
BufferedWriter output;
try{
output = new BufferedWriter(new FileWriter(fileName.getAbsolutePath()));
output.write(page.getText(), 0, page.getText().length());
output.close();
}//end try
catch(Exception e){
JOptionPane.showMessageDialog(this, "Error", e.toString(), JOptionPane.ERROR_MESSAGE);
}//end catch
}//end else
}//end saveFile

@Override
public void actionPerformed(ActionEvent e) {
//TODO Auto-generated method stub

if (e.getSource() == exit){
this.dispose();
}//end if
else if (e.getSource() == copy){
page.copy();
}//end else if
else if (e.getSource() == paste){
page.paste();
}//end else if
else if (e.getSource() == save){
this.saveFile();
}//end else if
else{
this.openFile();
}//end else
}//end override

}//end class

我查看了其他堆栈溢出页面,它们不起作用。我尝试过的任何方法都没有效果。

最佳答案

JTextArea 包装在 JScrollPane 中,如下所示:

this.add(new JScrollPane(page));

然后根据需要添加/删除水平和垂直滚动条,并随插入符号一起滚动滚动 Pane 。

关于java - 在Java(JDK 7)中制作类似记事本的应用程序,无法使jtextarea自动滚动。有人能帮我吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24313851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com