嗨,提前感谢你的任何帮助,我正在尝试建立一个简单的程序来学习图形用户界面,但当我运行下面的代码时,我的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");
}
*/发布于 2010-12-25 01:56:54
尝试在添加所有内容之后、setVisible之前在JFrame上调用pack() (True)
此外,您不会想要忘记设置GridBagConstraints weighty和weighty字段。至少给它们一个非0的值,比如1.0代表大多数字段,0代表如果GUI改变大小,你不想改变大小的字段。
发布于 2012-09-04 02:35:01
添加这些对我来说是有效的:
c.weightx=1.;
c.fill=GridBagConstraints.HORIZONTAL;发布于 2011-05-16 22:18:16
Swing中也有一个bug,可能导致JTextArea显示为一个狭缝,尽管Sun/Oracle表示“这不是一个bug,而是一个特性”:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4247013
有人在该线程上建议的一个潜在解决方案是设置JTextField的最小大小……如下所示:
textField.setMinimumSize(textField.getPreferredSize());https://stackoverflow.com/questions/4527604
复制相似问题