Hej,
Stworzyłem klase Adding, z ktorej mam zamiar dziedziczyc przy tworzeniu nowych klas. Stworzylem tam funkcje, ktore potrzebuje, ale jedna z nich
public void addFeatureToFrame(Object component){
frame.add((Component) component);
})
generuje wyjatek:
Exception in thread "main" java.lang.ClassCastException: class com.michal.Adding$AddingTextField cannot be cast to class java.awt.Component (com.michal.Adding$AddingTextField is in unnamed module of loader 'app'; java.awt.Component is in module java.desktop of loader 'bootstrap')
at com.michal.Adding$AddingFrame.addFeatureToFrame(Adding.java:26)
at com.michal.AddOwner.<init>(AddOwner.java:26)
at com.michal.Main.main(Main.java:10)
Za "Object" podstawialem tez nazwe klasy AddingTextField oraz stworzylem kopie funkcji i podstawilem AddingDropList, ale nie dzialalo.
Main:
public class Main {
public static void main(String[] args) {
new AddOwner();
}
klasa Adding:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Adding extends JFrame {
class AddingFrame{
protected JFrame frame;
AddingFrame() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.setSize(300, 500);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void setTitleFrame(String text){
frame.setTitle(text);
}
public void addFeatureToFrame(Object component){
frame.add((Component) component);
}
//public void addFeatureToFrame(AddingDropList component){
// frame.add(component);
// }
}
class AddingTextField{
protected JTextField nameField;
String textFieldContent;
AddingTextField(String textFieldContent) {
nameField = new JTextField(textFieldContent);
nameField.setPreferredSize(new Dimension(200, 50));
nameField.setFont(new Font("MV Boli", Font.PLAIN, 35));
}
}
class AddingDropList <T>{
protected JComboBox dropListContent;
AddingDropList(){
dropListContent=new JComboBox<>();
dropListContent.setPreferredSize(new Dimension(200,50));
dropListContent.setFont(new Font("MV Boli",Font.PLAIN,35));
}
public void addContentToDropList(T content){
dropListContent.addItem(content);
}
public void removeContentFromDropList(T content){
dropListContent.removeItem(content);
}
}
}
klasa dziedzicząca AddOwner:
import javax.swing.*;
import java.awt.*;
public class AddOwner extends Adding{
private AddingFrame frame;
private AddingTextField name;
private AddingTextField surname;
private AddingTextField age;
private AddingDropList gender;
AddOwner(){
frame=new AddingFrame();
frame.setTitleFrame("New Owner");
name=new AddingTextField("name");
surname=new AddingTextField("surname");
age =new AddingTextField("age");
gender=new AddingDropList<String>();
gender.addContentToDropList("Male");
gender.addContentToDropList("Female");
frame.addFeatureToFrame(name);
frame.addFeatureToFrame(surname);
frame.addFeatureToFrame(age);
frame.addFeatureToFrame(gender);
}
}
Proszę o pomoc.