猿问

关于Java + Swing 输入提示问题,麻烦大神们帮忙看看

想做一个小软件,查询电话的,当输入号码时,自动在数据库中找到与之相匹配的,出现在下拉框选项里... ....Java + Swing 麻烦哪位大神给段代码

Cats萌萌
浏览 211回答 2
2回答

三国纷争

1. 来看看这个: 这是不是很难给它自己,顺便说一句.a对夫妇的监听器和自定义渲染,瞧的。2. 你可以创建自己的:import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.*;public class Main {public static void main(String[] args) {final JFrame frame = new JFrame();frame.setLayout(new BorderLayout());final JTextField textFieldA = new HintTextField("A hint here");final JTextField textFieldB = new HintTextField("Another hint here");frame.add(textFieldA, BorderLayout.NORTH);frame.add(textFieldB, BorderLayout.CENTER);JButton btnGetText = new JButton("Get text");btnGetText.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String message = String.format("textFieldA='%s', textFieldB='%s'",textFieldA.getText(), textFieldB.getText());JOptionPane.showMessageDialog(frame, message);}});frame.add(btnGetText, BorderLayout.SOUTH);frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);frame.setVisible(true);frame.pack();}}class HintTextField extends JTextField implements FocusListener {private final String hint;private boolean showingHint;public HintTextField(final String hint) {super(hint);this.hint = hint;this.showingHint = true;super.addFocusListener(this);}@Overridepublic void focusGained(FocusEvent e) {if(this.getText().isEmpty()) {super.setText("");showingHint = false;}}@Overridepublic void focusLost(FocusEvent e) {if(this.getText().isEmpty()) {super.setText(hint);showingHint = true;}}@Overridepublic String getText() {return showingHint ? "" : super.getText();}}如果你仍然在Java 1.5中,替换this.getText().isEmpty()同this.getText().length() == 0。3. 下面是一个类的复制/粘贴的解决方案:import java.awt.Color;import java.awt.Graphics;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.plaf.basic.BasicTextFieldUI;import javax.swing.text.JTextComponent;public class HintTextFieldUI extends BasicTextFieldUI implements FocusListener {private String hint;private boolean hideOnFocus;private Color color;public Color getColor() {return color;}public void setColor(Color color) {this.color = color;repaint();}private void repaint() {if(getComponent() != null) {getComponent().repaint();}}public boolean isHideOnFocus() {return hideOnFocus;}public void setHideOnFocus(boolean hideOnFocus) {this.hideOnFocus = hideOnFocus;repaint();}public String getHint() {return hint;}public void setHint(String hint) {this.hint = hint;repaint();}public HintTextFieldUI(String hint) {this(hint,false);}public HintTextFieldUI(String hint, boolean hideOnFocus) {this(hint,hideOnFocus, null);}public HintTextFieldUI(String hint, boolean hideOnFocus, Color color) {this.hint = hint;this.hideOnFocus = hideOnFocus;this.color = color;}@Overrideprotected void paintSafely(Graphics g) {super.paintSafely(g);JTextComponent comp = getComponent();if(hint!=null && comp.getText().length() == 0 && (!(hideOnFocus && comp.hasFocus()))){if(color != null) {g.setColor(color);} else {g.setColor(comp.getForeground().brighter().brighter().brighter());}int padding = (comp.getHeight() - comp.getFont().getSize())/2;g.drawString(hint, 2, comp.getHeight()-padding-1);}}@Overridepublic void focusGained(FocusEvent e) {if(hideOnFocus) repaint();}@Overridepublic void focusLost(FocusEvent e) {if(hideOnFocus) repaint();}@Overrideprotected void installListeners() {super.installListeners();getComponent().addFocusListener(this);}@Overrideprotected void uninstallListeners() {super.uninstallListeners();getComponent().removeFocusListener(this);}}像这样使用它:TextField field = new JTextField();field.setUI(new HintTextFieldUI("Search", true));请注意,它是发生在protected void paintSafely(Graphics g)。4. 对于任何(也就是,任何一个扩展JComponent的),你可以调用的setToolTipText(String)方法。 参考以下链接: 为的setToolTipText API “如何使用工具提示”教程5. 如果您仍然寻找一个解决方案 CodeGo.net,这里有一个其他的答案(巴特Kiers和culmat),供大家参考:import javax.swing.*;import javax.swing.text.JTextComponent;import java.awt.*;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;public class HintTextField extends JTextField implements FocusListener{private String hint;public HintTextField (){this("");}public HintTextField(final String hint){setHint(hint);super.addFocusListener(this);}public void setHint(String hint){this.hint = hint;setUI(new HintTextFieldUI(hint, true));//setText(this.hint);}public void focusGained(FocusEvent e){if(this.getText().length() == 0){super.setText("");}}public void focusLost(FocusEvent e){if(this.getText().length() == 0){setHint(hint);}}public String getText(){String typed = super.getText();return typed.equals(hint)?"":typed;}}class HintTextFieldUI extends javax.swing.plaf.basic.BasicTextFieldUI implements FocusListener{private String hint;private boolean hideOnFocus;private Color color;public Color getColor(){return color;}public void setColor(Color color){this.color = color;repaint();}private void repaint(){if(getComponent() != null){getComponent().repaint();}}public boolean isHideOnFocus(){return hideOnFocus;}public void setHideOnFocus(boolean hideOnFocus){this.hideOnFocus = hideOnFocus;repaint();}public String getHint(){return hint;}public void setHint(String hint){this.hint = hint;repaint();}public HintTextFieldUI(String hint){this(hint, false);}public HintTextFieldUI(String hint, boolean hideOnFocus){this(hint, hideOnFocus, null);}public HintTextFieldUI(String hint, boolean hideOnFocus, Color color){this.hint = hint;this.hideOnFocus = hideOnFocus;this.color = color;}protected void paintSafely(Graphics g){super.paintSafely(g);JTextComponent comp = getComponent();if(hint != null && comp.getText().length() == 0 && (!(hideOnFocus && comp.hasFocus()))){if(color != null){g.setColor(color);}else{g.setColor(Color.gray);}int padding = (comp.getHeight() - comp.getFont().getSize()) / 2;g.drawString(hint, 5, comp.getHeight() - padding - 1);}}public void focusGained(FocusEvent e){if(hideOnFocus) repaint();}public void focusLost(FocusEvent e){if(hideOnFocus) repaint();}protected void installListeners(){super.installListeners();getComponent().addFocusListener(this);}protected void uninstallListeners(){super.uninstallListeners();getComponent().removeFocusListener(this);}}Usage:HintTextField field = new HintTextField();field.setHint("Here's a hint");

噜噜哒

JOptionPane.showConfirmDialog (null, "本文档正在打印。允许要打印吗?", "友情提示", JOptionPane.YES_NO_OPTION);
随时随地看视频慕课网APP
我要回答