1

これは私のコードです

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 は非表示になりません。つまり、ステートメントが実行されない場合です。何故ですか?

4

1 に答える 1

3

ユーザーが JTextField にデータを入力する前に、プログラムのコンストラクターで if ブロックを呼び出しています。プログラムの実行中にこの変更を行う場合は、JTextField に ActionListener を追加するなど、何らかのリスナーを使用する必要があります。

あなたのこの声明について:

tf_type は、netbeans のカスタマイズ コードを介して public static に設定されます

あなたはこれをしたくありません。インスタンスなしでメインでアクセスできるようにするためだけに、フィールドを静的にしないでください。これにより、すべての OOP 原則が破られ、コードの保守や更新が非常に困難になります。より良い方法は、非静的パブリック メソッドを介してインスタンスの状態を変更することです。


編集: あなたは述べます

これは main_panel.java のスニペットです...ログイン jframe では、このコードは Main_panel.tf_type.setText(txt_type.getText()); によって tf_type の値を設定します。参考までに....ログインすると、メインパネルが表示されます...

ログインには JFrame ではなくモーダル JDialog を使用します。これは、モーダル 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);
   }
}
于 2013-05-12T02:36:47.657 に答える