Java 密码框无法显示的问题

tangqing24680 2018-12-05 10:24:02

package com.src.self;

import java.awt.*;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class LoginFrame {
//添加组件的方法
public void addComponentsToPane(Container pane) {
JButton buttonOk,buttonCancel;

JLabel labelName,labelPassword;//账户名和密码标签足
JTextField textFieldName;
JPasswordField textFieldPwd;

//创建一个顶部面板
JPanel topPane=new JPanel();
topPane.setLayout(new GridBagLayout());

//创建一个底部面板
JPanel bottomPane=new JPanel();
FlowLayout flowLayout=new FlowLayout();
flowLayout.setHgap(20);
flowLayout.setVgap(10);
bottomPane.setLayout(flowLayout);

//标签账户
GridBagConstraints conLabelName=new GridBagConstraints();
conLabelName.fill=GridBagConstraints.NONE;
labelName=new JLabel("账户:");
conLabelName.weightx=0.2;
conLabelName.gridx=0;
conLabelName.gridy=0;
conLabelName.anchor=GridBagConstraints.LINE_END;
topPane.add(labelName,conLabelName);

//账户文本框
GridBagConstraints conTextFieldName=new GridBagConstraints();
conTextFieldName.fill=GridBagConstraints.HORIZONTAL;
textFieldName=new JTextField();
conTextFieldName.weightx=0.8;
conTextFieldName.weighty=0.5;
conTextFieldName.gridx=1;
conTextFieldName.gridy=0;
conTextFieldName.insets=new Insets(10, 0, 10, 20);
topPane.add(textFieldName,conTextFieldName);

//标签密码
GridBagConstraints conLabelPassword=new GridBagConstraints();
conLabelPassword.fill=GridBagConstraints.NONE;
labelPassword=new JLabel("密码:");
conLabelPassword.gridx=0;
conLabelPassword.gridy=1;
conLabelPassword.anchor=GridBagConstraints.LINE_END;
topPane.add(labelPassword,conLabelPassword);

//密码框
GridBagConstraints conTextFieldPwd=new GridBagConstraints();
conTextFieldName.fill=GridBagConstraints.HORIZONTAL;
textFieldPwd=new JPasswordField("");
conTextFieldPwd.weighty=0.5;
conTextFieldPwd.gridx=1;
conTextFieldPwd.gridy=1;
conTextFieldPwd.insets=new Insets(0, 0, 0, 20);
topPane.add(textFieldPwd,conTextFieldPwd);

//添加确定取消面板
buttonOk=new JButton("确定");
buttonCancel=new JButton("取消");

bottomPane.add(buttonOk);
bottomPane.add(buttonCancel);
pane.add(topPane,BorderLayout.CENTER);
pane.add(bottomPane,BorderLayout.PAGE_END);

}
private void createAndShowGUI() {
JFrame frame=new JFrame("LoginFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
UIManager.put("swing.boldMetal", Boolean.FALSE);//关闭粗体字
new LoginFrame().createAndShowGUI();
}
});
}

}



运行的时候,密码框怎么会这样,大神们,帮忙解答下!!!
...全文
175 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_39936465 2018-12-05
  • 打赏
  • 举报
回复
textFieldPwd=new JPasswordField("");你JPasswordField设置为空,所以显示不了。 JPasswordField的构造 JPasswordField() JPasswordField(String text) JPasswordField(int columns) JPasswordField(String text, int columns) text: 默认显示的文本 columns: 用来计算首选宽度的列数;如果列设置为 0,则首选宽度将是组件实现的自然结果 修改为 textFieldPwd=new JPasswordField(12);

62,626

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧