有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java文本字段显示不正确,它看起来像一条狭缝

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

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

共 (2) 个答案

  1. # 1 楼答案

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

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

    JTextField not appearing properly, it is appearing like a vertical line

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

    由于使用了pack(),因此基本代码看起来很合理。但是,您可能会在其他地方使用setSize(…)覆盖pack()或者别的什么

    如果您需要更多帮助,请发布一个适当的SSCCE来演示您的问题

  2. # 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();
        }
    }
    

    enter image description here