ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JTable没有出现在JFrame(Java)上

2019-09-16 01:00:15  阅读:181  来源: 互联网

标签:java swing jtable jframe


我遇到的问题是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);

    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

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

让我们看一下我PC上显示的实际GUI:

enter image description here

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

但是……为什么它在我的电脑里看起来像那样?

好吧,正如上面@MadProgrammer的评论所述,这是因为setLayout(null);线.您可能需要阅读Null layout is evilWhy is it frowned upon to use a null layout in Java Swing?以获取更多信息.

现在,话虽如此,您还应该阅读并学习如何使用各种layout managers来创建复杂的GUI.

在您的代码中,您永远不会设置scrollPane的位置/边界及其大小,因此该组件的默认大小为0,0.

但是……我认为最好向您展示如何获得一个非常相似的GUI(我很匆忙所以我没有制作更相似的GUI).您可以复制粘贴我的代码并查看相同的输出(由于操作系统可能略有不同),但不会裁剪文本.

enter image description here

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

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

另外,您可能已经注意到main()方法不同,其中的代码是将程序放在Event Dispatch Thread (EDT)上.

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

标签:java,swing,jtable,jframe
来源: https://codeday.me/bug/20190916/1806521.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有