1

我有一个 JPanel,上面有一张桌子。在同一个 JPanel 上,我想添加另一个表,但作为一个完全独立的表。所以我想要顶部某处的两个选项卡按钮在两个表之间进行交换。(第一个选项卡保留在默认表上,单击第二个选项卡将转到另一个表)。

到目前为止,我该如何处理我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.awt.Font; 
import javax.swing.plaf.FontUIResource; 
import java.util.Calendar; 
import java.awt.Color;
import javax.swing.table.*;

class table extends JFrame 
 {
    // Instance attributes used in this example
    private JPanel      topPanel;
    private JTable      table;
    private JScrollPane scrollPane;
    private static JFrame openFrame;

    public table ()
    {

    //.....{some code work deleted to simplify}....

    String columnNames[] = {"Time","Volume","Rate","CPP"};

    String dataValues[][]= new String [counter][4]; 


for (int i= 0; i<counter; i++){
dataValues[i][0] =dataValues_timemap[i];}

for (int j = 0; j<counter; j++){
dataValues[j][1] = dataValues_vol[j]; }

for (int k = 0; k<counter; k++){ 

dataValues[k][2] = dataValues_rate[k]; }    

for (int l = 0; l<countery; l++){
dataValues[l][3] = dataValues_cpp[l]; }


        setTitle("Table Summary");
        setSize(280,300);

        topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocation(1300,280);
        table = new JTable( dataValues, columnNames );

        scrollPane = new JScrollPane(table);
        topPanel.add( scrollPane, BorderLayout.CENTER );

}
    }

    public static void main( String args[] )
    {
        // Create an instance of the test application
        SimpleTableExample mainFrame    = new SimpleTableExample();
        mainFrame.setVisible( true );
    }
}
4

1 回答 1

2

使用JTabbedPane. 它正好解决了这个问题。

JTabbedPane可以添加为您的JPanel.

于 2011-01-11T22:42:20.633 回答