简体   繁体   中英

JTextField Does not show up initially, only when clicking on it?

So this problem is really giving me a headache because for some reason last night when i was working on it, my code ran perfectly and my textfields would show up without a problem...

Go to bed, wake up, time to work on it again aaaaaand bam. Now my JtextFields only show up when i highlight them or click them or something...I was wondering what could be wrong?

My code is really just messy and crappy at this point while i figure out a better way to design my program...

I thought it was just eclipse but netbeans is giving me the same issue.

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.awt.*;
import javax.swing.border.*;


public class DiffuserCalc {
    //create the class data fields
    private double Qts;
    private double Qes;
    private double Vas;
    JFrame ProgramBounds = new JFrame();
    JLabel label1= new JLabel("Qts");
    JLabel label2= new JLabel("Qes");
    JLabel label3= new JLabel("Fs");
    JLabel label4= new JLabel("BL");
    JLabel label5= new JLabel("Xmax");
    JLabel label6= new JLabel("Fs");
    JLabel label7= new JLabel("Vas");
    JLabel label8= new JLabel("Diameter");
    JLabel label9= new JLabel("Pmax (RMS)"); 
    JTextField QtsParam = new JTextField("Value");
    JTextField QesParam = new JTextField("Value");
    JTextField FsParam = new JTextField("   ");
    JTextField BLParam = new JTextField("   ");
    JTextField XmaxParam = new JTextField("   ");

    Font myFont = new Font("Tahoma", Font.BOLD, 20);

    DiffuserCalc()
    {
        ProgramBounds.setTitle("Box Designer");
        JPanel ParameterMenu = new JPanel();
        JPanel FieldInputs = new JPanel();


        ParameterMenu.setBounds(30, 0, 1180, 120);
        FieldInputs.setBounds(0,0, 1280, 720);
        ProgramBounds.add(ParameterMenu);
        ProgramBounds.add(FieldInputs);
        ProgramBounds.setSize(1280,720);



        // LAYOUT

        ParameterMenu.setLayout(new FlowLayout(FlowLayout.CENTER, 60, 10));

        FieldInputs.setLayout(null);

        Border lineBdr = BorderFactory.createLineBorder(Color.BLACK);
        Border BlackBorder = BorderFactory.createTitledBorder(lineBdr, "    T/S Parameters    ", TitledBorder.CENTER, TitledBorder.TOP, myFont, Color.black);

        //FIELD PROPERTIES

        label1.setFont(myFont);
        label2.setFont(myFont);
        label3.setFont(myFont);
        label4.setFont(myFont);
        label5.setFont(myFont);
        label6.setFont(myFont);
        label7.setFont(myFont);
        label8.setFont(myFont);
        label9.setFont(myFont);

        // PARAMETER BOUNDS
        int XLoc = 150;
        int YLoc = 70;
        QtsParam.setBounds(XLoc, YLoc, 40, 20);
        QesParam.setBounds(XLoc+95, YLoc, 40, 20);
        FsParam.setBounds(XLoc+190, YLoc, 40, 20);



        // ADD FIELDS
        ParameterMenu.add(label1);
        ParameterMenu.add(label2);
        ParameterMenu.add(label3);
        ParameterMenu.add(label4);
        ParameterMenu.add(label5);
        ParameterMenu.add(label6);
        ParameterMenu.add(label7);
        ParameterMenu.add(label8);
        ParameterMenu.add(label9);

        ParameterMenu.setBorder(BlackBorder);

        FieldInputs.add(QtsParam);
        FieldInputs.add(QesParam);
        FieldInputs.add(FsParam);
        FieldInputs.add(BLParam);
        FieldInputs.add(XmaxParam);




        // set everything proper
        QtsParam.requestFocus();
        ParameterMenu.setVisible(true);
        FieldInputs.setVisible(true);
        ProgramBounds.setVisible(true);
    }
    public double BoxDimension(int x, int y)
    {
        return x;
    }
    public static void main(String[] args) {

        DiffuserCalc MainProgram = new DiffuserCalc();


    }

}

Your code only sets the bounds for 3 text fields but you add 5 text fields to the panel.

Don't use a null layout!!!

Use a proper layout manager and then you don't have to worry about making mistakes like this.

Also, follow Java naming conventions. Variable names do NOT start with an upper case character.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;


public class DiffuserCalc {
    //create the class data fields
    private double qts;
    private double qes;
    private double vas;
    private JFrame programBounds = new JFrame();
    private JLabel label1= new JLabel("Qts");
    private JLabel label2= new JLabel("Qes");
    private JLabel label3= new JLabel("Fs");
    private JLabel label4= new JLabel("BL");
    private JLabel label5= new JLabel("Xmax");
    private JLabel label6= new JLabel("Fs");
    private JLabel label7= new JLabel("Vas");
    private JLabel label8= new JLabel("Diameter");
    private JLabel label9= new JLabel("Pmax (RMS)"); 
    private JTextField qtsParam = new JTextField("Value");
    private JTextField qesParam = new JTextField("Value");
    private JTextField fsParam = new JTextField("");
    private JTextField bLParam = new JTextField("");
    private JTextField xmaxParam = new JTextField("");

    private Font myFont = new Font("Tahoma", Font.BOLD, 20);

    DiffuserCalc()
    {
        programBounds.setTitle("Box Designer");
        JPanel parameterMenu = new JPanel();
        JPanel labelPanel = new JPanel();
        JPanel fieldInputs = new JPanel();

        // LAYOUT
        programBounds.setLayout(new BorderLayout());
        parameterMenu.setLayout(new BorderLayout());
        fieldInputs.setLayout(new FlowLayout(FlowLayout.LEFT));
        fieldInputs.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        labelPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        labelPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);


        programBounds.add(parameterMenu, BorderLayout.NORTH);
        parameterMenu.add(labelPanel, BorderLayout.NORTH);
        parameterMenu.add(fieldInputs, BorderLayout.SOUTH);
//        programBounds.add(fieldInputs);
        programBounds.setSize(1280,720);

        Border lineBdr = BorderFactory.createLineBorder(Color.BLACK);
        Border BlackBorder = BorderFactory.createTitledBorder(lineBdr, "    T/S Parameters    ", TitledBorder.CENTER, TitledBorder.TOP, myFont, Color.black);

        //FIELD PROPERTIES
        label1.setFont(myFont);
        label2.setFont(myFont);
        label3.setFont(myFont);
        label4.setFont(myFont);
        label5.setFont(myFont);
        label6.setFont(myFont);
        label7.setFont(myFont);
        label8.setFont(myFont);
        label9.setFont(myFont);

        // ADD FIELDS
        labelPanel.add(label1);
        labelPanel.add(label2);
        labelPanel.add(label3);
        labelPanel.add(label4);
        labelPanel.add(label5);
        labelPanel.add(label6);
        labelPanel.add(label7);
        labelPanel.add(label8);
        labelPanel.add(label9);

        parameterMenu.setBorder(BlackBorder);

        qtsParam.setColumns(3);
        fieldInputs.add(qtsParam);
        qesParam.setColumns(3);
        fieldInputs.add(qesParam);
        fsParam.setColumns(2);
        fieldInputs.add(fsParam);
        bLParam.setColumns(2);
        fieldInputs.add(bLParam);
        xmaxParam.setColumns(2);
        fieldInputs.add(xmaxParam);

        // set everything proper
        qtsParam.requestFocus();
        programBounds.pack();
        programBounds.setVisible(true);
    }
    public double BoxDimension(int x, int y)
    {
        return x;
    }
    public static void main(String[] args) {

        DiffuserCalc MainProgram = new DiffuserCalc();


    }
}

So I rewrote the class for you to be more according to the Java style standard . Next not using a Layout manager is asking for trouble. And even though your requirements are like you say it's still better to put the effort to use a Layout manager as you'll keep running into problems like these. Read more about layout managershere . Furthermore don't call setVisible on JPanels that you added to a frame. When you call setVisible on the JFrame it will call setVisible on all of it child components.

Next call the method setColumns on the JTextField instead of initializing it with spaces for more consistent and predictable behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM