繁体   English   中英

如果语句不适用于JTexfield和字符串比较

[英]If Statements are not working for JTexfield & String Comparison

这是我的代码

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);
 }

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

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

关于你的陈述:

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

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


编辑:你说

这是来自main_panel.java的片段...在登录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);
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM