如何在JPanels上添加标签

时间:2011-01-11 22:41:35

标签: java swing jframe jpanel jtabbedpane

我有一张带有桌子的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 );
    }
}

1 个答案:

答案 0 :(得分:2)

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

JTabbedPane可以作为JPanel的孩子添加。