我正在学习java,我对它很陌生。我试图使用swing创建一个静态配置文件窗口。此外,头像图片应该根据用户而变化,用户信息应该从文本文件中读取。这个代码没有给出错误,但这是因为avatarPath
。我认为程序认为它是非静态的。
import java.io.*;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author B_Ali
*/
public class ProfilePanel extends javax.swing.JFrame {
public static String avatarPath;
public static int x;
public static class UsersInfo{
public int userID;
public String userName;
public String userSurname;
public String userGender;
UsersInfo(String name, String surname, String gender){
userID = x;
userName = name;
userSurname = surname;
userGender = gender;
}
}
static UsersInfo permitedUser[];
public static void InfoReader()throws IOException{
@SuppressWarnings("UnusedAssignment")
BufferedReader in = null;
try {
in = new BufferedReader(
new FileReader("C:\Users\B_Ali\Documents\NetBeansProjects\JavaApplication20\UserInformation.txt"));
String s, s2 = new String();
while((s = in.readLine())!= null)
s2 += s + "n";
@SuppressWarnings("UnusedAssignment")
String[] s1 = new String[100];
s1 = s2.split("\s+");
in.close();
permitedUser[0] = new UsersInfo( s1[0] , s1[1], s1[2] );
permitedUser[1] = new UsersInfo ( s1[3], s1[4], s1[5] );
permitedUser[2] = new UsersInfo ( s1[6], s1[7], s1[8] );
} catch (FileNotFoundException ex) {
Logger.getLogger(ProfilePanel.class.getName()).log(Level.SEVERE, null, ex);
} finally {
}
}
public static void ProfilePanel() {
initComponents();
}
public static String AvatarPic(){
if( x == 0 ){
avatarPath = ""/Users/hei_cosplay_darker_than_black_by_seras0victoria.jpg"";
}
else if( x == 1 ){
avatarPath = ""/Users/lyralei_the_windrunner_by_trungth-d5zhmc1.jpg"";
}
else if( x == 2 ){
avatarPath = ""/Users/shingeki_no_kyojin_by_asuka10-d6k762k.jpg"";
}
return avatarPath;}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
jFrame2 = new javax.swing.JFrame();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
jFrame2.getContentPane().setLayout(jFrame2Layout);
jFrame2Layout.setHorizontalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame2Layout.setVerticalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Avatar", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(ProfilePanel.AvatarPic())));
jLabel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 2, true));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
jScrollPane2.setName(""); // NOI18N
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
try {
ProfilePanel.InfoReader(); // TODO add your handling code here:
} catch (IOException ex) {
Logger.getLogger(ProfilePanel.class.getName()).log(Level.SEVERE, null, ex);
}
jTextArea1.setText("Name: " +permitedUser[x].userName+ "rnSurname :"+permitedUser[x].userSurname+"rnGender :"+permitedUser[x].userGender);
jTextArea1.setDragEnabled(true);
jTextArea1.setMaximumSize(new java.awt.Dimension(2147483647, 120));
jTextArea1.setMinimumSize(new java.awt.Dimension(92, 120));
jTextArea1.setPreferredSize(new java.awt.Dimension(164, 120));
jScrollPane2.setViewportView(jTextArea1);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(120, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
Scanner in = new Scanner(System.in);
x = in.nextInt();
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ProfilePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProfilePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProfilePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProfilePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProfilePanel().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JFrame jFrame1;
private javax.swing.JFrame jFrame2;
public javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
public javax.swing.JScrollPane jScrollPane2;
public javax.swing.JTextArea jTextArea1;
// End of variables declaration
}
编辑:我改变了代码一点,它给出了错误。编辑2:我想做的是创建用户和他们的个人资料。个人资料将包含头像图像和3个变量是姓名,姓氏和性别。我想显示它们相对于用户
我的生活问题与这些代码;
static void AvatarPic(){
if( x == 0 ){
avatarPath = ""/Users/hei_cosplay_darker_than_black_by_seras0victoria.jpg"";
}
else if( x == 1 ){
avatarPath = ""/Users/lyralei_the_windrunner_by_trungth-d5zhmc1.jpg"";
}
else if( x == 2 ){
avatarPath = ""/Users/shingeki_no_kyojin_by_asuka10-d6k762k.jpg"";
}
}
和
ProfilePanel.AvatarPic();
jLabel1.setIcon(new
javax.swing.ImageIcon(getClass().getResource(ProfilePanel.avatarPath)));
你的代码有很多问题。
-
initComponents()
不是一个静态函数。我猜在你的静态转换中,你只是用公共静态代替了每个函数。下面是一个构造函数[我敢肯定,因为它是一个Netbeans
生成的],你也修改了它。不需要那样做public static void ProfilePanel() { initComponents(); }
你可以把它改回原来的样子
public ProfilePanel() { initComponents(); }
-
你会得到一个
java.lang.NullPointerException
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(ProfilePanel.AvatarPic())));
,因为如果你使用getResource,你只需要指定相对于包的文件名。如果你要从其他位置加载,那么你就必须修改为
jLabel1.setIcon(new javax.swing.ImageIcon(ProfilePanel.AvatarPic()));//file location
你还必须给出绝对路径
avatarPath = "D://hei_cosplay_darker_than_black_by_seras0victoria.jpg";
-
您将再次获得
java.lang.NullPointerException
permitedUser[0] = new UsersInfo(s1[0], s1[1], s1[2]);
,因为您没有在任何地方初始化
这样写permitedUser
。所以你必须像static UsersInfo permitedUser[]=new UsersInfo[3];
在做了这些改变之后,如果你用0运行它,那么BK201[代号Hei]将出现在你的屏幕上
和你编辑的FullCode[Make change to ImageLocations]
import java.io.*;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author B_Ali
*/
public class ProfilePanel extends javax.swing.JFrame {
public static String avatarPath;
public static int x;
public static class UsersInfo {
public int userID;
public String userName;
public String userSurname;
public String userGender;
UsersInfo(String name, String surname, String gender) {
userID = x;
userName = name;
userSurname = surname;
userGender = gender;
}
}
static UsersInfo permitedUser[] = new UsersInfo[3];//change 3
public static void InfoReader() throws IOException {
@SuppressWarnings("UnusedAssignment")
BufferedReader in = null;
try {
in = new BufferedReader(
new FileReader("C:\Users\B_Ali\Documents\NetBeansProjects\JavaApplication20\UserInformation.txt"));
String s, s2 = new String();
while ((s = in.readLine()) != null) {
s2 += s + "n";
}
@SuppressWarnings("UnusedAssignment")
String[] s1 = new String[100];
s1 = s2.split("\s+");
in.close();
permitedUser[0] = new UsersInfo(s1[0], s1[1], s1[2]);
permitedUser[1] = new UsersInfo(s1[3], s1[4], s1[5]);
permitedUser[2] = new UsersInfo(s1[6], s1[7], s1[8]);
} catch (FileNotFoundException ex) {
Logger.getLogger(ProfilePanel.class.getName()).log(Level.SEVERE, null, ex);
} finally {
}
}
//Change 1
public ProfilePanel() {
initComponents();
}
public static String AvatarPic() {
//change 2
if (x == 0) {
avatarPath = "D://hei_cosplay_darker_than_black_by_seras0victoria.jpg";
} else if (x == 1) {
avatarPath = "D://lyralei_the_windrunner_by_trungth-d5zhmc1.jpg";
} else if (x == 2) {
avatarPath = "D://shingeki_no_kyojin_by_asuka10-d6k762k.jpg";
}
return avatarPath;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFrame1 = new javax.swing.JFrame();
jFrame2 = new javax.swing.JFrame();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
javax.swing.GroupLayout jFrame2Layout = new javax.swing.GroupLayout(jFrame2.getContentPane());
jFrame2.getContentPane().setLayout(jFrame2Layout);
jFrame2Layout.setHorizontalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame2Layout.setVerticalGroup(
jFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Avatar", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
//Change 2
jLabel1.setIcon(new javax.swing.ImageIcon(ProfilePanel.AvatarPic()));
jLabel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 2, true));
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Information", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 12))); // NOI18N
jScrollPane2.setName(""); // NOI18N
jTextArea1.setEditable(false);
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
try {
ProfilePanel.InfoReader(); // TODO add your handling code here:
} catch (IOException ex) {
Logger.getLogger(ProfilePanel.class.getName()).log(Level.SEVERE, null, ex);
}
jTextArea1.setText("Name: " + permitedUser[x].userName + "rnSurname :" + permitedUser[x].userSurname + "rnGender :" + permitedUser[x].userGender);
jTextArea1.setDragEnabled(true);
jTextArea1.setMaximumSize(new java.awt.Dimension(2147483647, 120));
jTextArea1.setMinimumSize(new java.awt.Dimension(92, 120));
jTextArea1.setPreferredSize(new java.awt.Dimension(164, 120));
jScrollPane2.setViewportView(jTextArea1);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(120, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
Scanner in = new Scanner(System.in);
x = in.nextInt();
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ProfilePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProfilePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProfilePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProfilePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProfilePanel().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JFrame jFrame1;
private javax.swing.JFrame jFrame2;
public javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
public javax.swing.JScrollPane jScrollPane2;
public javax.swing.JTextArea jTextArea1;
// End of variables declaration
}