如何将组件添加到JTabbedPane

时间:2016-04-15 07:47:24

标签: java jframe jtabbedpane

所以我有两个类,一个叫做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("", " ");
    }
}

1 个答案:

答案 0 :(得分:1)

你几乎就在那里我认为你只需要调整框架并添加你的组件。因此,在ApplicationViewer而不是创建新的JFrame而不是将您的组件添加到已ApplicationViewer的{​​{1}}。在JFrame上,您需要将PetShopOverview的{​​{1}}设置为ViewportView

以下是一个例子:

PetShop的

PetShopOverview

PetShopOverview:

listTable

ApplicationViewer:

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;
}}

主要方法:

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);
}}

P.S。我刚创建了一个任意的PetShop类