我可以在jtabbedpane的内部添加表吗?
我对使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将以下内容添加到您的代码中。
排行榜现在位于您的 JTabbedPane 中。如果您想在一个选项卡中添加排行榜以及排行榜标签,我建议使用类似 https://docs.oracle.com/javase/tutorial/uiswing/components/panel.html 或 如何布局? (JFrame、JPanel 等) 作为您的第一步:)。
You could add the following to your Code.
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 :) .