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

时间:2020-01-09 14:48:38

标签: java swing jtextfield prompt swingx

嗨,为什么我的左JTextField没有显示文本提示符,而我的右JTextField为什么呢? 我正在谈论的gname被称为JTextField。 正在运行的fid称为JTextField

一旦有人按下JButton搜索,该程序就应该在sql数据库中搜索两个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 } } 值。 我正在使用swingx库中包含的Prompt Api。

Database

my problem

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

这是我的代码:

class TrainOneModel

1 个答案:

答案 0 :(得分:1)

提示正在起作用,但焦点位于左侧的TextBox上,这使其不可见。

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

设置好帧后,您可以像这样保持焦点:

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