关于Java的问题
publicstaticvoidadder()throwsIOException{FileOutputStreamtestfile=newFileOutputStream...
public static void adder() throws IOException{
FileOutputStream testfile = new FileOutputStream("C:\\data.txt");
testfile.write(new String("").getBytes());
for(int n=1;n<=5;n++){
String a=Input.infln("请输入第"+n+"位学生成绩", "输入不能为空!");
BufferedWriter bw = new BufferedWriter(new FileWriter("C:/data.txt",true));
bw.newLine();
bw.append(a);
bw.close();
} }
它提示说String a=Input.infln("请输入第"+n+"位学生成绩", "输入不能为空!");input无法解析,要怎么改啊
PS:只是截取了错了部分的代码 展开
FileOutputStream testfile = new FileOutputStream("C:\\data.txt");
testfile.write(new String("").getBytes());
for(int n=1;n<=5;n++){
String a=Input.infln("请输入第"+n+"位学生成绩", "输入不能为空!");
BufferedWriter bw = new BufferedWriter(new FileWriter("C:/data.txt",true));
bw.newLine();
bw.append(a);
bw.close();
} }
它提示说String a=Input.infln("请输入第"+n+"位学生成绩", "输入不能为空!");input无法解析,要怎么改啊
PS:只是截取了错了部分的代码 展开
2个回答
展开全部
设置两个事件
1、panel的鼠标点击事件
2、工号输入框的失去焦点事件
参考代码如下:
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class test extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public JPanel contentPanel;
public JLabel JlNo;
public JTextField JtNo;
public JLabel Jlname;
public JTextField Jtname;
static int x, y,px, py,bx,by,offx,offy; //
/**
* @param args
*/
public void initialize() {
this.setSize(new Dimension(400, 100));
this.setTitle("");
this.setContentPane(getContentPanel());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocation(200, 100);
this.setVisible(true);
}
public JPanel getContentPanel() {
if (contentPanel == null) {
contentPanel=new JPanel();
contentPanel.setLayout(null);
JlNo=new JLabel("工号:");
JlNo.setBounds(new Rectangle(10,10,60,20));
contentPanel.add(JlNo);
JtNo=new JTextField();
JtNo.setBounds(new Rectangle(80,10,100,20));
contentPanel.add(JtNo);
JtNo.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
//查询数据库(略)
//设置信息
Jtname.setText("失去焦点");
}
@Override
public void focusGained(FocusEvent e) {
}
});
contentPanel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
//查询数据库(略)
//设置信息
Jtname.setText("点击面板");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
//Jtname.setText("name");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
Jlname=new JLabel("名字:");
Jlname.setBounds(new Rectangle(200,10,60,20));
contentPanel.add(Jlname);
Jtname=new JTextField();
Jtname.setBounds(new Rectangle(280,10,100,20));
contentPanel.add(Jtname);
}
return contentPanel;
}
public static void main(String[] args) {
new test().initialize();
}
}
1、panel的鼠标点击事件
2、工号输入框的失去焦点事件
参考代码如下:
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class test extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public JPanel contentPanel;
public JLabel JlNo;
public JTextField JtNo;
public JLabel Jlname;
public JTextField Jtname;
static int x, y,px, py,bx,by,offx,offy; //
/**
* @param args
*/
public void initialize() {
this.setSize(new Dimension(400, 100));
this.setTitle("");
this.setContentPane(getContentPanel());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocation(200, 100);
this.setVisible(true);
}
public JPanel getContentPanel() {
if (contentPanel == null) {
contentPanel=new JPanel();
contentPanel.setLayout(null);
JlNo=new JLabel("工号:");
JlNo.setBounds(new Rectangle(10,10,60,20));
contentPanel.add(JlNo);
JtNo=new JTextField();
JtNo.setBounds(new Rectangle(80,10,100,20));
contentPanel.add(JtNo);
JtNo.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
//查询数据库(略)
//设置信息
Jtname.setText("失去焦点");
}
@Override
public void focusGained(FocusEvent e) {
}
});
contentPanel.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
//查询数据库(略)
//设置信息
Jtname.setText("点击面板");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
//Jtname.setText("name");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
Jlname=new JLabel("名字:");
Jlname.setBounds(new Rectangle(200,10,60,20));
contentPanel.add(Jlname);
Jtname=new JTextField();
Jtname.setBounds(new Rectangle(280,10,100,20));
contentPanel.add(Jtname);
}
return contentPanel;
}
public static void main(String[] args) {
new test().initialize();
}
}
追问
呃~好复杂啊,看不懂,初学者能简单点说吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询