我正在编写自定义文件选择组件.在我的UI中,首先用户点击一个按钮,弹出一个按钮JFileChooser
; 当它关闭时,所选文件的绝对路径被写入a JTextField
.
问题是,绝对路径通常很长,导致文本字段放大,使其容器太宽.
我试过这个,但它没有做任何事情,文本字段仍然太宽:
fileNameTextField.setMaximumSize(new java.awt.Dimension(450, 2147483647));
Run Code Online (Sandbox Code Playgroud)
目前,当它是空的时,它已经是400px长,因为GridBagConstraints
附着它.
我希望它像HTML页面中的文本字段一样,它具有固定的大小,并且在输入太长时不会放大.
那么,如何设置最大尺寸JTextField
?
我想在灰色和白色之间交替作为背景,从我的JTextPane的一行到下一行.我尝试通过重载paintComponent()方法并根据组件高度和字体大小手动绘制背景来做到这一点,但我没有成功.
任何提示?
我正在尝试用图像和提示创建一些看起来更漂亮的JTextFields.为此,我做了一个覆盖paintComponent方法的装饰器.我使用装饰器的原因是我想将它应用于其他类型的JTextField,例如JPasswordField.
这是我到目前为止所做的;
左边的表格中看到的问题是,即使我使用了JPasswordField,paintComponent也似乎忽略了我所假设的密码paintComponent,它可能是密码屏蔽符号.
所以问题是,如何避免重复JTextFields和JPasswordFields的代码,但仍然具有不同的功能,如密码屏蔽.
这是装饰器代码;
public class JTextFieldHint extends JTextField implements FocusListener{
private JTextField jtf;
private Icon icon;
private String hint;
private Insets dummyInsets;
public JTextFieldHint(JTextField jtf, String icon, String hint){
this.jtf = jtf;
setIcon(createImageIcon("icons/"+icon+".png",icon));
this.hint = hint;
Border border = UIManager.getBorder("TextField.border");
JTextField dummy = new JTextField();
this.dummyInsets = border.getBorderInsets(dummy);
addFocusListener(this);
}
public void setIcon(Icon newIcon){
this.icon = newIcon;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int textX = 2;
if(this.icon!=null){
int iconWidth = icon.getIconWidth();
int iconHeight = icon.getIconHeight(); …
Run Code Online (Sandbox Code Playgroud) 因此,您可能知道,如果您有一个文本字段并且向其添加了ActionListener,它将只收听Enter按钮的按键.但是,我想让我的ActionListener监听文本中的更改.所以基本上我有这个:
public static JPanel mainPanel() {
JPanel mainp = new JPanel();
JTextArea areap = new JTextArea("Some text in the textarea");
JTextField fieldp = new JTextField("Edit this");
areap.setEditable(false);
fieldp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(//change in textfield, for instance a letterpress or space bar)
{
//Do this
}
}
});
mainp.add(areap);
mainp.add(fieldp);
return mainp;
}
Run Code Online (Sandbox Code Playgroud)
我可以通过任何方式收听文本中的更改(如actionPerformed事件中记录的那样)?
我试图建立一个javax.swing.JTextField
与javax.swing.JList
用于自动完成像谷歌.
当写一个单词时,Google
显示几个匹配和
我的应用是关于圣经,我想在研究圣经时寻找一个特定的词.我已经看过Java2sAutoTextField,但箭头键没有这种特殊的行为.
在Food Tab中,我想实现这一目标
但我只能得到这个
如何增加JTextField
Food Tab中的宽度?以下是我的代码
public class FoodOrdering {
static private JFrame frame;
static private JTextField textField;
static private GridBagConstraints gbc;
static private JLabel[] foodLabel;
static private JLabel[] labels;
static private JTextField[] qtyField;
static private JLabel[] foodImage;
static private File[] file;
private static final int ELEMENTS = 9;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FoodOrdering window = new FoodOrdering();
window.frame.setVisible(true);
} …
Run Code Online (Sandbox Code Playgroud) JTextField有一个keyTyped事件,但它似乎在它触发时单元格的内容尚未更改.
因为.如果在这里阅读,.length()总是错误的.
必须有一种简单的方法来获得按键击中后用户看到的长度?
我有一个JLabel
,当你点击它替换为a JTextField
,我需要JTextField
自动选择它出现时的所有文本.
感谢分配给我们的帮助.
我试图让我的JTextField填充宽度并为其设置高度但仍然失败.我尝试添加代码setPreferredSize(new Dimension(320,200));
但仍然失败.有什么方法可以让我的JTextField填充宽度并将高度设置为200或其他什么?
我有自己的Dialog弹出两个文本字段,两个JLabel和一个"ok"JButton.弹出窗口是一个登录窗口.窗口工作完美我只是想知道我如何能够添加"取消"JButton,因此用户可以取消登录.
这是我的窗口代码:
public Hashtable<String, String> login(JFrame frame) {
Hashtable<String, String> logininformation = new Hashtable<String, String>();
JPanel panel = new JPanel(new BorderLayout(5, 5));
JPanel label = new JPanel(new GridLayout(0, 1, 2, 2));
label.add(new JLabel("E-Mail", SwingConstants.RIGHT));
label.add(new JLabel("Password", SwingConstants.RIGHT));
panel.add(label, BorderLayout.WEST);
JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
JTextField username = new JTextField();
controls.add(username);
JPasswordField password = new JPasswordField();
controls.add(password);
panel.add(controls, BorderLayout.CENTER);
JOptionPane.showMessageDialog(frame, panel, "login", JOptionPane.QUESTION_MESSAGE);
logininformation.put("user", username.getText());
logininformation.put("pass", new String(password.getPassword()));
return logininformation;
}
Run Code Online (Sandbox Code Playgroud)
如果需要,请点击登录窗口的屏幕截图:
如果你点击右下角的"x",它也会关闭.但我想要取消JButton,如果它很容易.