JTable 未显示在 JFrame (Java) 上 [英] JTable not showing up on JFrame (Java)

查看:27
本文介绍了JTable 未显示在 JFrame (Java) 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 JFrame 没有显示添加到其中的 JTable 的问题.我已经尝试过 getContentPane().add(..),我已经切换到只是添加以保持代码更短一些.任何帮助都非常感谢!

package com.embah.Accgui;导入 java.awt.*;导入 javax.swing.*;公共类 accCreator 扩展 JFrame {私人字符串[] columnNames = {用户名",密码",成员",世界"};私有对象[][] 数据 = {{"b", "b", "b", "b"},{e",e",e",e"}};私人 JTable tbl_Accounts;私人 JScrollPane 滚动窗格;私人 JLabel lbl_Account = new JLabel();私有 JLabel lbl_Username = new JLabel();私有 JLabel lbl_Password = new JLabel();私人 JLabel lbl_Homeworld = new JLabel();私有 JButton btn_Select = new JButton();私有 JButton btn_Addacc = new JButton();私有 JButton btn_Delacc = new JButton();私人 JTextArea txt_Username = 新 JTextArea();私人 JTextArea txt_Password = 新 JTextArea();私人 JTextArea txt_Homeworld = 新 JTextArea();私人 JCheckBox cbox_Members = new JCheckBox();私人 JCheckBox cbox_RanWrld = new JCheckBox();公共 accCreator() {设置布局(空);设置GUI();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}无效设置GUI(){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);添加(tbl_Accounts);scrollPane = new JScrollPane(tbl_Accounts);添加(滚动窗格);lbl_Account.setLocation(4, 5);lbl_Account.setSize(100, 20);lbl_Account.setText("选择账户:");添加(lbl_Account);lbl_Username.setLocation(5, 285);lbl_Username.setSize(70, 20);lbl_Username.setText("用户名:");添加(lbl_用户名);lbl_Password.setLocation(5, 310);lbl_Password.setSize(70, 20);lbl_Password.setText("密码:");添加(lbl_密码);lbl_Homeworld.setLocation(310, 310);lbl_Homeworld.setSize(80, 20);lbl_Homeworld.setText("家园世界:");添加(lbl_Homeworld);btn_Select.setLocation(305, 5);btn_Select.setSize(120, 20);btn_Select.setText("选择账户");添加(btn_Select);btn_Addacc.setLocation(300, 285);btn_Addacc.setSize(60, 20);btn_Addacc.setText("添加");btn_Addacc.addActionListener(new ActionListener(){@覆盖公共无效动作执行(ActionEvent arg0){String worldSel = "";if(cbox_RanWrld.isSelected()){worldSel = "随机";} 别的 {worldSel = txt_Homeworld.getText();}Object[] row = {txt_Username.getText(), txt_Password.getText(), cbox_Members.isSelected(), worldSel};DefaultTableModel 模型 = (DefaultTableModel) tbl_Accounts.getModel();模型.addRow(row);}});添加(btn_Addacc);btn_Delacc.setLocation(365, 285);btn_Delacc.setSize(60, 20);btn_Delacc.setText("Del");btn_Delacc.addActionListener(new ActionListener(){@覆盖公共无效动作执行(ActionEvent arg0){DefaultTableModel 模型 = (DefaultTableModel) tbl_Accounts.getModel();}});添加(btn_Delacc);txt_Username.setLocation(80, 285);txt_Username.setSize(100, 20);txt_Username.setText("");txt_Username.setRows(5);txt_Username.setColumns(5);添加(txt_用户名);txt_Password.setLocation(80, 310);txt_Password.setSize(100, 20);txt_Password.setText("");txt_Password.setRows(5);txt_Password.setColumns(5);txt_Password.setTabSize(0);添加(txt_密码);txt_Homeworld.setLocation(395, 310);txt_Homeworld.setSize(30, 20);txt_Homeworld.setText("82");txt_Homeworld.setRows(5);txt_Homeworld.setColumns(5);txt_Homeworld.setTabSize(0);添加(txt_Homeworld);cbox_Members.setLocation(185, 285);cbox_Members.setSize(80, 20);cbox_Members.setText("成员");cbox_Members.setSelected(false);添加(cbox_Members);cbox_RanWrld.setLocation(185, 310);cbox_RanWrld.setSize(115, 20);cbox_RanWrld.setText("随机世界");cbox_RanWrld.setSelected(false);添加(cbox_RanWrld);setTitle("客户经理");设置大小(440、370);设置可见(真);setResizable(false);}公共静态无效主(字符串参数[]){新的accCreator();}}

解决方案

我知道这不是问题,因为其他一切都很好

哦……真的吗?不在我的电脑里...

让我们在我的 PC 上显示您的实际 GUI 的图片:

GUI 在您的计算机中看起来是否相同?我打赌没有.

但是……为什么在我的电脑上看起来是这样的?

好吧,正如上面@MadProgrammer 的评论中所述,这是因为 setLayout(null); 行.您可能想阅读

产生上图的代码是这样的:

import java.awt.BorderLayout;导入 java.awt.Dimension;导入 java.awt.FlowLayout;导入 java.awt.GridBagConstraints;导入 java.awt.GridLayout;导入 javax.swing.BoxLayout;导入 javax.swing.JButton;导入 javax.swing.JCheckBox;导入 javax.swing.JFrame;导入 javax.swing.JLabel;导入 javax.swing.JPanel;导入 javax.swing.JPasswordField;导入 javax.swing.JScrollPane;导入 javax.swing.JTable;导入 javax.swing.JTextField;导入 javax.swing.SwingUtilities;公共类 AccountCreator {私人 JFrame 框架;私人 JPanel 主面板;私人 JPanel 顶板;私人 JPanel 表窗格;私人 JPanel 底部面板;私人 JLabel selectAccountLabel;私有 JLabel 用户名标签;私人 JLabel 密码标签;私人 JLabel homeWorldLabel;私有 JTextField 用户名字段;私人 JTextField homeWorldField;私人 JPasswordField 密码字段;私人 JCheckBox 成员框;私人 JCheckBox randomBox;私人 JButton selectAccountButton;私有 JButton 添加按钮;私有 JButton 删除按钮;私有 JTable 表;私人 JScrollPane 滚动;公共静态无效主(字符串 [] args){SwingUtilities.invokeLater(new Runnable() {@覆盖公共无效运行(){new AccountCreator().createAndShowGui();}});}公共无效 createAndShowGui() {frame = new JFrame(getClass().getSimpleName());整数行 = 30;int cols = 3;String[][] data = new String[rows][cols];for (int i = 0; i <行; i++) {for (int j = 0; j < cols; j++) {data[i][j] = i + "-";+ j;}}String[] columnNames = { "Column1", "Column2", "Column3";};table = new JTable(data, columnNames);scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_​​ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_​​ALWAYS);table.setPreferredScrollableViewportSize(new Dimension(420, 250));table.setFillsViewportHeight(true);selectAccountLabel = new JLabel(选择账户");userNameLabel = new JLabel("用户名:");passwordLabel = new JLabel("密码:");homeWorldLabel = new JLabel(家庭世界");selectAccountButton = new JButton(选择账户");addButton = new JButton(添加");deleteButton = new JButton("Del");userNameField = new JTextField(10);passwordField = new JPasswordField(10);homeWorldField = new JTextField(3);membersBox = new JCheckBox("Members");randomBox = new JCheckBox(随机世界");topPane = new JPanel();topPane.setLayout(new BorderLayout());topPane.add(selectAccountLabel, BorderLayout.WEST);topPane.add(selectAccountButton, BorderLayout.EAST);tablePane = new JPanel();tablePane.add(scroll);bottomPane = new JPanel();bottomPane.setLayout(new GridLayout(0, 5, 3, 3));bottomPane.add(userNameLabel);bottomPane.add(userNameField);bottomPane.add(membersBox);bottomPane.add(addButton);bottomPane.add(deleteButton);bottomPane.add(passwordLabel);bottomPane.add(passwordField);bottomPane.add(randomBox);bottomPane.add(homeWorldLabel);bottomPane.add(homeWorldField);mainPane = new JPanel();mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));frame.add(topPane, BorderLayout.NORTH);frame.add(tablePane, BorderLayout.CENTER);frame.add(bottomPane, BorderLayout.SOUTH);框架.pack();frame.setVisible(true);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}

另外,你可能已经注意到 main() 方法是不同的,好吧,它里面的代码是把程序放在 事件调度线程 (EDT).

因此,请务必将其包含在您未来的计划中

I'm having a problem with a JFrame not showing a JTable that is added to it. I've tried getContentPane().add(..), I've switched to just add to keep the code a little shorter. Any help is more than appreciated!

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

    lbl_Homeworld.setLocation(310, 310);
    lbl_Homeworld.setSize(80, 20);
    lbl_Homeworld.setText("Home World:");
    add(lbl_Homeworld);

    btn_Select.setLocation(305, 5);
    btn_Select.setSize(120, 20);
    btn_Select.setText("Select Account");
    add(btn_Select);

    btn_Addacc.setLocation(300, 285);
    btn_Addacc.setSize(60, 20);
    btn_Addacc.setText("Add");
    btn_Addacc.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            String worldSel = "";
            if(cbox_RanWrld.isSelected()){
                worldSel = "Random";
            } else {
                worldSel = txt_Homeworld.getText();
            }
            Object[] row = {txt_Username.getText(), txt_Password.getText(), cbox_Members.isSelected(), worldSel};
            DefaultTableModel model = (DefaultTableModel) tbl_Accounts.getModel();
            model.addRow(row);
        }
    });
    add(btn_Addacc);

    btn_Delacc.setLocation(365, 285);
    btn_Delacc.setSize(60, 20);
    btn_Delacc.setText("Del");
    btn_Delacc.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            DefaultTableModel model = (DefaultTableModel) tbl_Accounts.getModel();

        }
    });
    add(btn_Delacc);

    txt_Username.setLocation(80, 285);
    txt_Username.setSize(100, 20);
    txt_Username.setText("");
    txt_Username.setRows(5);
    txt_Username.setColumns(5);
    add(txt_Username);

    txt_Password.setLocation(80, 310);
    txt_Password.setSize(100, 20);
    txt_Password.setText("");
    txt_Password.setRows(5);
    txt_Password.setColumns(5);
    txt_Password.setTabSize(0);
    add(txt_Password);

    txt_Homeworld.setLocation(395, 310);
    txt_Homeworld.setSize(30, 20);
    txt_Homeworld.setText("82");
    txt_Homeworld.setRows(5);
    txt_Homeworld.setColumns(5);
    txt_Homeworld.setTabSize(0);
    add(txt_Homeworld);

    cbox_Members.setLocation(185, 285);
    cbox_Members.setSize(80, 20);
    cbox_Members.setText("Members");
    cbox_Members.setSelected(false);
    add(cbox_Members);

    cbox_RanWrld.setLocation(185, 310);
    cbox_RanWrld.setSize(115, 20);
    cbox_RanWrld.setText("Random World");
    cbox_RanWrld.setSelected(false);
    add(cbox_RanWrld);

    setTitle("Account Manager");
    setSize(440, 370);
    setVisible(true);
    setResizable(false);

}

public static void main(String args[]) {
    new accCreator();
}
}

解决方案

I know thats not the problem tho because everything else shows up just fine

Oh... really? Not in my computer...

Let's have a picture of your actual GUI shown in my PC:

Does the GUI looks the same in your computer? I bet no.

But... why does it looks like that in my PC?

Well, as stated above in the comments by @MadProgrammer this is because of the setLayout(null); line. You might want to read Why is it frowned upon to use a null layout in Java Swing? for more information.

Now, that being said, you should also want to read and learn how to use the various layout managers that will let you create complex GUIs.

In your code you never set the location / bounds for scrollPane, and the size of it, so the component has a default size of 0, 0.

But... I think it's better to show you how you can get a really similar GUI (I'm in a hurry so I didn't make an even more similar GUI). You can copy-paste my code and see the same output (with slight differences because of the OS maybe) but text won't be cropped.

The code that produces the above image is this one:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class AccountCreator {

    private JFrame frame;
    private JPanel mainPane;
    private JPanel topPane;
    private JPanel tablePane;
    private JPanel bottomPane;

    private JLabel selectAccountLabel;
    private JLabel userNameLabel;
    private JLabel passwordLabel;
    private JLabel homeWorldLabel;

    private JTextField userNameField;
    private JTextField homeWorldField;
    private JPasswordField passwordField;

    private JCheckBox membersBox;
    private JCheckBox randomBox;

    private JButton selectAccountButton;
    private JButton addButton;
    private JButton deleteButton;

    private JTable table;

    private JScrollPane scroll;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AccountCreator().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        int rows = 30;
        int cols = 3;

        String[][] data = new String[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                data[i][j] = i + "-" + j;
            }
        }

        String[] columnNames = { "Column1", "Column2", "Column3" };

        table = new JTable(data, columnNames);

        scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        table.setPreferredScrollableViewportSize(new Dimension(420, 250));
        table.setFillsViewportHeight(true); 

        selectAccountLabel = new JLabel("Select Account");
        userNameLabel = new JLabel("Username: ");
        passwordLabel = new JLabel("Password: ");
        homeWorldLabel = new JLabel("Home world");

        selectAccountButton = new JButton("Select Account");
        addButton = new JButton("Add");
        deleteButton = new JButton("Del");

        userNameField = new JTextField(10);
        passwordField = new JPasswordField(10);
        homeWorldField = new JTextField(3);

        membersBox = new JCheckBox("Members");
        randomBox = new JCheckBox("Random world");

        topPane = new JPanel();
        topPane.setLayout(new BorderLayout());

        topPane.add(selectAccountLabel, BorderLayout.WEST);
        topPane.add(selectAccountButton, BorderLayout.EAST);

        tablePane = new JPanel();
        tablePane.add(scroll);

        bottomPane = new JPanel();
        bottomPane.setLayout(new GridLayout(0, 5, 3, 3));

        bottomPane.add(userNameLabel);
        bottomPane.add(userNameField);
        bottomPane.add(membersBox);
        bottomPane.add(addButton);
        bottomPane.add(deleteButton);
        bottomPane.add(passwordLabel);
        bottomPane.add(passwordField);
        bottomPane.add(randomBox);
        bottomPane.add(homeWorldLabel);
        bottomPane.add(homeWorldField);
        
        mainPane = new JPanel();
        mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));
        
        frame.add(topPane, BorderLayout.NORTH);
        frame.add(tablePane, BorderLayout.CENTER);
        frame.add(bottomPane, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Also, you might have noticed that the main() method is different, well, the code inside it is placing the program on the Event Dispatch Thread (EDT).

So, be sure to include it in your future programs

这篇关于JTable 未显示在 JFrame (Java) 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆