【发布时间】:2016-05-30 12:00:08
【问题描述】:
我还是java新手,所以愚蠢的问题来了。我一直在使用JFrame、PrintWriter、File 和Scanner 开发一个简单的软件来创建和读取文本文件,我使用您输入的名称保存文件,然后使用您输入的数据进入JTextField,问题是:只要你输入一个空格,它就不会将空格后面的文本保存到.txt文件中:
输入
等一下
WriteToFile(textarea.getText(), name.getText());
// my function input text name
输出:
等待
但如果我以这种方式手动输入文本:
WriteToFile("waitFor it", name.getText());
// my function input text name
输出:
等一下
这让我认为我的功能可能不会导致这种情况,但我还是个菜鸟。
Main.java
package creator;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import library.Database;
public class Main extends Database{
public static void main(String[] args) {
JFrame frame = new JFrame();
JButton button = new JButton("Erase");
JButton button2 = new JButton("Add");
JButton button3 = new JButton("Save to Database");
Font font = new Font("Courier", Font.BOLD, 12);
Font font2 = new Font("Courier", Font.BOLD, 14);
final JTextField name = new JTextField();
final JTextField editorPane = new JTextField();
final JTextField textarea = new JTextField();
final JScrollPane scroll = new JScrollPane (textarea,
JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.setTitle("Array Creator");
frame.setSize(401, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setLayout(null);
frame.add(button);
frame.add(name);
frame.add(button2);
frame.add(button3);
frame.add(editorPane);
frame.add(scroll);
button.setBounds(0, 200, 80, 22);
name.setBounds(82, 200, 80, 22);
button2.setBounds(164, 200, 80, 22);
button3.setBounds(246, 200, 148, 22);
editorPane.setBounds(100,175,200,18);
scroll.setBounds(50, 10, 300, 160);
textarea.setEditable(false);
button.setFont(font);
name.setFont(font);
button2.setFont(font);
button3.setFont(font);
editorPane.setFont(font);
textarea.setFont(font2);
textarea.setText("[");
frame.setVisible(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textarea.setText("[");
editorPane.setText("");
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textarea.setText(textarea.getText()+"["+editorPane.getText()+"],");
editorPane.setText("");
}
});
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String temp = textarea.getText();
temp = temp.substring(0, temp.length()-1);
textarea.setText(temp+"]");
editorPane.setText("");
WriteToFile(textarea.getText(), name.getText());
}
});
name.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent arg0) {
}
public void keyReleased(KeyEvent arg0) {
textarea.setText(readFile(name.getText()));
}
public void keyPressed(KeyEvent arg0) {
}
});
}
}
Database.java
package library;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class Database {
public static String WriteToFile(String data, String name){
String output = "ERROR";
PrintWriter writer = null;
try {
writer = new PrintWriter(name+".txt", "UTF-8");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
writer.println(data);
writer.close();
File file = new File(name+".txt");
try {
@SuppressWarnings("resource")
Scanner sc = new Scanner(file);
output = sc.next();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return output;
}
public static String readFile(String name){
String output = "[";
File file = new File(name+".txt");
try {
@SuppressWarnings("resource")
Scanner sc = new Scanner(file);
output = sc.next();
} catch (FileNotFoundException e) {
}
return output;
}
}
有人可以解释一下这是为什么吗?
【问题讨论】:
-
而不是
output = sc.next();使用output = sc.nextLine();。 -
避免使用
null布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正 -
KeyListener不是使用文本组件的最佳选择,相反,您应该使用ActionListener来监视 [Enter] 键或使用DocumentLstener来监听文档的更改或DocumentFilter过滤字段将接受的内容 -
虽然这与问题无关,但我建议您阅读oracle.com/technetwork/java/codeconventions-135099.html - 您的方法应以小写字母开头。
-
@Mr.Derpinthoughton 因为,坦率地说,有很多因素会影响组件的大小,您“可能”认为您正在获得控制权并且它“看起来”不错,但是当屏幕和/或视频卡和/或分辨率和/或字体大小和/或 DPI 发生变化时,所有“漂亮”的布局都会变成垃圾。高效的 UI 布局实际上是相当复杂的,没有“真正的”解决方案,但有很多可能性,Swing 提供的布局管理器 API 只是其中一种。 iOS 刚刚经历了 4 年的“重新设计”,引入了自动布局,因为它们最初只有两种屏幕尺寸:P