我可以在jtabbedpane的内部添加表吗?

发布于 2025-01-20 13:04:15 字数 3301 浏览 3 评论 0原文

我对使用 JPanels/Tabs 非常陌生,因此我的代码遇到了很多问题。 我已经从 SQL 创建了一个表,我希望能够在选项卡内查看该表。 因此,我首先有一个包含登录的类,然后将您带到一个带有三个选项卡的窗口: 公共类用户界面 { UserInterface() {

    JFrame frame = new JFrame(); //create a frame
    frame.setTitle("Bet Worthiness"); //sets title of frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //system exit when user closes window
    frame.setResizable(false); //prevents user from resizing frame
    JTextField text = new JTextField(10); //creates a text box
    frame.getContentPane().setBackground(new Color(255, 243, 181)); //background colour

    //Create panels
    JPanel accountTab = new JPanel();
    JPanel bettingTab = new JPanel();
    JPanel leaderboardTab = new JPanel();

    JTabbedPane tabs = new JTabbedPane(); //Create the tab container
    tabs.setBounds(40, 20, 1400, 700); //Set tab container position


    //Account Details label
    JLabel accountLabel = new JLabel();
    accountLabel.setText("Account Details");
    accountLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
    accountTab.add(accountLabel);

    //Betting label
    JLabel bettingLabel = new JLabel();
    bettingLabel.setText("Place Bets");
    bettingLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
    bettingTab.add(bettingLabel);

    //Leaderboard label
    JLabel leaderboardLabel = new JLabel();
    leaderboardLabel.setText("Leaderboard");
    leaderboardLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
    leaderboardTab.add(leaderboardLabel);

    //Associate each panel with the corresponding tab
    tabs.add("Account", accountTab);
    tabs.add("Betting", bettingTab);
    tabs.add("Leaderboard", leaderboardTab);

    frame.add(tabs); //Add tabs to the frame
    frame.setSize(1500, 800);
    frame.setLayout(null);
    frame.setVisible(true);

}

}

然后,我在一个单独的类中创建了一个 SQL 表,我希望将其嵌入到“排行榜”选项卡中:

  public class Leaderboard extends JFrame {

public void displayLeaderboard() {

    try
    {
        String url = "jdbc:mysql://localhost:3306/bettingappdb";
        String user = "root";
        String password = "lucyb";

        Connection con = DriverManager.getConnection(url, user, password);

        String query = "SELECT * FROM users";

        Statement stm = con.createStatement();
        ResultSet res = stm.executeQuery(query);

        String columns[] = { "Username", "Success Rate" };
        String data[][] = new String[20][3];

        int i = 0;
        while (res.next()) {
            String nom = res.getString("Username");
            int successRate = res.getInt("SuccessRate");
            data[i][0] = nom;
            data[i][1] = successRate + "";
            i++;
        }

        DefaultTableModel model = new DefaultTableModel(data, columns);
        JTable table = new JTable(model);
        table.setShowGrid(true);
        table.setShowVerticalLines(true);
        JScrollPane pane = new JScrollPane(table);



        JFrame f = new JFrame("Leaderboard");
        JPanel panel = new JPanel();
        panel.add(pane);
        f.add(panel);
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    } catch(SQLException e) {
        e.printStackTrace();
    }
}

}

有什么方法可以将此表放入选项卡中吗?谢谢 :)

I'm very new at using JPanels/Tabs and so have run into a bunch of issues with my code.
I've created a table from SQL that I want to be able to be viewed inside of a tab.
So I have a class first of all that includes a login and then takes you to a window with three tabs:
public class UserInterface
{
UserInterface() {

    JFrame frame = new JFrame(); //create a frame
    frame.setTitle("Bet Worthiness"); //sets title of frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //system exit when user closes window
    frame.setResizable(false); //prevents user from resizing frame
    JTextField text = new JTextField(10); //creates a text box
    frame.getContentPane().setBackground(new Color(255, 243, 181)); //background colour

    //Create panels
    JPanel accountTab = new JPanel();
    JPanel bettingTab = new JPanel();
    JPanel leaderboardTab = new JPanel();

    JTabbedPane tabs = new JTabbedPane(); //Create the tab container
    tabs.setBounds(40, 20, 1400, 700); //Set tab container position


    //Account Details label
    JLabel accountLabel = new JLabel();
    accountLabel.setText("Account Details");
    accountLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
    accountTab.add(accountLabel);

    //Betting label
    JLabel bettingLabel = new JLabel();
    bettingLabel.setText("Place Bets");
    bettingLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
    bettingTab.add(bettingLabel);

    //Leaderboard label
    JLabel leaderboardLabel = new JLabel();
    leaderboardLabel.setText("Leaderboard");
    leaderboardLabel.setFont(new Font("Helvetica", Font.BOLD, 20));
    leaderboardTab.add(leaderboardLabel);

    //Associate each panel with the corresponding tab
    tabs.add("Account", accountTab);
    tabs.add("Betting", bettingTab);
    tabs.add("Leaderboard", leaderboardTab);

    frame.add(tabs); //Add tabs to the frame
    frame.setSize(1500, 800);
    frame.setLayout(null);
    frame.setVisible(true);

}

}

I have then created a table on SQL in a separate class that I want to be embedded in the 'leaderboard' tab:

  public class Leaderboard extends JFrame {

public void displayLeaderboard() {

    try
    {
        String url = "jdbc:mysql://localhost:3306/bettingappdb";
        String user = "root";
        String password = "lucyb";

        Connection con = DriverManager.getConnection(url, user, password);

        String query = "SELECT * FROM users";

        Statement stm = con.createStatement();
        ResultSet res = stm.executeQuery(query);

        String columns[] = { "Username", "Success Rate" };
        String data[][] = new String[20][3];

        int i = 0;
        while (res.next()) {
            String nom = res.getString("Username");
            int successRate = res.getInt("SuccessRate");
            data[i][0] = nom;
            data[i][1] = successRate + "";
            i++;
        }

        DefaultTableModel model = new DefaultTableModel(data, columns);
        JTable table = new JTable(model);
        table.setShowGrid(true);
        table.setShowVerticalLines(true);
        JScrollPane pane = new JScrollPane(table);



        JFrame f = new JFrame("Leaderboard");
        JPanel panel = new JPanel();
        panel.add(pane);
        f.add(panel);
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    } catch(SQLException e) {
        e.printStackTrace();
    }
}

}

Is there any way to put this table within the tab? Thank you :)

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

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

发布评论

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

评论(1

晨曦÷微暖 2025-01-27 13:04:15

您可以将以下内容添加到您的代码中。

JPanel lbTab = new JPanel();
Leaderboard leaderboard = new Leaderboard();
lbTab.add(leaderboard);

排行榜现在位于您的 JTabbedPane 中。如果您想在一个选项卡中添加排行榜以及排行榜标签,我建议使用类似 https://docs.oracle.com/javase/tutorial/uiswing/components/panel.html如何布局? (JFrame、JPanel 等) 作为您的第一步:)。

You could add the following to your Code.

JPanel lbTab = new JPanel();
Leaderboard leaderboard = new Leaderboard();
lbTab.add(leaderboard);

the Leaderboard is now in your JTabbedPane. If you want to add the Leaderboard as well as the leaderboardLabel in one Tab i suggest something like https://docs.oracle.com/javase/tutorial/uiswing/components/panel.html or How to layout? (JFrame, JPanel, etc.) for your first steps :) .

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