我是Java的新手,需要快速回答,所以不要判断我是否搜索不够。所以我有类Antena并尝试创建Controller类,我不能创建数组列表实例。代码尚未完成。有人能解决这个吗? :d 这是控制器类:
package paket;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Scanner;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
public class KontrolaAntene implements Initializable {
private List<Antena> antene= new ArrayList<Antena>();
private int broj;
@FXML
private Button btnIspisi;
@FXML
private Button btnIzlaz;
@FXML
private TextArea taIspis;
@FXML
private TextArea taIspis_izlaz;
@FXML
private Label label1;
@FXML
private Label label2;
@FXML
private Label label3;
@FXML
private TextField tfx;
@FXML
private TextField tfy;
@FXML
void ispisi(ActionEvent event){
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Izaberi datoteku");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("Text files","*.txt"),
new ExtensionFilter("All files","*.txt")
);
String pocetakputanja = System.getProperty("user.dir");
File pocetak = new File(pocetakputanja);
if(!pocetak.canRead())
{
pocetak = new File("C:/");
}
fileChooser.setInitialDirectory(pocetak);
File selectedFile = fileChooser.showOpenDialog(TestAntene.mainStage);
if(selectedFile == null || selectedFile.getName()!="antene.txt"){
taIspis.setText("Datoteka antene.txt nije izabrana");
}
else{
taIspis.setText("");
ispisiFajl(selectedFile);
}
}
void ispisiFajl(File selectedFile){
antene.clear();
Path put = Paths.get(selectedFile.getPath());
Charset kod = StandardCharsets.UTF_8;
Scanner sc;
try {
sc = new Scanner(put, kod.name());
while(sc.hasNextLine()){
String linija = sc.nextLine();
String tip = linija.substring(0,linija.indexOf(":")).trim();
String delovi[]= linija.substring(linija.indexOf(":") + 1,
linija.length()).split(", ");
if(tip == "K"){
//THIS LINE IS A PROBLEM!
Antena nova = new Antena(Integer.parseInt(delovi[1].trim()),
Integer.parseInt(delovi[1].trim()),Integer.parseInt(delovi[1].trim()));
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
taIspis.setText("Datoteka antene.txt nije izabrana");
}
}
@FXML
void ispisi_izlaz(ActionEvent event){
taIspis_izlaz.appendText("Broj antena koje pokrivaju zadato mesto je "+broj);
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
}
这是Antena班:
package paket;
公共抽象类Antena {
public int x;
public int y;
public int Domet1;
public int Domet2;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDomet1() {
return Domet1;
}
public void setDomet1(int domet1) {
Domet1 = domet1;
}
public int getDomet2() {
return Domet2;
}
public void setDomet2(int domet2) {
Domet2 = domet2;
}
public Antena(int x, int y, int domet1, int domet2) {
super();
this.x = x;
this.y = y;
Domet1 = domet1;
Domet2 = domet2;
}
boolean uDometu(int x, int y)
{
double rastojanje;
int x2 = getX();
int y2 = getY();
int domet1= getDomet1();
int domet2= getDomet2();
if(getDomet2() == 0){
rastojanje= Math.sqrt((y-y2)*(y-y2)+(x-x2)*(x-x2));
if((double)getDomet1() >= rastojanje)
return true;
else
return false;
}
else
{
if((x2==x && y2+domet2>y)||
(x2==x && y2-domet2<y)||
(x2+domet1>x && y2==y)||
(x2-domet1<x && y2==y)){ return true;}
else if((x2+domet1>=x && y2+domet1>=y)||
(x2-domet1<=x && y2+domet1<=y)||
(x2+domet1<=x && y2+domet1>=y)||
(x2+domet1>=x && y2+domet1<=y))
{return true;}
else return false;
}
}
}
答案 0 :(得分:1)
您似乎没有正确地比较String
(s),并且您无法实例化abstract
类。选项 1 ,
abstract
或Antenna
像
这样的东西if (tip.equals("K")) { // == "K"){
Antenna nova = new Antenna(Integer.parseInt(delovi[1].trim()), // ...
请记住然后将“Antena
”更改为
public class Antenna { // <-- Not abstract
1 我还更正了天线的拼写。
答案 1 :(得分:0)
您标记的行中有2个问题:
Antena
是抽象类,无法实例化。这是抽象类的一个非常基本的特征,意味着继承。你应该继承Antena
或制作非抽象的
您正在调用Antena
的构造函数,并且您将传递3个整数作为参数。但是Antena
的唯一构造函数需要4个int
答案 2 :(得分:0)
抽象类,无法实例化,antena是抽象类型。删除抽象关键字,它将起作用。