2016-04-15 60 views
0

所以我有两个类,一个叫ApplicationViewer,一个叫PetshopOverview。我希望applicationviewer使用JTabbedPane在两个选项卡中显示一些信息。我需要从扩展JScrollPane的另一个类获取一些信息,但是,该选项卡不显示信息。我看了各种答案,但似乎我的工作不正常。请参见下面的代码:如何添加组件到JTabbedPane

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("", " "); 
    } 
} 
+0

您可以添加“PetShop”类吗? –

+0

在这里你去@canlekili –

+0

谢谢我刚创建petshop任意类。 –

回答

1

就快,我认为你只需要调整的框架,并添加你的组件。因此,在ApplicationViewer而不是创建一个新的JFrame添加您的组件到ApplicationViewer这已经是JFrame。在PetShopOverview上,您需要将PetShopOverviewViewportView设置为listTable

下面是一个例子:

PetShop的

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

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

p.s.我刚刚创建了一个任意的PetShop类

+0

它完美的作品!非常感谢! –