【问题标题】:JTextField only shows as a slit Using GridBagLayout, need helpJTextField 只显示为一个狭缝 使用 GridBagLayout,需要帮助
【发布时间】:2011-05-30 11:33:08
【问题描述】:

您好,提前感谢您的帮助,我正在尝试构建一个简单的程序来学习 GUI,但是当我运行 JTextFields 下面的代码时,所有的代码都显示为一个狭缝,甚至一个字符都不够大。

无法发布图片,但它看起来类似于:标签 [|

在哪里 [|是文本字段的实际样子

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.*;



public class lab6start implements ActionListener
{
    JTextField custNameTxt;
    JTextField acctNumTxt;
    JTextField dateCreatedTxt;
    JButton checkingBtn;
    JButton savingsBtn;
    JTextField witAmountTxt;
    JButton withDrawBtn;
    JTextField depAmountTxt;
    JButton depositBtn;

    lab6start()
    {
        JFrame bankTeller = new JFrame("Welcome to Suchnsuch Bank");
        bankTeller.setSize(500, 280);
        bankTeller.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        bankTeller.setResizable(false);
        bankTeller.setLayout(new GridBagLayout());

        bankTeller.setBackground(Color.gray);

        //bankTeller.getContentPane().add(everything, BorderLayout.CENTER);

        GridBagConstraints c = new GridBagConstraints();

        JPanel acctInfo = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.insets = new Insets(5,5,5,5);
        bankTeller.add(acctInfo, c);
        c.gridwidth = 1;

        //labels
        //name acct# balance interestRate dateCreated
        JLabel custNameLbl = new JLabel("Name");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(0,0,0,0);
        acctInfo.add(custNameLbl, c);

        custNameTxt = new JTextField("customer name",50);
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(custNameTxt,c);
        custNameTxt.requestFocusInWindow();

        JLabel acctNumLbl = new JLabel("Account Number");
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(acctNumLbl,c);

        acctNumTxt = new JTextField("account number");
        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(acctNumTxt,c);

        JLabel dateCreatedLbl = new JLabel("Date Created");
        c.gridx = 0;
        c.gridy = 2;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(dateCreatedLbl,c);

        dateCreatedTxt = new JTextField("date created");
        c.gridx = 1;
        c.gridy = 2;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(dateCreatedTxt,c);

        //buttons
        checkingBtn = new JButton("Checking");
        c.gridx = 0;
        c.gridy = 3;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(checkingBtn,c);

        savingsBtn = new JButton("Savings");
        c.gridx = 1;
        c.gridy = 3;
        c.insets = new Insets(5,5,5,5);
        acctInfo.add(savingsBtn,c);

//end of info panel

        JPanel withDraw = new JPanel(new GridBagLayout());
        c.gridx = 0;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        bankTeller.add(withDraw, c);

        witAmountTxt = new JTextField("Amount to Withdraw:");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        withDraw.add(witAmountTxt,c);

        withDrawBtn = new JButton("Withdraw");
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        withDraw.add(withDrawBtn,c);

        //add check balance

//end of withdraw panel

        JPanel deposit = new JPanel(new GridBagLayout());
        c.gridx = 1;
        c.gridy = 1;
        c.insets = new Insets(5,5,5,5);
        bankTeller.add(deposit, c);

        depAmountTxt = new JTextField("Amount to Deposit");
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        deposit.add(depAmountTxt,c);

        depositBtn = new JButton("Deposit");
        c.gridx = 1;
        c.gridy = 0;
        c.insets = new Insets(5,5,5,5);
        deposit.add(depositBtn,c);      

        bankTeller.setVisible(true);

        // action/event 
        checkingBtn.addActionListener(this);
        savingsBtn.addActionListener(this);
        withDrawBtn.addActionListener(this);
        depositBtn.addActionListener(this);

    }

    public void actionPerformed(ActionEvent e) 
    {
        if (e.getSource()== checkingBtn)
        {
            witAmountTxt.requestFocusInWindow();
            //checking newcheck = new checking();
        }

    }
}


/*
        String accountType = null;
        accountType = JOptionPane.showInputDialog(null, "Checking or Savings?");

        if (accountType.equalsIgnoreCase("checking"))
        {
            checking c_Account = new checking();
        }
        else if (accountType.equalsIgnoreCase("savings"))
        {
        //  savings s_Account = new savings();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Invalid Selection");
        }

    */

【问题讨论】:

    标签: java user-interface jtextfield gridbaglayout


    【解决方案1】:

    为我添加这些作品:

        c.weightx=1.;
        c.fill=GridBagConstraints.HORIZONTAL;
    

    【讨论】:

    • +1 效果很好,比 repack() 或一些 hack 更有效
    【解决方案2】:

    在添加所有内容之后和 setVisible(true) 之前尝试在 JFrame 上调用 pack()

    此外,您不要忘记设置 GridBagConstraints weightx 和 weighty 字段。至少给它们一个非 0 值,例如大多数字段为 1.0,如果 GUI 更改大小,则为不希望更改其大小的字段为 0。

    【讨论】:

    • 太棒了!泰!不知道 pack() 这正是需要的。当它显示时我知道尺寸有问题我只是不知道如何修复它。
    • 是的,这正是我们所需要的。优秀的答案+1
    • weightx 设置为 1 为我修复了它。谢谢!
    【解决方案3】:

    Swing 中还有一个错误可能导致 JTextArea 显示为一个狭缝,尽管 Sun/Oracle 说“这不是错误,它是一个特性”:

    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4247013

    有人在该线程上建议的一个潜在解决方案是设置 JTextField 的最小大小...类似这样:

    textField.setMinimumSize(textField.getPreferredSize());
    

    【讨论】:

    • 这让我能够修复我多年前编写的一个很快就崩溃的程序。我不应该只说谢谢,所以我不会,但你明白了....
    【解决方案4】:

    我从lab6这个名字猜到你以前可能没有使用过GridBagLayout。它是 Swing Layout 工具中最困难和最令人恐惧的工具之一。如果您还没有使用它,我建议您阅读以下教程: http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html 并建立你的榜样

    【讨论】:

    • 1+ 的好建议。此外,他应该考虑使用其他更容易使用更简单的布局管理器的 JPanel 来嵌套每个 JPanel。其他选项包括 miglayout。
    • 并为您的 pack() 和 setVisible() +1。我永远不会选择使用 GBL,除非需要 OP 为实验室执行此操作,否则我也会选择嵌套的 JPanel
    • 其实我有,但谢谢。这次我只是尝试了一些更冒险的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2015-08-06
    • 2012-02-20
    • 1970-01-01
    • 2022-07-28
    相关资源
    最近更新 更多