JTextField只显示为一个狭缝,使用GridBagLayout布局,需要帮助。

16

你好,提前感谢任何帮助。我试图构建一个简单的程序来学习GUI,但是当我运行下面的代码时,我的JTextFields都显示为不足以容纳一个字符的狭缝。

不能发布图片,但它看起来类似于:Label [|

其中[|是文本字段实际上的样子

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

    */
4个回答

13

对我来说,添加那些东西有效:

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

+1非常好,比repack()或某些hack要高效得多。 - wonton

10

在添加所有组件之后,在调用setVisible(true)之前尝试对JFrame调用pack()。

此外,您不会忘记设置GridBagConstraints的weightx和weighty字段。对于大多数字段,请至少给它们一个非0值,例如1.0,并为那些在GUI更改大小时不想更改大小的字段设置0。


太棒了!谢谢!我不知道pack(),但这正是所需的。当它显示时,我知道大小有问题,只是不知道如何修复它。 - Bill.Caffery
是的,这正是所需之物。非常好的答案 +1 - gcoulby
weightx 设置为1解决了我的问题。谢谢! - John R Perry

9

Swing中还存在一个问题,可能会导致JTextArea以缝隙的形式显示,虽然Sun/Oracle表示“这不是一个bug,而是一个功能”:

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

在该线程中,有人提出了一个潜在的解决方案,即设置JTextField的最小大小...类似于这样:

textField.setMinimumSize(textField.getPreferredSize());

这使我能够修复几年前写的一个程序,它很快就崩溃了。我不应该只是说谢谢,所以我不会,但你明白我的意思... - Nat Kuhn

1

1+ 非常好的建议。此外,他应该考虑嵌套使用每个使用其他更容易使用的简单布局管理器的 JPanel。其他选项包括 miglayout。 - Hovercraft Full Of Eels
而且加1分给你的pack()和setVisible()。我从不会选择使用GBL,除非OP必须在实验室中这样做,否则我也会选择嵌套的JPanels。 - peter.murray.rust
我已经有了,但还是谢谢你。这次我只是尝试了一些更具冒险精神的东西。 - Bill.Caffery

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接