如何在 JPanel 上添加选项卡

发布于 2024-10-11 16:24:10 字数 1829 浏览 7 评论 0原文

我有一个 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 );
    }
}

I have a JPanel with a table on it. On that same JPanel, I want to add another table but as a totally separate table. So I want two tabs button somewhere on the top to interchange between the two tables. (first tab to stay on default table, clicking on second tab will go to the other table).

How do I go about in my code so far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光礼记 2024-10-18 16:24:10

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

JTabbedPane 可以添加为 JPanel 的子级。

Use a JTabbedPane. It solves exactly this problem.

The JTabbedPane can be added as a child of your JPanel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文