如何显示图像和不同的变量取决于此代码中的某些条件?
How can I display images and different variables depends on some conditions in this code?
我正在学习 java,我对此还很陌生。我正在尝试使用 swing 创建静态配置文件 window。此外,头像图片应根据用户而变化,用户信息应从文本文件中读取。此代码没有给出错误,但它是因为 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+ "\r\nSurname :"+permitedUser[x].userSurname+"\r\nGender :"+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
}
编辑:我稍微更改了代码,但出现错误。
Edit2:我想做的是创建用户和他们的个人资料。个人资料将包含头像图像和 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()
不是静态函数。我想在你的静态转换中你只是用 public 静态替换了每个函数。下面是一个构造函数 [I'我确定,因为它是 Netbeans
生成的] 并且您已经修改了它 also.No 需要这样做
public static void ProfilePanel() {
initComponents();
}
您可以将其改回原来的样子
public ProfilePanel() {
initComponents();
}
您将在
获得 java.lang.NullPointerException
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(ProfilePanel.AvatarPic())));
因为如果您使用 getResource you just have to specify the filename relative to the package.If you'll be loading from other location,then you have to change it like
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
anywhere.So 你必须这样做
static UsersInfo permitedUser[]=new UsersInfo[3];
进行这些更改后,如果您 运行 将其设置为 0,则 BK201[代号 Hei] 将出现在您的屏幕上
和您编辑的完整代码[更改 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 + "\r\nSurname :" + permitedUser[x].userSurname + "\r\nGender :" + 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
}
我正在学习 java,我对此还很陌生。我正在尝试使用 swing 创建静态配置文件 window。此外,头像图片应根据用户而变化,用户信息应从文本文件中读取。此代码没有给出错误,但它是因为 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+ "\r\nSurname :"+permitedUser[x].userSurname+"\r\nGender :"+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
}
编辑:我稍微更改了代码,但出现错误。 Edit2:我想做的是创建用户和他们的个人资料。个人资料将包含头像图像和 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()
不是静态函数。我想在你的静态转换中你只是用 public 静态替换了每个函数。下面是一个构造函数 [I'我确定,因为它是Netbeans
生成的] 并且您已经修改了它 also.No 需要这样做public static void ProfilePanel() { initComponents(); }
您可以将其改回原来的样子
public ProfilePanel() { initComponents(); }
您将在
获得java.lang.NullPointerException
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource(ProfilePanel.AvatarPic())));
因为如果您使用 getResource you just have to specify the filename relative to the package.If you'll be loading from other location,then you have to change it like
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
anywhere.So 你必须这样做static UsersInfo permitedUser[]=new UsersInfo[3];
进行这些更改后,如果您 运行 将其设置为 0,则 BK201[代号 Hei] 将出现在您的屏幕上
和您编辑的完整代码[更改 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 + "\r\nSurname :" + permitedUser[x].userSurname + "\r\nGender :" + 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
}