對我而言,這是第一篇文章。我是科學學士學位第一年的學生。Java - JFrame根本不顯示其內容
我只是爲一個任務創建了一個基本的圖形用戶界面,並且由於某種原因,我無法看到我的生活,我創建的JFrames之一,Register Class的一個實例在被調用時顯示爲完全空白從登錄類中的註冊按鈕動作監聽器...
我也有一個單獨的主類,它包含Main方法並調用Login類。 Login Class JFrame正常工作,如前所述,只有在Login Class'Register按鈕動作偵聽器中調用Register Class時纔會出現問題。程序中的所有其他JFrames也可以正常工作。
我試圖直接從main調用Register Class,它有相同的問題,儘管我試圖將其減少到最基本的形式,並且它仍然不顯示任何空白的無色JFrame。
這裏是代碼(未完成但按原樣工作)。我爲我的sl ap道歉,但我是一個完整的初學者。
任何人都可以看到它有什麼問題嗎?
在此先感謝!
package guitest;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Register extends JFrame{
JButton regSubmit = new JButton("Submit");
JTextField email = new JTextField();
JTextField name = new JTextField();
JTextField Address1 = new JTextField();
JTextField Address2 = new JTextField();
JPasswordField password1 = new JPasswordField();
JPasswordField password2 = new JPasswordField();
String nameTxt = email.getText();
String passTxt = password1.getText();
String info = "";
FlowLayout layout1 = new FlowLayout();
public void Reg(){
this.setTitle("La Volpe Registration");
this.setLayout(layout1);
this.add(Address1);
this.add(Address2);
this.add(email);
this.add(password1);
this.add(password2);
this.add(name);
this.add(regSubmit);
this.getContentPane().setBackground(Color.green);
this.setSize(370, 160);
regSubmit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
regSubmitActionPerformed(evt);
}
private void regSubmitActionPerformed(java.awt.event.ActionEvent evt) {
String name = email.getText();
String pass = password1.getText();
String info = "";
System.out.println("registering...");
boolean eof;
try{
// Create file
FileWriter file = new FileWriter("\\regdata.txt");
BufferedWriter out = new BufferedWriter(file);
out.write("\n"+nameTxt+", "+passTxt);
}
catch (Exception e){
}
}
});
this.setVisible(true);
}
}
並鏈接到它的類...
package guitest;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
/**
* @author david
*/
public class Login {
JFrame loginFrame = new JFrame();
Register reg3 = new Register();
JButton submit = new JButton("Submit");
JButton clear = new JButton("Clear");
JButton register = new JButton ("Register with Us");
JPasswordField pass = new JPasswordField(20);
JTextField email = new JTextField(20);
JLabel em = new JLabel("Email Address: ");
JLabel pw = new JLabel("Password: ");
String pathname;
String line;
String [] records = new String [1000];
int count = 0;
FlowLayout layout1 = new FlowLayout();
public Login(){
//Adds Email label and text field
loginFrame.add(em);
loginFrame.add(email);
//Adds password label and field
loginFrame.add(pw);
loginFrame.add(pass);
//Adds buttons
loginFrame.add(submit);
loginFrame.add(clear);
loginFrame.add(register);
loginFrame.getContentPane().setBackground(Color.green);
loginFrame.setLayout(layout1);
loginFrame.setSize(370, 160);
loginFrame.setTitle("La Volpe - Login");
submit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
submitActionPerformed(evt);
}
private void submitActionPerformed(java.awt.event.ActionEvent evt){
String emailadd = email.getText();
String password = pass.getText();
pathname = "\\regdata.txt";
boolean eof;
try{
FileReader file = new FileReader(pathname);
BufferedReader buff = new BufferedReader(file);
eof = false; // set the eof boolean to false
while (!eof){
line = buff.readLine();
if (line == null){ // test to see if the end of file has been reached
eof = true; // if the end of file has been found set the eof Boolean to true
}
else{
// end of file not reached so move the contents of the line to the records
//array
records[count] = line;
count ++;
System.out.println(line); // print out the new line input for error checking
}
}
buff.close();
}
catch (IOException e){
System.out.println("Error -- "+ e.toString());
}
boolean notTrue = false;
for (int i = 0; i < count; i++) {
if ((!notTrue)&&((emailadd + "," + password).equals(records[i]))) {
FoodSelectionMain loggedIn = new FoodSelectionMain();
loggedIn.setVisible(true);
}
}
if (!notTrue){
JOptionPane.showInputDialog("Please check your login "
+ "and try again. If you are a new user, please "
+ "register by pressing the 'REGISTER' button");
}
}
});
// TODO add your handling code here:
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
public void clearActionPerformed(java.awt.event.ActionEvent evt){
email.setText(null);
pass.setText(null);
}
});
register.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
registerActionPerformed(evt);
}
public void registerActionPerformed(java.awt.event.ActionEvent evt){
reg3.setVisible(true);
System.out.println("Register pressed");
}
});
loginFrame.setVisible(true);
}
}
沒有提及'SwingUtilities',沒有調用'pack()'。我不驚訝它失敗了。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –
我非常喜歡創造性地使用縮進和空格,但是這種相同的創意使得其他人(即我們這些想要幫助你的人)很難輕鬆閱讀和理解你的代碼。畢竟,你是在尋求陌生人的免費建議,所以我們不應該讓你放棄一點點努力,以免讓我們難以幫助你。祝你好運。 –
請參閱[使用多個JFrames,好/壞實踐?](http:// stackoverflow。com/a/9554657/418556) –