Java 只需按一个JButton,就可以将文件输出到文本字段,需要一些帮助吗
这是我现在删除的代码,其他的不是 你现在能运行这个吗???如果你能运行这个程序并告诉我我错了,我真的很感激。这是我的决赛项目,我不知道我将如何做下一个和上一个按钮。所以我特别咨询了这个网站Java 只需按一个JButton,就可以将文件输出到文本字段,需要一些帮助吗,java,swing,user-interface,button,Java,Swing,User Interface,Button,这是我现在删除的代码,其他的不是 你现在能运行这个吗???如果你能运行这个程序并告诉我我错了,我真的很感激。这是我的决赛项目,我不知道我将如何做下一个和上一个按钮。所以我特别咨询了这个网站 import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.util.*; import java.io.*; imp
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
public class dadas implements ActionListener {
static String summary,summary2,summary3;
JFrame frame=new JFrame("Generator X");
JPanel p = new JPanel();
JLabel studentno=new JLabel("Student No:");
JLabel name= new JLabel("First name:");
JLabel lastname= new JLabel("Last Name:");
JTextField number= new JTextField();
JTextField name2= new JTextField();
JTextField lastname2= new JTextField();
JButton next=new JButton("NEXT---->");
JButton prev=new JButton("<----PREV");
JButton add=new JButton("Add");
JButton save=new JButton("Save");
public static void main(String args[]) {
new dadas();
}
public dadas(){
frame.setSize(300,500);//frame
frame.setLayout(null);
frame.setVisible(true);
frame.setResizable(false);
studentno.setBounds(30,30,80,60);//LABEL
number.setBounds(100,45,100,25);//TextField
number.addActionListener(this);
number.setDocument(new JTextFieldLimit(7));
number.setEditable(false);
name.setBounds(30,70,100,65);//LABEL
name2.setBounds(100,90,100,28);//TextField
name2.addActionListener(this);
name2.setEditable(false);
lastname.setBounds(30,115,100,65);//LABEL
lastname2.setBounds(100,133,120,28);//TextField
lastname2.addActionListener(this);
lastname2.setEditable(false);
next.setBounds(150,170,100,30);//Next Button
next.addActionListener(this);
prev.setBounds(50,170,100,30);//Previous Button
prev.addActionListener(this);
add.setBounds(50,210,100,30);
add.addActionListener(this);
save.setBounds(100,250,100,30);//Save Button
save.addActionListener(this);
frame.add(studentno);
frame.add(name);
frame.add(lastname);
frame.add(number);
frame.add(name2);
frame.add(lastname2);
frame.add(next);
frame.add(prev);
frame.add(add);
frame.add(save);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==add){
number.setEditable(true);
name2.setEditable(true);
lastname2.setEditable(true);
next.setEnabled(false);
prev.setEnabled(false);
}
else if( e.getSource()==save ){//save button
summary = (" "+number.getText()) ;
summary2 = ("" +name2.getText()) ;
summary3 = (" "+lastname2.getText());
String I = dadas.summary;
String Love = dadas.summary2;
String You = dadas.summary3;
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(newFile("C:\\lol.txt"),true));
bw.write(I);
bw.newLine();
bw.close();
JOptionPane.showMessageDialog(frame,"Your File has been Saved");
}
catch(IOException E)
{
}
}
else if(e.getSource()==next){//Next Button
try {
String filePath = "C:\\lol.txt";
BufferedReader br = new BufferedReader(new FileReader(filePath));//my file Writer
String file;
while((file = br.readLine()) != null){
number.setText(file);
lastname2.setText(file);
name2.setText(file);
}
br.close();
}catch(IOException ex) {
ex.printStackTrace();
}
}
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入java.util.*;
导入java.io.*;
导入javax.swing.JOptionPane;
公共类dadas实现ActionListener{
静态字符串摘要,摘要2,摘要3;
JFrame=新JFrame(“发电机X”);
JPanel p=新的JPanel();
JLabel studentno=新JLabel(“学生编号”);
JLabel name=新的JLabel(“名字:”);
JLabel lastname=新JLabel(“姓氏:”);
JTextField number=新的JTextField();
JTextField name 2=新的JTextField();
JTextField lastname2=新的JTextField();
JButton next=新JButton(“next-->”);
JButton prev=新JButton(关于:
try
{
String filePath = "C:\\lol.txt";
BufferedReader br = new BufferedReader(new FileReader(filePath));
String file;
while((file = br.readLine()) != null){
file = br.readLine();
number.setText(file);
lastname2.setText(file);
name2.setText(file);
br.close();
}
}catch(IOException F)
{
}
- 你仍然在忽略异常,这是你永远不应该做的。再说一次,至少打印一个堆栈跟踪
- 您正在while循环中读取文件两次。在while条件下仅读取一次
- 每次尝试读取一行时,您都将关闭BufferedReader。只有在读取整个文件后才能关闭它
- 您的代码格式仍然很差,阅读起来非常困难。请尽一点努力,不要让代码比需要的更难阅读。这并不是对您的要求太高,真的
i、 e
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filePath));
String filePath = "C:\\lol.txt";
// String file; // bad variable name
String line; // better name
while((line = br.readLine()) != null){
// file = br.readLine(); // Don't read twice
// You'll probably want to split the line in order to extract info from it
// not sure what your line looks like so I can't suggest a String to split on
String[] tokens = line.split(...); // this needs to be fixed
number.setText(tokens[0]); // ? first token in line? not sure
lastname2.setText(tokens[1]); // ? 2nd item in tokens array? not sure
name2.setText(tokens[2]); // ? 3rd item in tokens array? not sure
// br.close(); // ** no don't close the br here.
}
}catch(IOException ex) {
ex.printStackTrace(); // Always catch or throw your exceptions. Don't ignore them.
} finally {
br.close(); // better to have this in a finally block actually
}
“我需要知道如何使用这个程序。。。。"
--你在哪里找到这个程序的源代码?你能显示链接吗?另外,请花点力气来显示格式良好的代码,而不是所有左对齐的难读代码。另外,标记被删除,因为你的问题与Javascript无关。你的输入文件lol.txt
是什么样子的?还有,您永远不想这样做:catch(IOException F){}
。如果您不捕获异常,您如何知道是否发生异常或更重要的异常发生原因。至少打印stacktrace。catch(IOException ex){ex.printStackTrace()}
请澄清您的上述陈述——您管理了什么?再次请解决我的问题,因为格式良好的代码是可读的代码,它不会要求您太多,以使我们更容易阅读。此外,此代码的来源是什么?请回答这些问题,这样您的问题就不会结束。1)您的代码的格式仍然很差,都是左对齐的。您的代码应该有适当的缩进/格式,以便于阅读。2)添加printStackTrace()时
到catch块,您是否看到任何异常?并且BufferedReader br
应声明为局部变量
或在try-catch-+finally
块之前,然后br.close();
应移动到finally块
(在所有情况下都会执行),否则(在1.exception上)BufferedReader
和FileReader
保留在JVM中memory@mKorbel:谢谢你的剪辑。对不起,我几个小时前才注册,这是我的第一次Question@ChrisYao:欢迎使用stackoverflow。仍有时间编辑您的问题和后期格式化代码。再次,请立即执行此操作。@HovercraftFullOfEels是否运行这个节目?