gpt4 book ai didi

java - 在 JFrame 中显示外部文件内容的问题

转载 作者:行者123 更新时间:2023-12-02 11:08:35 26 4
gpt4 key购买 nike

我正在为我的大学作业编写 Java 程序,但有些东西不起作用。我可以被认为是 Java 新手,因为我没有太多编码经验。

package javaassignment;

import java.io.*;
import java.util.*;

public class Main extends javax.swing.JFrame

{
private FileInputStream in;
static String name;
Scanner file;

public Main(String filename) throws FileNotFoundException
{
in = new FileInputStream(filename);
try
{
file=new Scanner(new File("c:/users/MSI-USER/desktop/JavaAssignment/Student.txt"));
}
catch(FileNotFoundException ea)
{
ea.printStackTrace();
}
}
public void Read()
{
do
{
name=file.nextLine();
}
while(file.hasNext());
System.out.println(name);
file.close();
}


public String getWord() throws IOException
{
int c;
StringBuffer buf = new StringBuffer();

do
{
c = in.read();
if (Character.isSpace((char) c))
return buf.toString();
else
buf.append((char) c);
} while (c != -1);

return buf.toString();
}

public Main()
{
ComboPanel.add(NameText);
NameText.setEditable(false);
NameText.setText(name);
initComponents();
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

ButtonPanel = new javax.swing.JPanel();
PrintButton = new javax.swing.JButton();
SaveButton = new javax.swing.JButton();
CalcButton = new javax.swing.JButton();
LabelPanel = new javax.swing.JPanel();
StudentLabel = new javax.swing.JLabel();
AttendanceLabel = new javax.swing.JLabel();
ScrollPanel = new javax.swing.JPanel();
jScrollBar1 = new javax.swing.JScrollBar();
ComboPanel = new javax.swing.JPanel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

PrintButton.setText("Print");
PrintButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PrintButtonActionPerformed(evt);
}
});

SaveButton.setText("Save");
SaveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SaveButtonActionPerformed(evt);
}
});

CalcButton.setText("Calculate Percentage");
CalcButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CalcButtonActionPerformed(evt);
}
});

javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
ButtonPanel.setLayout(ButtonPanelLayout);
ButtonPanelLayout.setHorizontalGroup(
ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ButtonPanelLayout.createSequentialGroup()
.addGap(43, 43, 43)
.addComponent(SaveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(PrintButton, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(CalcButton)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
ButtonPanelLayout.setVerticalGroup(
ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(ButtonPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(PrintButton, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
.addComponent(CalcButton, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
.addComponent(SaveButton, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))
.addGap(21, 21, 21))
);

StudentLabel.setText("Student");

AttendanceLabel.setText("Attendance Status");

javax.swing.GroupLayout ComboPanelLayout = new javax.swing.GroupLayout(ComboPanel);
ComboPanel.setLayout(ComboPanelLayout);
ComboPanelLayout.setHorizontalGroup(
ComboPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
ComboPanelLayout.setVerticalGroup(
ComboPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);

javax.swing.GroupLayout ScrollPanelLayout = new javax.swing.GroupLayout(ScrollPanel);
ScrollPanel.setLayout(ScrollPanelLayout);
ScrollPanelLayout.setHorizontalGroup(
ScrollPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ScrollPanelLayout.createSequentialGroup()
.addComponent(ComboPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
ScrollPanelLayout.setVerticalGroup(
ScrollPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 460, Short.MAX_VALUE)
.addComponent(ComboPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);

javax.swing.GroupLayout LabelPanelLayout = new javax.swing.GroupLayout(LabelPanel);
LabelPanel.setLayout(LabelPanelLayout);
LabelPanelLayout.setHorizontalGroup(
LabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(LabelPanelLayout.createSequentialGroup()
.addComponent(StudentLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(AttendanceLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
.addComponent(ScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
LabelPanelLayout.setVerticalGroup(
LabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(LabelPanelLayout.createSequentialGroup()
.addGroup(LabelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(AttendanceLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
.addComponent(StudentLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(LabelPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(LabelPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);

pack();
}// </editor-fold>

private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void PrintButtonActionPerformed(java.awt.event.ActionEvent evt) {

}

private void CalcButtonActionPerformed(java.awt.event.ActionEvent evt) {
JavaAssignment J=new JavaAssignment();
PercentageDialog P=new PercentageDialog();
P.setVisible(true);

}




public static void main(String args[]) throws java.io.IOException
{




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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (InstantiationException ex)
{
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (IllegalAccessException ex)
{
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
catch (javax.swing.UnsupportedLookAndFeelException ex)
{
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}

java.awt.EventQueue.invokeLater(() -> {
new Main().setVisible(true);
});
}
private javax.swing.JTextField NameText;

// Variables declaration - do not modify
private javax.swing.JLabel AttendanceLabel;
private javax.swing.JPanel ButtonPanel;
private javax.swing.JButton CalcButton;
private javax.swing.JPanel ComboPanel;
private javax.swing.JPanel LabelPanel;
private javax.swing.JButton PrintButton;
private javax.swing.JButton SaveButton;
private javax.swing.JPanel ScrollPanel;
private javax.swing.JLabel StudentLabel;
private javax.swing.JScrollBar jScrollBar1;
// End of variables declaration
}

我想在 JFrame 中显示外部文件的内容,但是当我运行该程序时,它一直给我这个错误。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javaassignment.Main.<init>(Main.java:57)
at javaassignment.Main.lambda$main$0(Main.java:254)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 2 seconds)

有人可以告诉我我做错了什么吗?

最佳答案

在 Java 中,java.lang.NullPointerException 几乎总是意味着您试图对空对象执行某些操作 - 尚未创建的对象,或者已创建但未正确初始化的对象。

第 57 行是这样的:

        NameText.setEditable(false);

因此,首先要查看的是您是否创建过 NameText。

你把它放得更远了:

private javax.swing.JTextField NameText;

请注意,您违反了一个重要的样式原则 - 对象和变量最初应小写,类最初应大写。

请注意,这与自然英语用法相反。

Steve is a person.

在 Java 中:

Person steve = new Person();

你会习惯的。如果您不这样做,其他人(还有您!)将很难阅读您的代码。

所以 NameText 应该是 nameText。

既然强制性的风格唠叨结束了,我可以回到强制性的初始化唠叨了:D

使用前必须先更改名称文本。

例如

        NameText = new JTextField();

请注意,几乎可以肯定您的所有其他变量都会遇到同样的问题。

此外,回到风格上的唠叨 - 调用类 Main 使理解发生了什么变得更加困难,因为构造函数看起来与默认的 Java 入口点(即 public static void main(String[] args) )太相似这是代码开始执行的起点)。

关于java - 在 JFrame 中显示外部文件内容的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50752780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com