我有一个方法,在很多方面,从JTextField获取输入。我需要在另一个类中使用该输入,因此我创建了一个返回输入字符串的getOutput()方法。唯一的问题是它返回一个空字符串""
。
提示类:此类创建一个包含JTextField的窗口。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Prompt {
private final String title;
private final String promptstr;
private String myInput;
public Prompt() {
title = "";
promptstr = "";
myInput = "";
}
public Prompt(String title, String prompt) {
this.title = title;
promptstr = prompt;
myInput = "";
}
public void createPrompt() {
JFrame pwindow = new JFrame(title);
pwindow.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
JPanel main = new JPanel();
main.setLayout(new GridLayout(2,1));
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.CENTER));
JPanel input = new JPanel();
input.setLayout(new FlowLayout(FlowLayout.CENTER));
input.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
JLabel prompt = new JLabel(promptstr);
input.add(prompt);
JTextField in = new JTextField(25);
input.add(in);
JButton ok = new JButton("OK");
buttons.add(ok);
ok.addActionListener((ActionEvent e) -> {
myInput = in.getText();
try (FileWriter output = new FileWriter(new File("C:\\Users\\Mike\\Desktop\\output.txt"))) {
// function here
} catch (IOException ex) {}
pwindow.dispose();
});
JButton cancel = new JButton("Cancel");
buttons.add(cancel);
cancel.addActionListener((ActionEvent e) -> {
try (FileWriter output = new FileWriter(new File("C:\\Users\\Mike\\Desktop\\output.txt"))) {
// function here
} catch (IOException ex) {}
pwindow.dispose();
});
main.add(input);
main.add(buttons);
pwindow.getContentPane();
pwindow.add(main);
pwindow.pack();
pwindow.setVisible(true);
}
public String getOutput() {
return myInput;
}
}
下面是Menu类,我尝试使用上面的类中的getOutput()方法打印JTextField输入。
package japp1;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Menu {
public Menu() {
//do something
}
public JMenuBar createCalcMenu() {
JMenuBar calcmenu = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
calcmenu.add(fileMenu);
calcmenu.add(editMenu);
JMenuItem newAction = new JMenuItem("New");
JMenuItem openAction = new JMenuItem("Open");
JMenuItem exitAction = new JMenuItem("Exit");
JMenuItem cutAction = new JMenuItem("Cut");
JMenuItem copyAction = new JMenuItem("Copy");
JMenuItem pasteAction = new JMenuItem("Paste");
JCheckBoxMenuItem checkAction = new JCheckBoxMenuItem("Check Action");
JRadioButtonMenuItem radioAction1 = new JRadioButtonMenuItem("Radio Button 1");
JRadioButtonMenuItem radioAction2 = new JRadioButtonMenuItem("Radio Button 2");
ButtonGroup bg = new ButtonGroup();
bg.add(radioAction1);
bg.add(radioAction2);
fileMenu.add(newAction);
fileMenu.add(openAction);
fileMenu.add(checkAction);
fileMenu.addSeparator();
fileMenu.add(exitAction);
editMenu.add(cutAction);
editMenu.add(copyAction);
editMenu.add(pasteAction);
editMenu.addSeparator();
editMenu.add(radioAction1);
editMenu.add(radioAction2);
//actionevents
exitAction.addActionListener((ActionEvent e) -> {
System.exit(0);
});
return calcmenu;
}
public JMenuBar createPlotMenu() {
JMenuBar plotmenu = new JMenuBar();
JMenu file = new JMenu("File");
JMenu plot = new JMenu("Plot");
plotmenu.add(file);
plotmenu.add(plot);
//file items
JMenuItem newAction = new JMenuItem("New Function");
JMenu clearAction = new JMenu("Clear");
JMenuItem exitAction = new JMenu("Exit");
ArrayList<JMenuItem> funcs = new ArrayList<>();
//plot items
file.add(newAction);
file.add(clearAction);
file.addSeparator();
file.add(exitAction);
//actionevents
newAction.addActionListener((ActionEvent e) -> {
Prompt nf = new Prompt("Add a new function","f(x)=");
nf.createPrompt();
System.out.println(nf.getOutput());
});
exitAction.addActionListener((ActionEvent e) -> {
System.exit(0);
});
return plotmenu;
}
}
值得注意的是,这些类都没有完成,因此一些注释和空构造函数不会保持这种状态。
答案 0 :(得分:2)
您正在为控制台(或程序)API编写代码,但GUI不能以这种方式工作。
让我们从您的切入点开始......
newAction.addActionListener((ActionEvent e) -> {
Prompt nf = new Prompt("Add a new function","f(x)=");
nf.createPrompt();
System.out.println(nf.getOutput());
});
这没有什么特别的错误,但如果我们看一下createPrompt
......
public void createPrompt() {
JFrame pwindow = new JFrame(title);
你要做的第一件事是创建一个JFrame
,这将是一个问题,因为JFrame
不会“阻止”当代码可见时的当前执行,相反, if(几乎)立即返回,这意味着createPrompt
方法将返回,并且在用户甚至有机会进行选择之前评估getOutput
方法。
相反,您应该使用模态对话框。这将阻止代码在对话框可见的位置执行,并将继续阻止它直到对话框关闭(这一切都以不会实际阻止事件调度线程的方式完成,因此UI赢了' t似乎“死了”)
有关详细信息,请参阅How to Make Dialogs
您可以通过多种方式执行此操作,但我喜欢使用单个方法来显示提示并返回结果,就像这样的示例......
public String showPrompt(JComponent owner) {
JDialog pwindow = new JDialog(SwingUtilities.windowForComponent(owner), title, Dialog.ModalityType.APPLICATION_MODAL);
JPanel main = new JPanel();
main.setLayout(new GridLayout(2, 1));
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.CENTER));
JPanel input = new JPanel();
input.setLayout(new FlowLayout(FlowLayout.CENTER));
input.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel prompt = new JLabel(promptstr);
input.add(prompt);
JTextField in = new JTextField(25);
input.add(in);
JButton ok = new JButton("OK");
buttons.add(ok);
ok.addActionListener((ActionEvent e) -> {
myInput = in.getText();
try (FileWriter output = new FileWriter(new File("C:\\Users\\Mike\\Desktop\\output.txt"))) {
//function here
} catch (IOException ex) {
}
pwindow.dispose();
});
JButton cancel = new JButton("Cancel");
buttons.add(cancel);
cancel.addActionListener((ActionEvent e) -> {
try (FileWriter output = new FileWriter(new File("C:\\Users\\Mike\\Desktop\\output.txt"))) {
//function here
} catch (IOException ex) {
}
pwindow.dispose();
});
main.add(input);
main.add(buttons);
pwindow.getContentPane();
pwindow.add(main);
pwindow.pack();
pwindow.setVisible(true);
return myInput;
}