所以我试图在Java中为学校制作一种平面文件数据库。要输入学生信息,我使用Jbuttons。我的问题是按钮是彼此重叠的,你不能点击。代码:
package hsqi;
import java.util.Random;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import static java.lang.System.out;
public class PopUp {
public static void main(final String[] args) {
String hsqi = "School";
File fl = new File(hsqi);
if (!fl.exists()) {
System.out.println("Creating... ");
fl.mkdir();
} else {
System.out.println("Main File already exists!");
}
Random rand = new Random();
int value = rand.nextInt(10000);
System.out.println("Student ID is: " + value);
final JFrame parent = new JFrame();
JButton button = new JButton();
button.setText("Add Student");
parent.add(button);
parent.pack();
parent.setVisible(true);
button.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
String name = JOptionPane.showInputDialog(parent, "Student Name: ", null);
String flName = "C:\\Users\\Saeed\\Workspace1\\Database\\School\\" + name;
File newFile = new File(flName);
if(!newFile.exists()){
System.out.println("Creating user Profile...");
newFile.mkdir();
}else{
System.out.println("Profile already exists!");
}
PrintWriter writer;
try {
writer = new PrintWriter(flName);
writer.println(name);
writer.close();
} catch (Exception e) {
}
JButton button = new JButton();
button.setText("Add Student");
parent.add(button);
parent.pack();
parent.setVisible(true);
button.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
String Grade = JOptionPane.showInputDialog(parent, "Grade: ", null);
String fname = "C:\\Users\\Saeed\\Workspace1\\Database\\School\\" + name + Grade ;
File newnewFile = new File(fname);
if(newnewFile.exists()){
System.out.println("Creating user Profile...");
}else{
System.out.println("Profile already exists!");}
PrintWriter writer;
try {
writer = new PrintWriter(fname);
writer.println(Grade);
writer.close();
} catch (Exception e) {}
}
});
}
});
}
}