gpt4 book ai didi

java - 如何向 JTabbedPane 添加组件

转载 作者:行者123 更新时间:2023-12-01 10:03:21 27 4
gpt4 key购买 nike

所以我有两个类,一个称为 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,而是将组件添加到已经是 JFrameApplicationViewer 中。在 PetShopOverview 上,您需要将 PetShopOverviewViewportView 设置为 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);
}}

应用程序查看器:

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

附:我刚刚创建了一个任意 PetShop 类

关于java - 如何向 JTabbedPane 添加组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36641211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com