【问题标题】:Java Swings JTextField not appearing properly, it is appearing like a slitJava Swing JTextField 没有正确显示,它看起来像一个狭缝
【发布时间】:2014-05-13 14:39:53
【问题描述】:

我创建了一个JLabel,它显示了一个背景图像,然后我添加了三个按钮和一个JTextFieldJLable 的布局是GridBagLayout,但主要问题是JTextField。我的错误是什么?我试过preferredSizesetBounds。我是GridBagLayout 的新手。

    p = new JPanel();
    ImageIcon img = new ImageIcon("C:/Users/manpreet/Desktop/G.L.E.W/3.jpg");
    l = new JLabel(img);

        practice = new JButton("Practice (No Time Limit)");
        Challenge = new JButton("Challenge (60 Seconds)");

        l.setLayout(g);
        practice.setFont(new Font("Garamond", Font.BOLD, 20));
        practice.setForeground(Color.yellow);
        practice.setBackground(Color.black);

Challenge.setBackground(Color.black);
    Challenge.setFont(new Font("Garamond", Font.BOLD, 20));
        Challenge.setForeground(Color.yellow);


        gbc.gridx = 0;
        gbc.gridy = 0;

        l.add(practice,gbc);

        gbc.gridwidth=1;
    Insets insets1 = new Insets(2,2,2,2);
        gbc.insets = insets1;

        gbc.gridx = 2;
        gbc.gridy = 0;

        l.add(Challenge,gbc);
        gbc.gridx=1;
        gbc.gridy = 1;

        t = new JTextField("TextField");
        //t.setPreferredSize(new Dimension(150,20));      
        //t.setBounds(5,5,20,20);

        start = new JButton("start");
        t.setVisible(true);
        start.setVisible(true);
        l.add(t,gbc);
        gbc.gridx=1;
        gbc.gridy = 2;
        l.add(start,gbc);           
        p.add(l);
        f.add(p);       
        f.pack();
        practice.addActionListener(this);
        Challenge.addActionListener(this);

【问题讨论】:

  • 删除 f.pack()。这应该可以帮助您朝着正确的方向前进
  • @Sanjeev 不,不要删除 pack()。 pack() 需要以首选大小显示组件。问题是文本字段未以其首选大小显示。
  • 所以你想将按钮和文本字段添加到带有图片的标签中?您看到按钮和图像,但 textField 显示为一条线?只是需要澄清一下你所看到的。
  • 谢谢大家,但实际上我发现了我的错误,使用 gridwidth=1 后代码工作正常,感谢您的时间。

标签: java swing jtextfield gridbaglayout


【解决方案1】:

首先,如果您希望人们花时间查看您的代码,那么您应该:

  1. 使用有意义的变量名。 “l” “t” 没有任何意义,很难按照代码理解这些变量代表什么。
  2. 遵循 Java 命名约定。变量名不应以大写字符开头。看起来您的所有变量名称(“挑战”除外)都是正确的。保持一致!!!

JTextField 没有正确显示,它看起来像一条垂直线

当框架中没有足够的空间以首选尺寸显示组件时,就会发生这种情况。然后 GridBagLayout 将以最小尺寸显示组件,这就是为什么你会得到一个非常小的文本字段。

基本代码看起来很合理,因为您执行了 pack()。但是,也许在其他地方你用 setSize(...) 或其他东西覆盖 pack()。

如果您需要更多帮助,请发布正确的 SSCCE 来证明您的问题。

【讨论】:

  • 从现在开始我将按照约定来命名变量。谢谢
【解决方案2】:

1) 您可以为JTextField 指定列数,例如:t = new JTextField("TextField",20);

2) 借助 GridBagConstraints 参数:weightxfill

3) 避免使用:

 setPreferredSize(new Dimension(150,20));      
 setBounds(5,5,20,20);

阅读更多关于How to Use GridBagLayout的信息。

例子:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;


public class TestFrame extends JFrame{

    public TestFrame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        pack();
        setVisible(true);
    }

    private void init() {
        setLayout(new GridBagLayout());

        JTextField f1 = new JTextField("text",10);
        JTextField f2 = new JTextField("text");

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST;
        add(f1,c);
        c.gridy = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        add(f2,c);
    }


    public static void main(String... strings) {
        new TestFrame();
    }
}

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 1970-01-01
    • 2020-01-10
    • 2014-01-14
    相关资源
    最近更新 更多