为什么我在 FlowLayout 中左侧的 TextField 不显示文本提示,而右侧的显示文本提示?

Why is my left TextField in FlowLayout not showing text prompt but the right one does?

嗨,为什么我的左边 JTextField 没有显示文本提示,而我的右边却显示? 我所说的 JTextField 叫做 gname 。 正在工作的 JTextField 称为 fid .

程序应该在有人按下 JButton 搜索时立即在 sql 数据库中搜索两个 JTextField 值。 我正在使用 swingx 库中包含的 Prompt Api。

Database

my problem

编辑:经过进一步测试,我发现当我按下“搜索”按钮时,两个提示都可见。我不知道为什么会这样或如何发生。

这是我的代码:

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import org.jdesktop.swingx.prompt.PromptSupport;

import com.mysql.jdbc.PreparedStatement;

public class errorbrowser implements ActionListener{

    JFrame frame = new JFrame();
    JPanel contentPanel = new JPanel(new CardLayout());

    private String db_host = "localhost";
    private String db_port = "3306";
    private String db_user = "root";
    private String db_pass = "password";
    private String db_base = "errormanager";

    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private java.sql.Connection connection = null;

    errorbrowser(){



        JPanel searchsite = new JPanel(new BorderLayout(5,6));
        searchsite.setBackground(Color.DARK_GRAY);

        JPanel searchbar = new JPanel(new FlowLayout());
        searchbar.setBackground(Color.DARK_GRAY);
    //  searchbar.setBorder(new LineBorder(Color.WHITE, 3));

        JTextField gname = new JTextField(10);
        JTextField fid = new JTextField(10);

        PromptSupport.setPrompt("Gerätebezeichnung",gname);
        gname.setOpaque(false);
        gname.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        gname.setForeground(Color.WHITE);
        gname.setFont(new Font("Dialog BOLD",Font.BOLD, 17));

        PromptSupport.setPrompt("FehlerID",fid);
        fid.setOpaque(false);
        fid.setBorder(javax.swing.BorderFactory.createEmptyBorder());
        fid.setForeground(Color.WHITE);
        fid.setFont(new Font("Dialog BOLD", Font.BOLD, 17));

        searchbar.add(gname);
        searchbar.add(fid);
        searchsite.add(searchbar, BorderLayout.NORTH);

        JButton search = new JButton("Search");
        search.setPreferredSize(new Dimension(100,60));
        search.setMaximumSize(new Dimension(100,60));
        search.setMaximumSize(new Dimension(100,60));
        searchsite.add(search,BorderLayout.CENTER);


        search.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String gerätename = gname.getText();
                String fehlerid = fid.getText();
                System.out.println("suche startet");
                try {
                    PreparedStatement st = (PreparedStatement) connection
                            .prepareStatement("Select gerätebezeichnung, fehlerid from rational where gerätebezeichnung=? and fehlerid=?");

                    st.setString(1, gerätename);
                    st.setString(2, fehlerid);

                    ResultSet results = st.executeQuery();
                    if(results.next()) {
                        System.out.println("Fehler gefunden!");
                    }else {
                        System.out.println("Fehler nicht gefunden");
                    }
                }catch(SQLException sqlException) {
                    sqlException.printStackTrace();
                }
            }
            });

        JPanel textbox = new JPanel();

        contentPanel.add(searchsite);

        frame.add(contentPanel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600,400);
        frame.setVisible(true);

    }


    public static void main(String[] args) {

        errorbrowser app = new errorbrowser();
        try {
            //Verbindung herstellen 
            app.createConnection();

        }catch(SQLException e) {
            System.err.println("SQL Fehler - "+ e.getLocalizedMessage());
            e.printStackTrace();
        }catch(ClassNotFoundException e) {
            System.err.println("Der Datenbank treiber wurde nicht gefunden. - " + e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    public void createConnection() throws SQLException, ClassNotFoundException{
        //Treiber initialisieren
        Class.forName(DRIVER);
        //Url für die Verbindung zu der Datenbank
        String mySqlUrl = "jdbc:mysql://" + db_host + ":" + db_port + "/" + db_base;
        //Verbindung herstellen
        connection = DriverManager.getConnection(mySqlUrl,db_user,db_pass);
    }

    public void showAll(String nameOfTable)throws SQLException{
            String query = "Select * FROM " + nameOfTable;
            java.sql.Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(query);
            int columns = rs.getMetaData().getColumnCount();
            System.out.println("Alle Einträge zu den Personen ");
            System.out.println();
            // Ich zeige die Tabellenspaltennamen an
            for(int i = 1;i<=columns;i++) {
                System.out.print(rs.getMetaData().getColumnLabel(i)+"\t\t");
            }
                System.out.println();
                System.out.println();
                //Ich zeige den Inhalt der Tabelle an

            while(rs.next()) {
                for(int j = 1;j<=columns;j++) {
                    System.out.print(rs.getString(j)+"\t\t");
                }
                System.out.println();
            }

            //Ich schließe die Streams wieder und gebe die Tabelle wieder frei
            rs.close();
            stmt.close();
        }
        //Ich schließe die Verbindung zu der Datenbank und gebe de Port am PC frei
    public void closeProgramm() throws SQLException{
            this.connection.close();
        }


    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }



}

提示正常,但焦点在左侧 TextBox,这使其不可见。

您可以在开始时将焦点放在 TextBox 之外(例如将其放在 JButton 上)

设置框架后,您可以像这样保持焦点:

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600,400);
    frame.setVisible(true);
    search.requestFocus();//set your focus on button
}