所以我有两个类,一个叫ApplicationViewer,另一个叫PetshopOverview。我希望applicationviewer使用JTabbedPane在两个选项卡中显示一些信息。我需要从扩展JScrollPane的另一个类中获取一些信息,但是,该选项卡不显示该信息。我已经看过各种答案,但是看来我的方法行不通。参见下面的代码:

public class ApplicationViewer extends JFrame{


public void viewer(final ArrayList<PetShop> petshops){
    lookAndFeel(); //this calls the look and feel method which changes the looks of the frame.

   PetshopOverview ov = new PetshopOverview(petshops);



    Object[] columnNames = {"Name", "Address", "Phone Number", "Website", "Opening Time"}; //declaring columns names
    Object[][] rowData = new Object[petshops.size()][columnNames.length]; //initializing rows.
    DefaultTableModel listTableModel;

    for (int i = 0; i < petshops.size(); i++) { //this for loop adds data from the arraylist to each coloumn.
        rowData[i][0] = petshops.get(i).getName();
        rowData[i][1] = petshops.get(i).getAddress();
        rowData[i][2] = petshops.get(i).getPhoneNumber();
        rowData[i][3] = petshops.get(i).getWebsite();
        rowData[i][4] = petshops.get(i).getOpeningTime();
    }

    listTableModel = new DefaultTableModel(rowData, columnNames);
    JPanel panelLB = new JPanel();
    JPanel panel = new JPanel();
    JPanel panelBT = new JPanel();
    JButton btnViewSum = new JButton("View Summary");
    JButton btnExp = new JButton("Export table data");

    //---------------------JTABLE AND JFRAME (adding adding table, panels and buttons to the jframe)--------------------------------------------------------

    JTable listTable;
    listTable = new JTable(listTableModel);




    listTable.setRowSelectionAllowed(true);
    JScrollPane scroll = new JScrollPane(listTable);
    scroll.setViewportView(listTable);
    JFrame frame = new JFrame("PetShops");
    JTabbedPane tab = new JTabbedPane();

    tab.addTab("Tab1", scroll);
    tab.addTab("Tab2", new PetshopOverview(petshops));
    JLabel lb = new JLabel("Welcome to Pet shop app");
    panelBT.add(lb);
    panelBT.add(btnExp);
    panelBT.add(btnViewSum);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setSize(600, 400);
    frame.add(panelBT);
    frame.add(tab);
    frame.getContentPane().add(panelBT, java.awt.BorderLayout.NORTH);
    frame.getContentPane().add(tab, java.awt.BorderLayout.CENTER);
    frame.setVisible(true);
    }


这是另一个类PetshopOverview:

public class PetshopOverview extends JScrollPane{
    public PetshopOverview(ArrayList<PetShop> petshopsSum){


    Object[] columnNames = {"Name", "Opening Time"};
    Object[][] rowData = new Object[petshopsSum.size()][columnNames.length];
    DefaultTableModel listTableModel;

    int size= petshopsSum.size();

    for (int i = 0; i < size; i++) {
        rowData[i][0] = petshopsSum.get(i).getName();
        rowData[i][1] = petshopsSum.get(i).getOpeningTime();
    }


    listTableModel = new DefaultTableModel(rowData, columnNames);

//-------------------------JTABLE AND JFRAME--------------------------
JTable listTable;
listTable = new JTable(listTableModel);


宠物商店:

public class PetShop {
    private String name, address, phoneNumber, website, openingTime;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getOpeningTime() {
        return openingTime;
    }

    public void setOpeningTime(String openingTime) {
        this.openingTime = openingTime;
    }


    public PetShop(String sName, String sAddress, String sPhoneNumber, String sWebsite, String sOpeningTime){
        this.name = sName;
        this.address = sAddress;
        this.phoneNumber = sPhoneNumber;
        this.website = sWebsite;
        this.openingTime = sOpeningTime;
    }




    @Override
    public String toString(){
        return getName()+"\n"
                +getAddress().replaceAll(":", "").replaceFirst("", " ")+"\n"
                +getPhoneNumber()+"\n"
                +getWebsite().replaceFirst("", " ")+"\n"
                +getOpeningTime().replaceFirst(",", "").replaceFirst("", " ")+"\n\n";
    }

    public PetShop(String sName, String sOpeningTime){
        this.name = sName;
        this.openingTime = sOpeningTime;
    }

    public String toString2 (){
        return getName()+": "
                +getOpeningTime().replaceFirst(",", "").replaceFirst("", " ");
    }
}

最佳答案

我想您几乎在那里,我只需要调整框架并向其中添加组件。因此,在ApplicationViewer中而不是创建新的JFrame,将组件添加到已经是ApplicationViewerJFrame中。在PetShopOverview上,您需要将ViewportViewPetShopOverview设置为listTable

这是一个例子:

宠物商店

public class PetShop {
String name;
String openingTime;
String address;
String phoneNumber;
String website;

public PetShop(String name, String openingTime, String address, String phoneNumber, String website) {
    this.name = name;
    this.openingTime = openingTime;
    this.address = address;
    this.phoneNumber = phoneNumber;
    this.website = website;
}

public String getName() {
    return name;
}

public String getOpeningTime() {
    return openingTime;
}

public String getAddress() {
    return address;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public String getWebsite() {
    return website;
}}


PetShop概述:

public class PetShopOverview extends JScrollPane {
public PetShopOverview(ArrayList<PetShop> petshopsSum) {
    Object[] columnNames = { "Name", "Opening Time" };
    Object[][] rowData = new Object[petshopsSum.size()][columnNames.length];
    DefaultTableModel listTableModel;

    int size = petshopsSum.size();

    for (int i = 0; i < size; i++) {
        rowData[i][0] = petshopsSum.get(i).getName();
        rowData[i][1] = petshopsSum.get(i).getOpeningTime();
    }

    listTableModel = new DefaultTableModel(rowData, columnNames);

    // -------------------------JTABLE AND JFRAME--------------------------
    JTable listTable;
    listTable = new JTable(listTableModel);
    this.setViewportView(listTable);
}}


ApplicationViewer:

public class ApplicationViewer extends JFrame {

public ApplicationViewer(ArrayList<PetShop> petshops) {
    viewer(petshops);
}

public void viewer(final ArrayList<PetShop> petshops) {

    PetShopOverview ov = new PetShopOverview(petshops);

    Object[] columnNames = { "Name", "Address", "Phone Number", "Website", "Opening Time" }; // declaring columns
                                                                                             // names
    Object[][] rowData = new Object[petshops.size()][columnNames.length]; // initializing rows.
    DefaultTableModel listTableModel;

    for (int i = 0; i < petshops.size(); i++) { // this for loop adds data from the arraylist to each coloumn.
        rowData[i][0] = petshops.get(i).getName();
        rowData[i][1] = petshops.get(i).getAddress();
        rowData[i][2] = petshops.get(i).getPhoneNumber();
        rowData[i][3] = petshops.get(i).getWebsite();
        rowData[i][4] = petshops.get(i).getOpeningTime();
    }

    listTableModel = new DefaultTableModel(rowData, columnNames);
    JPanel panelLB = new JPanel();
    JPanel panel = new JPanel();
    JPanel panelBT = new JPanel();
    JButton btnViewSum = new JButton("View Summary");
    JButton btnExp = new JButton("Export table data");

    // ---------------------JTABLE AND JFRAME (adding adding table, panels and buttons to the
    // jframe)--------------------------------------------------------

    JTable listTable;
    listTable = new JTable(listTableModel);

    listTable.setRowSelectionAllowed(true);
    JScrollPane scroll = new JScrollPane(listTable);
    scroll.setViewportView(listTable);
    JTabbedPane tab = new JTabbedPane();

    tab.addTab("Tab1", scroll);
    tab.addTab("Tab2", new PetShopOverview(petshops));
    JLabel lb = new JLabel("Welcome to Pet shop app");
    panelBT.add(lb);
    panelBT.add(btnExp);
    panelBT.add(btnViewSum);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    this.setSize(600, 400);
    this.add(panelBT);
    this.add(tab);
    this.getContentPane().add(panelBT, java.awt.BorderLayout.NORTH);
    this.getContentPane().add(tab, java.awt.BorderLayout.CENTER);
    this.setVisible(true);
}}


主要方法:

public static void main(String[] args) {
    ArrayList<PetShop> p = new ArrayList<PetShop>();
    p.add(new PetShop("a", "9", "street 1", "123", "www.a.com"));
    p.add(new PetShop("b", "10", "street 2", "456", "www.b.com"));
    p.add(new PetShop("c", "11", "street 3", "789", "www.c.com"));
    p.add(new PetShop("d", "12", "street 4", "000", "www.d.com"));

    ApplicationViewer v = new ApplicationViewer(p);
    v.setVisible(true);
}


ps。我刚刚创建了一个任意的PetShop类

09-25 17:45