我正在尝试为正在进行的工作项目创建另一个GUI屏幕。如果触发此GUI屏幕将打开一个JOptionsPane,其中包含三个JTextFields和一个确认按钮(它是实际JOptionsPane的一部分)。
我无法在JTextField上获取动作侦听器。我希望textfield在用户点击确认时获取输入的任何值。然后通过各种条件,程序将继续。我已经创建了一个测试类来计划和创建与我的程序的其余部分隔离的GUI,以便我可以在下面分享它。我还要补充一点,我为我创建这个GUI的方式道歉。我不是很擅长GUI的视觉方面,所以我使用了对我来说最容易丢失的方法和许多带边框布局的JPanel。可能不是最好但它有效:)
这是我的代码:
package guitesting;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GUITesting
{
public static int tester = 0;
public static String resultString = null;
static JTextField tRDFI = new JTextField("RDFI Number Here",30);
public static void NOCGUI(){
String text = "<html>"
+ "This is where the entry detail will go"+
"<br><br>"
+"</html>";
JLabel RDFI = new JLabel("RDFI Number:");
JLabel DDA = new JLabel("DDA:");
JLabel TCode = new JLabel("Transaction Code: ");
tRDFI.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
resultString = tRDFI.getText();
}
});
JTextField tDDA = new JTextField("DDA Number Here",30);
JTextField tTCode = new JTextField("TCode Here",30);
JLabel label = new JLabel(text);
JPanel mainPanel = new JPanel();
JPanel fieldPanel = new JPanel();
JPanel textPanel = new JPanel();
JPanel northFieldPanel = new JPanel();
JPanel southFieldPanel = new JPanel();
JPanel tCodePanel = new JPanel();
JPanel RDFIPanel = new JPanel();
JPanel DDAPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
fieldPanel.setLayout(new BorderLayout());
textPanel.setLayout(new BorderLayout());
northFieldPanel.setLayout(new BorderLayout());
southFieldPanel.setLayout(new BorderLayout());
tCodePanel.setLayout(new BorderLayout());
RDFIPanel.setLayout(new BorderLayout());
DDAPanel.setLayout(new BorderLayout());
textPanel.add(label,BorderLayout.CENTER);
RDFIPanel.add(RDFI,BorderLayout.WEST);
RDFIPanel.add(tRDFI,BorderLayout.EAST);
tCodePanel.add(TCode,BorderLayout.WEST);
tCodePanel.add(tTCode,BorderLayout.EAST);
DDAPanel.add(DDA,BorderLayout.WEST);
DDAPanel.add(tDDA,BorderLayout.EAST);
northFieldPanel.add(RDFIPanel,BorderLayout.NORTH);
northFieldPanel.add(DDAPanel,BorderLayout.SOUTH);
southFieldPanel.add(tCodePanel,BorderLayout.NORTH);
fieldPanel.add(northFieldPanel,BorderLayout.NORTH);
fieldPanel.add(southFieldPanel,BorderLayout.SOUTH);
mainPanel.add(textPanel,BorderLayout.NORTH);
mainPanel.add(fieldPanel,BorderLayout.SOUTH);
String options[] = {"Confirm"};
int result = JOptionPane.showOptionDialog(null, mainPanel, "NOC Builder", JOptionPane.YES_OPTION,
JOptionPane.PLAIN_MESSAGE,null, options, options[0]);
if(result==0 && resultString.equals("A")){
System.out.println("pass");
}
}
public static void main(String args[]){
GUITesting.NOCGUI();
}
}
答案 0 :(得分:3)
我认为ActionListener
不是你想要的。只有当文本字段具有焦点时用户按下Return键才会调用它。
如果您想收听文本字段中的所有更改,请查看DocumentListener
。
textField.getDocument().addDocumentListener(documentListener);
但是为什么不在对话框返回时引用实际的文本字段?这可能是最简单的解决方案。试试这个:
if (result == 0) {
System.out.println("tRDFI: " + tRDFI.getText());
System.out.println("tDDA: " + tDDA.getText());
System.out.println("tTCode: " + tTCode.getText());
}