【问题标题】:If Statements are not working for JTexfield & String Comparison如果语句不适用于 JTexfield 和字符串比较
【发布时间】:2013-05-12 02:32:56
【问题描述】:

这是我的代码

public Main_panel() {
 initComponents();
 setLocationRelativeTo(null);
 tf_type.setVisible(false);
 String normal = tf_type.getText();
 String ntext = "normal";
 if(normal.equals(ntext)) {
      cmb_report.setVisible(false);
      cmb_cu.setVisible(false);
 }

对于其他信息,通过 netbeans 中的自定义代码将 tf_type 设置为 public static。但是 cmb_reports 和 cmb_cu 不是不可见的,即不执行 if 语句。这是为什么呢?

【问题讨论】:

    标签: java string swing if-statement comparison


    【解决方案1】:

    在用户有时间将数据输入任何 JTextField 之前,您正在调用程序构造函数中的 if 块。如果您希望在程序运行期间发生此更改,则需要使用某种类型的侦听器,例如将 ActionListener 添加到 JTextField。

    关于你的这份声明:

    通过 netbeans 中的自定义代码将 tf_type 设置为 public static

    你不想这样做。不要让你的字段静态,这样你就可以在没有实例的情况下在 main 中访问它们。这将破坏所有 OOP 原则,并使您的代码很难维护或更新。更好的方法是通过非静态公共方法更改实例的状态。


    编辑:你说

    这是来自 main_panel.java 的 sn-p...在登录 jframe 中,此代码通过 Main_panel.tf_type.setText(txt_type.getText()) 设置 tf_type 的值; fyi....登录后出现主面板...

    我会使用模态 JDialog 而不是 JFrame 来登录,因为模态 JDialog 会很容易地让您知道它何时已被完全处理,并且我会通过调用来获取登录对话框的字段状态公共方法,而不是使用静态字段。


    编辑2:例如,

    import java.awt.Dialog.ModalityType;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    
    import javax.swing.*;
    
    public class LogInDialogTest {
    
       private static void createAndShowGui() {
          JTextField textField1 = new JTextField(10);
          textField1.setEditable(false);
          textField1.setFocusable(false);
    
          JPanel mainPanel = new JPanel();
          mainPanel.add(textField1);
          mainPanel.add(new JButton(new AbstractAction("Exit") {
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                JButton thisBtn = (JButton) evt.getSource();
                Window win = SwingUtilities.getWindowAncestor(thisBtn);
                win.dispose();
             }
          }));
    
          JFrame frame = new JFrame("Frame");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          // frame.setVisible(true);
    
          JTextField textField2 = new JTextField(10);
          JPanel mainPanel2 = new JPanel();
          mainPanel2.add(textField2);
          mainPanel2.add(new JButton(new AbstractAction("Submit") {
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                JButton thisBtn = (JButton) evt.getSource();
                Window win = SwingUtilities.getWindowAncestor(thisBtn);
                win.dispose();
             }
          }));
          JDialog dialog = new JDialog(frame, "Dialog", ModalityType.APPLICATION_MODAL);
          dialog.getContentPane().add(mainPanel2);
          dialog.pack();
          dialog.setLocationRelativeTo(frame);
          dialog.setVisible(true);
    
          textField1.setText(textField2.getText());
          frame.setVisible(true);
    
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    编辑 3: 一个更好的例子,

    import java.awt.Dialog.ModalityType;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    
    import javax.swing.*;
    
    public class LogInDialogTest {
    
       private static void createAndShowGui() {
          final MainJPanel mainPanel = new MainJPanel();
    
          JFrame frame = new JFrame("Frame");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          // frame.setVisible(true);
    
          LoginJPanel loginPanel = new LoginJPanel();
          JDialog dialog = new JDialog(frame, "Dialog",
                ModalityType.APPLICATION_MODAL);
          dialog.getContentPane().add(loginPanel);
          dialog.pack();
          dialog.setLocationRelativeTo(frame);
          dialog.setVisible(true);
    
          mainPanel.textFieldSetText(loginPanel.textFieldGetText());
          frame.setVisible(true);
    
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class LoginJPanel extends JPanel {
       private JTextField textField = new JTextField(10);
    
       public LoginJPanel() {
          add(textField);
          add(new JButton(new AbstractAction("Submit") {
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                JButton thisBtn = (JButton) evt.getSource();
                Window win = SwingUtilities.getWindowAncestor(thisBtn);
                win.dispose();
             }
          }));
       }
    
       public String textFieldGetText() {
          return textField.getText();
       }
    }
    
    class MainJPanel extends JPanel {
       private JTextField textField = new JTextField(10);
    
       public MainJPanel() {
          textField.setEditable(false);
          textField.setFocusable(false);
          add(textField);
          add(new JButton(new AbstractAction("Exit") {
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                JButton thisBtn = (JButton) evt.getSource();
                Window win = SwingUtilities.getWindowAncestor(thisBtn);
                win.dispose();
             }
          }));
       }
    
       public void textFieldSetText(String text) {
          textField.setText(text);
       }
    }
    

    【讨论】:

    • @MohammedZameer:在没有更多上下文的情况下,"i took the value for jtextfield from previous jframe...." 的声明对我们没有多大意义,除了您可能使用多个 JFrame,这通常也不是一件好事.
    • 这是来自 main_panel.java 的 sn-p...在登录 jframe 中,此代码通过 Main_panel.tf_type.setText(txt_type.getText()) 设置 tf_type 的值; fyi....登录后,出现主面板...
    • 谢谢你的朋友,我会试着去 jDialog (在 youtube 上搜索教程)。非常感谢,帮助很大...
    • @MohammedZameer:已经发布了一个更好的示例,该示例演示了使用公共方法来评估状态和更改状态。
    • 先生,您能解释一下如何将此值带到下一个 jframe 并使用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多