JTable未显示在JFrame(Java)上

我有一个问题,JFrame没有显示JTable添加到它。我已经尝试过getContentPane().add(..),我已切换为仅添加以使代码更短一些。任何帮助都超过了感激!


package com.embah.Accgui;


import java.awt.*;

import javax.swing.*;


public class accCreator extends JFrame {

private String[] columnNames = {"Username", "Password", "Members", "World"};

private Object[][] data = {{"b", "b", "b", "b"},

                          { "e", "e", "e", "e"}};

    private JTable tbl_Accounts;

    private JScrollPane scrollPane;

    private JLabel lbl_Account = new JLabel();

    private JLabel lbl_Username = new JLabel();

    private JLabel lbl_Password = new JLabel();

    private JLabel lbl_Homeworld = new JLabel();

    private JButton btn_Select = new JButton();

    private JButton btn_Addacc = new JButton();

    private JButton btn_Delacc = new JButton();

    private JTextArea txt_Username = new JTextArea();

    private JTextArea txt_Password = new JTextArea();

    private JTextArea txt_Homeworld = new JTextArea();

    private JCheckBox cbox_Members = new JCheckBox();

    private JCheckBox cbox_RanWrld = new JCheckBox();



public accCreator() {

    setLayout(null);

    setupGUI();

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}


void setupGUI() {

    tbl_Accounts = new JTable(data, columnNames);

    tbl_Accounts.setLocation(5, 30);

    tbl_Accounts.setPreferredScrollableViewportSize(new Dimension(420, 250));

    tbl_Accounts.setFillsViewportHeight(true);

    tbl_Accounts.setVisible(true);

    add(tbl_Accounts);

    scrollPane = new JScrollPane(tbl_Accounts);

    add(scrollPane);


    lbl_Account.setLocation(4, 5);

    lbl_Account.setSize(100, 20);

    lbl_Account.setText("Select Account:");

    add(lbl_Account);


    lbl_Username.setLocation(5, 285);

    lbl_Username.setSize(70, 20);

    lbl_Username.setText("Username:");

    add(lbl_Username);


    lbl_Password.setLocation(5, 310);

    lbl_Password.setSize(70, 20);

    lbl_Password.setText("Password:");

    add(lbl_Password);


智慧大石
浏览 697回答 2
2回答

梵蒂冈之花

不能说谢谢你们,对不起,我这么固执,无法早日听你们说。我只是想制作一个不错的GUI,而我整天都在努力使它正常工作,我非常沮丧。Frak,您向我展示了setLayout(null)非常糟糕,不应该这样做。非常感谢您的投入和帮助!我非常感谢你们的帖子!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java