我正在使用Swing登录屏幕。我编辑了屏幕上唯一的按钮“ Entrar”的enable属性,如下图所示:
但是,当我按下Shift-F6(运行文件)时,它没有显示更改:
Login Screen after running single file
代码如下:
package view;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
/**
*
* @author User
*/
public class Inicio extends javax.swing.JFrame {
String usuario;
String senha;
int a;
public Inicio() {
initComponents();
DocumentListener dUsuario = new DocumentListener(){
@Override
public void insertUpdate(DocumentEvent e) {
try {
usuario = e.getDocument().getText(0, 0);
a = 1;
} catch (BadLocationException ex) {
Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void removeUpdate(DocumentEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void changedUpdate(DocumentEvent e) {
try {
usuario = e.getDocument().getText(0, 0);
a = 1;
} catch (BadLocationException ex) {
Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
DocumentListener dSenha = new DocumentListener(){
@Override
public void insertUpdate(DocumentEvent e) {
try {
senha = e.getDocument().getText(0, 0);
a = 2;
} catch (BadLocationException ex) {
Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void removeUpdate(DocumentEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void changedUpdate(DocumentEvent e) {
try {
senha = e.getDocument().getText(0, 0);
a = 2;
} catch (BadLocationException ex) {
Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
tUsuario.getDocument().addDocumentListener(dUsuario);
tSenha.getDocument().addDocumentListener(dSenha);
if(a == 2) {
if (bEntrar.isEnabled() == false) {
bEntrar.setEnabled(true);
}
}else{
if (bEntrar.isEnabled() == true) {
bEntrar.setEnabled(false);
}
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
tSenha = new javax.swing.JPasswordField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
tUsuario = new javax.swing.JFormattedTextField();
bEntrar = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
jLabel1.setForeground(new java.awt.Color(153, 153, 153));
jLabel1.setText("Informe apenas os dígitos do CPF.");
jLabel2.setText("Senha:");
jLabel3.setText("CPF:");
jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel4.setText("LOGIN");
try {
tUsuario.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
bEntrar.setText("Entrar");
bEntrar.setEnabled(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(184, 184, 184)
.addComponent(jLabel4)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 91, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(7, 7, 7)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(tSenha, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bEntrar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
.addComponent(tUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(55, 55, 55))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(50, 50, 50)
.addComponent(jLabel4)
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(tUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(3, 3, 3)
.addComponent(jLabel1)
.addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(tSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
.addComponent(bEntrar)
.addGap(41, 41, 41))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Inicio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Inicio().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bEntrar;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPasswordField tSenha;
private javax.swing.JFormattedTextField tUsuario;
// End of variables declaration
}
我使用Swing中的Project视图编辑了按钮属性。那么,我该怎么做才能解决此问题?是Netbeans错误吗?顺便说一下,我正在使用Netbeans IDE 8.2。
答案 0 :(得分:0)
如果您要基于文本字段上发生的更改来影响按钮的状态,则这些动作需要在动作处理程序本身(即$(".go_btn").click(function(){
const number = $(".go_input").val();
if(number == 1){
$('html, body').animate({
'scrollTop' : $("#index_first").offset().bottom
});
}
$('html, body').animate({
'scrollTop' : $("#index_"+number).offset().top
});
})
s)中进行
以下是一个非常基本的示例,它将禁用“操作”按钮,直到用户名和密码字段都不再为空为止
DocumentListener
为什么初始状态未更新?
您将import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField userNameField;
private JPasswordField passwordField;
private JButton actionButton;
public TestPane() {
userNameField = new JTextField(15);
passwordField = new JPasswordField(15);
actionButton = new JButton("Do stuff");
actionButton.setEnabled(false);
DocumentListener documentListener = new DocumentListener() {
protected void stateDidChange() {
actionButton.setEnabled(userNameField.getText().length() > 0 && passwordField.getPassword().length > 0);
}
@Override
public void insertUpdate(DocumentEvent e) {
stateDidChange();
}
@Override
public void removeUpdate(DocumentEvent e) {
stateDidChange();
}
@Override
public void changedUpdate(DocumentEvent e) {
stateDidChange();
}
};
userNameField.getDocument().addDocumentListener(documentListener);
passwordField.getDocument().addDocumentListener(documentListener);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = gbc.REMAINDER;
add(userNameField, gbc);
add(passwordField, gbc);
add(actionButton, gbc);
}
}
}
定义为...。
int a;
为其赋予初始值(或默认值)a
。
然后您使用...
if(a == 2){ 如果(bEntrar.isEnabled()==否){ bEntrar.setEnabled(true); }
0
更改初始状态。由于}else{
if (bEntrar.isEnabled() == true) {
bEntrar.setEnabled(false);
}
}
为a
,因此执行了0
块,因此将按钮启用状态更改为else
的唯一条件是当前启用状态为{{1 }}
但是当我使用Shift + F6键运行代码时,它不起作用
执行“清理并生成”并运行项目,而不是文件。在测试您的代码时,显示的用户界面带有已禁用的按钮