I am making a simple bible reader for a confirmation project, and I have a UI class. The class opens a windows when called by the main class. I just satisfied multiple errors with static keywords, but one thing is left - my prepareGUI method. Should it be declared static? My IDE throws no errors either way.
package input;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class UI extends JFrame {
private static final long serialVersionUID = 4226151097999382106L;
private static JFrame mainFrame;
private static JLabel headerLabel;
private static JLabel statusLabel;
private static JPanel controlPanel;
public UI() {
prepareGUI();
}
private static/* Should that be there? */ void prepareGUI() {
mainFrame = new JFrame("Holy Bible");
mainFrame.setSize(700, 500);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.setLocationRelativeTo(null);
mainFrame.getContentPane().setBackground(Color.WHITE);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("", JLabel.CENTER);
statusLabel.setSize(350, 100);
controlPanel = new JPanel();
controlPanel.setBackground(Color.WHITE);
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
headerLabel.setText((String) ReadFile.currentBookData[0]);
JLabel label = new JLabel("", JLabel.CENTER);
label.setText((String) ReadFile.currentBookData[2]);
label.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
controlPanel.add(label);
mainFrame.setVisible(true);
}
public void updateText() {
headerLabel.setText((String) ReadFile.currentBookData[4]);
statusLabel.setText((String) ReadFile.currentBookData[4]);
}
}
The way you "fixed" your error goes against the concept of OOP. static
methods and fields are meant to be "properties" of the class itself, where non-static methods and fields are "properties" of an instance (object) of said class.
What you should do is remove the static
keyword from your fields and your prepareGUI()
method:
//private JFrame mainFrame; you only have one instance, so no point in having a global field
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private /*static Should that be there? answer is no. */ void prepareGUI() {
JFrame mainFrame = new JFrame("Holy Bible");
(...)
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다