Java静态与非静态使用这个和事件处理程序
-
11-09-2019 - |
题
我想了解Java的事件处理程序和不断收到错误类型为类型(静态/非静态的)创建方法。一些代码,我试着写是这样的:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main extends JFrame implements ActionListener{
static private int[] intArray = new int[10000];
static private int numOfInts = 0;
static private int avg = 0;
public static void main(String[] args) {
//create main frame
JFrame frame = new JFrame();
frame.setTitle("Section V, question 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 250);
frame.setLayout(new GridLayout(4, 1));
frame.setVisible(true);
//create instruction label and add to frame
Label instructions = new Label("Follow the instructions on the exam to use this program");
frame.add(instructions);
//create textfield for index entry and add to frame
JTextField indexEntry = new JTextField();
frame.add(indexEntry);
//create button for average and add to frame
JButton avgBtn = new JButton("Click for Average");
frame.add(avgBtn);
avgBtn.addActionListener(avgBtn);
//create panel to display results and add to frame
JPanel resultsPanel = new JPanel();
resultsPanel.setBackground(Color.BLUE);
frame.add(resultsPanel);
//read in from file
readFromFile();
//compute average
computeAverage();
System.out.println(avg);
}
static private void readFromFile(){
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("numbers.dat");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
int i = 0;
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
intArray[i] = Integer.parseInt(strLine);
numOfInts++;
i++;
}
//Close the input stream
in.close();
System.out.println ("numOfInts = " + numOfInts);
}
catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
static private void computeAverage(){
int sum = 0;
for(int i = 0; i < numOfInts; i++)
sum += intArray[i];
avg = sum/numOfInts;
//return avg;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == avgBtn){
computeAverage();
}
}
}
这是为了设置在从文件中某些整数读取的GUI,然后当按下按钮计算它们的平均值。不过,我不断收到与静态/非静态的东西,事件左撇子问题。我现在的错误是:点击 Main.java:35:addActionListener方法(java.awt.event.ActionListener)在javax.swing.AbstractButton中不能被施加到(javax.swing.JButton中),点击 avgBtn.addActionListener(avgBtn);
Main.java:91:找不到符号,点击 符号:变量avgBtn结果 位置:类主要结果 如果(e.getSource()== avgBtn){
据我所知,编译器无法找到avgBtn,因为它的另一个功能(Main()方法)中定义的,但任何人都可以阐明如何将事件处理程序附加给它一些轻?试图“这”无果也...在此先感谢,如果你看到什么都错了,我很想听听我怎么可以做的更好。
解决方案
您的代码是有点混乱,如果编译会有更多的语法错误。 你不应该混合摆动/ AWT组件,例如:而不是使用标签使用的JLabel在摇摆,对面板使用的JPanel
注意为摆动的“J”字头的,你应该也许读书,如果你想要更多的了解关于Java(秋千),甚至读了一些基本的教程。
除非你了解它的目的,不要使用静态方法。
总之这里是你想要的最接近的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Main extends JFrame implements ActionListener {
private int[] intArray = new int[10000];
private int numOfInts = 0;
private int avg = 0;
protected JButton avgBtn;
protected JTextField indexEntry;
protected JLabel instructions;
protected JPanel resultsPanel;
//constructor - construct the components here and do the initializations
public Main(){
//create main frame
this.setTitle("Section V, question 2");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(350, 250);
this.setLayout(new GridLayout(4, 1));
//this.setVisible(true);
//create instruction label and add to frame
instructions = new JLabel("Follow the instructions on the exam to use this program");
this.add(instructions);
//create textfield for index entry and add to frame
indexEntry = new JTextField();
this.add(indexEntry);
//create button for average and add to frame
avgBtn = new JButton("Click for Average");
this.add(avgBtn);
avgBtn.addActionListener(this);
//create panel to display results and add to frame
resultsPanel = new JPanel();
resultsPanel.setBackground(Color.BLUE);
this.add(resultsPanel);
//read in from file
readFromFile();
//compute average
computeAverage();
System.out.println(avg);
}
private void readFromFile() {
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("numbers.dat");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
int i = 0;
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
intArray[i] = Integer.parseInt(strLine);
numOfInts++;
i++;
}
//Close the input stream
in.close();
System.out.println ("numOfInts = " + numOfInts);
}
catch (Exception e) {
//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
private void computeAverage() {
int sum = 0;
for (int i = 0; i < numOfInts; i++)
sum += intArray[i];
avg = sum/numOfInts;
//return avg;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == avgBtn) {
computeAverage();
}
}
public static void main(String[] args) {
Main m = new Main();
m.setVisible(true);
}
}
不隶属于 StackOverflow