-1

我有一个问题,我一直在制作一个 Swing 应用程序。

我的问题是关于如何处理Jbutton像 a JOptionPane,如果可能的话?

我想像处理按钮一样处理所有JOptionpane按钮,但是我们在 main 函数中编写的消息System.out.println("this line executes...how to prevent.."); 这个函数是显示消息,直到Jframe可见。

谁能让我知道如何预防和处理按钮功能?尤其是当我单击按钮时它会进一步执行。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public class InputVerifierExample extends JPanel {
   public static final Color WARNING_COLOR = Color.red;
   private JTextField firstNameField = new JTextField(10);
   private JTextField middleNameField = new JTextField(10);
   private JTextField lastNameField = new JTextField(10);
   JLabel name=new JLabel("Name:");
   private JTextField[] nameFields = { 
         firstNameField, 
         middleNameField,
         lastNameField };
   private JLabel warningLabel = new JLabel("  ");

   public InputVerifierExample() {
      warningLabel.setOpaque(false);

      JPanel namePanel = new JPanel();
      namePanel.add(name);
      MyInputVerifier verifier = new MyInputVerifier();
      for (JTextField field : nameFields) {
         field.setInputVerifier(verifier);
         namePanel.add(field);
      }
      namePanel.add(new JButton(new SubmitBtnAction()));

      setLayout(new BorderLayout());
      add(namePanel, BorderLayout.CENTER);
      warningLabel.setForeground(Color.red);
      add(warningLabel, BorderLayout.NORTH);
   }

   private class SubmitBtnAction extends AbstractAction {
      public SubmitBtnAction() {
         super("Submit");
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         // first check all fields aren't empty
         for (JTextField field : nameFields) {
            if (field.getText().trim().isEmpty()) {
               return ;  // return if empty
            }
         }
         String name = "";
         for (JTextField field : nameFields) {
            name += field.getText() + " ";
            field.setText("");
         }
         name = name.trim();
         JOptionPane.showMessageDialog(InputVerifierExample.this, name, "Name Entered",
               JOptionPane.INFORMATION_MESSAGE);
      }
   }

   private class MyInputVerifier extends InputVerifier {

      @Override
      public boolean verify(JComponent input) {
         JTextField field = (JTextField) input;
         if (field.getText().trim().isEmpty()) {
            warningLabel.setText("Please do not leave this field empty :"+name.getText());
            warningLabel.setBackground(WARNING_COLOR);
            //firstNameField.setText("sorry");
            return false;
         }
         warningLabel.setText("");
         warningLabel.setBackground(null);
         return true;
      }

   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("InputVerifier Example");
      frame.setSize(200, 500);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new InputVerifierExample());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
            createAndShowGui();
      System.out.println("this line executes...how to prevent..");
   }

}
4

2 回答 2

1

基本上,你有这样的东西:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestButton {

    protected void createAndShowGUI() {
        JFrame frame = new JFrame("Test button");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("Click me");
        frame.add(button);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestButton().createAndShowGUI();
            }
        });
        System.err.println("Executed once the button has been clicked");
    }

}

并且您希望在System.err.println("Executed once the button has been clicked");按下按钮时执行该行(上面不是这种情况)。

解决方案实际上非常简单:在另一个方法中单击按钮后移动要执行的代码(参见下面的 proceed()方法),然后从 调用该行ActionListener

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestButton {

    protected void createAndShowGUI() {
        JFrame frame = new JFrame("Test button");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("Click me");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                proceed();
            }
        });
        frame.add(button);
        frame.setSize(200, 200);
        frame.setVisible(true);
    }

    protected void proceed() {
        System.err.println("Executed once the button has been clicked");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestButton().createAndShowGUI();
            }
        });

    }

}
于 2013-01-14T13:39:57.947 回答
0

好吧,这个问题不是很清楚,但从你的评论来看,你不想做任何事情,直到 aJButton被点击?或者您想在单击按钮后执行任务?

如果是这样,不要把你的进一步代码放在你的主块中,调用一个functionfrom块。actionPerformed像这样的东西:

 public void actionPerformed(ActionEvent e) {
     // first check all fields aren't empty
     for (JTextField field : nameFields) {
        if (field.getText().trim().isEmpty()) {
           return ;  // return if empty
        }
     }
     String name = "";
     for (JTextField field : nameFields) {
        name += field.getText() + " ";
        field.setText("");
     }
     name = name.trim();
     JOptionPane.showMessageDialog(InputVerifierExample.this, name, "Name Entered",
           JOptionPane.INFORMATION_MESSAGE);
     display();///////////this is the function containing further code
     }
    }
    //this is display
    public void display()
      {
      System.out.println("this line executes...how to prevent..");

      }
于 2013-01-14T12:43:13.447 回答