Swing 未写字段警告(Java)
Swing unwritten field warning (Java)
我安装了 findBugs,并在 if 语句的 actionPerformed 方法中收到错误警告
if (source == this.temp)
警告说有一个未写入的字段。该程序仍然可以编译,但当我单击名为 temp 的按钮时挂起。
我以为我已经正确地初始化了那个字段。有人可以告诉我我搞砸的地方吗?谢谢
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
/**
* View class.
*
* @author Redacted
*/
@SuppressWarnings("serial")
public final class PasswordManagerView1 extends JFrame
implements PasswordManagerView {
private JButton temp;
/**
* controller.
*/
private PasswordManagerController controller;
/**
* Jpanel.
*/
/**
* Useful constants.
*/
private Dimension maxSize;
private JTabbedPane tabbedPane;
/**
* Constructor.
*/
public PasswordManagerView1() {
super("Password Manager");
JTabbedPane tabbedPane = new JTabbedPane();
//Initial JPanel creation
tabbedPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//tabbedPane.setLayout(new BorderLayout(0, 0));
this.maxSize = new Dimension(700, 300);
tabbedPane.setPreferredSize(this.maxSize);
this.getContentPane().add(tabbedPane);
//Initial JTabbedPane creation
//Tab creation
JComponent panel1 = this.makeTextPanel("temp1");
ImageIcon icon = new ImageIcon("lock-icon.png");
tabbedPane.addTab("Add Password", icon, panel1,
"Adds a password to the vault");
JComponent panel2 = this.makeTextPanel("temp2");
tabbedPane.addTab("Delete Password", icon, panel2,
"Deletes a password from the vault");
JComponent panel3 = this.makeTextPanel("temp3");
tabbedPane.addTab("Password Vault", icon, panel3,
"View the passwords in the vault");
JComponent panel4 = this.makeInfoPanel();
tabbedPane.addTab("Info/Settings", icon, panel4,
"View settings and program info");
JButton temp = new JButton("Hey");
panel1.add(temp);
temp.addActionListener(this);
//Pack up
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private JComponent makeTextPanel(String text) {
JPanel panel = new JPanel();
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
private JComponent makeInfoPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 1));
StringBuilder toPrint = new StringBuilder();
SimpleReader in = new SimpleReader1L("data/Notice.txt");
while (!in.atEOS()) {
toPrint.append(in.nextLine() + "\n");
}
String toPrintString = toPrint.toString();
JTextArea noticeText = new JTextArea(toPrintString);
noticeText.setEditable(false);
JScrollPane noticeTextScroll = new JScrollPane(noticeText);
panel.add(noticeTextScroll);
in.close();
return panel;
}
@Override
public void registerObserver(PasswordManagerController controller) {
this.controller = controller;
}
@Override
public void actionPerformed(ActionEvent event) {
//Wait cursor
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//What button was pressed
Object source = event.getSource();
if (source == this.temp) {
this.controller.processTestEvent();
}
}
在构造函数中有这段代码:
JButton temp = new JButton("Hey");
panel1.add(temp);
它定义了一个隐藏成员的局部 temp
变量 - 删除 JButton
以便它使用 class 成员:
temp = new JButton("Hey");
panel1.add(temp);
我安装了 findBugs,并在 if 语句的 actionPerformed 方法中收到错误警告
if (source == this.temp)
警告说有一个未写入的字段。该程序仍然可以编译,但当我单击名为 temp 的按钮时挂起。
我以为我已经正确地初始化了那个字段。有人可以告诉我我搞砸的地方吗?谢谢
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import components.simplereader.SimpleReader;
import components.simplereader.SimpleReader1L;
/**
* View class.
*
* @author Redacted
*/
@SuppressWarnings("serial")
public final class PasswordManagerView1 extends JFrame
implements PasswordManagerView {
private JButton temp;
/**
* controller.
*/
private PasswordManagerController controller;
/**
* Jpanel.
*/
/**
* Useful constants.
*/
private Dimension maxSize;
private JTabbedPane tabbedPane;
/**
* Constructor.
*/
public PasswordManagerView1() {
super("Password Manager");
JTabbedPane tabbedPane = new JTabbedPane();
//Initial JPanel creation
tabbedPane.setBorder(new EmptyBorder(5, 5, 5, 5));
//tabbedPane.setLayout(new BorderLayout(0, 0));
this.maxSize = new Dimension(700, 300);
tabbedPane.setPreferredSize(this.maxSize);
this.getContentPane().add(tabbedPane);
//Initial JTabbedPane creation
//Tab creation
JComponent panel1 = this.makeTextPanel("temp1");
ImageIcon icon = new ImageIcon("lock-icon.png");
tabbedPane.addTab("Add Password", icon, panel1,
"Adds a password to the vault");
JComponent panel2 = this.makeTextPanel("temp2");
tabbedPane.addTab("Delete Password", icon, panel2,
"Deletes a password from the vault");
JComponent panel3 = this.makeTextPanel("temp3");
tabbedPane.addTab("Password Vault", icon, panel3,
"View the passwords in the vault");
JComponent panel4 = this.makeInfoPanel();
tabbedPane.addTab("Info/Settings", icon, panel4,
"View settings and program info");
JButton temp = new JButton("Hey");
panel1.add(temp);
temp.addActionListener(this);
//Pack up
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
private JComponent makeTextPanel(String text) {
JPanel panel = new JPanel();
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
private JComponent makeInfoPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 1));
StringBuilder toPrint = new StringBuilder();
SimpleReader in = new SimpleReader1L("data/Notice.txt");
while (!in.atEOS()) {
toPrint.append(in.nextLine() + "\n");
}
String toPrintString = toPrint.toString();
JTextArea noticeText = new JTextArea(toPrintString);
noticeText.setEditable(false);
JScrollPane noticeTextScroll = new JScrollPane(noticeText);
panel.add(noticeTextScroll);
in.close();
return panel;
}
@Override
public void registerObserver(PasswordManagerController controller) {
this.controller = controller;
}
@Override
public void actionPerformed(ActionEvent event) {
//Wait cursor
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//What button was pressed
Object source = event.getSource();
if (source == this.temp) {
this.controller.processTestEvent();
}
}
在构造函数中有这段代码:
JButton temp = new JButton("Hey");
panel1.add(temp);
它定义了一个隐藏成员的局部 temp
变量 - 删除 JButton
以便它使用 class 成员:
temp = new JButton("Hey");
panel1.add(temp);