"get" 方法在该字符串具有值时返回空字符串
"get" method is returning an empty string when that string has a value
我有一个方法,其中之一是从 JTextField 获取输入。我需要在另一个 class 中使用该输入,所以我创建了一个 getOutput() 方法,其中 returns 输入字符串。唯一的问题是它 returns 是一个空字符串 ""
.
提示 class:此 class 创建一个 window,其中包含 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;
}
}
下面是菜单 class,我在其中尝试使用上面 class 中的 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;
}
}
可能值得注意的是,这些 class 都没有完成,因此一些注释和空构造函数不会保持这种状态。
您正在为控制台(或程序)编写代码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
不会 "block" 当前执行的代码相反,如果 returns(几乎)立即可见,这意味着 createPrompt
方法将 return 并且您在用户甚至有机会做出之前评估 getOutput
方法一个选择。
相反,您应该使用模态对话框。这将在对话框可见时阻止代码的执行,并将继续阻止它直到对话框关闭(这一切都是以一种实际上不会阻止事件调度线程的方式完成的,所以 UI不会出现"dead")
有关详细信息,请参阅 How to Make Dialogs
您可以使用多种方法来执行此操作,但我喜欢使用一种方法来显示提示和 return 结果,例如这样...
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;
}
我有一个方法,其中之一是从 JTextField 获取输入。我需要在另一个 class 中使用该输入,所以我创建了一个 getOutput() 方法,其中 returns 输入字符串。唯一的问题是它 returns 是一个空字符串 ""
.
提示 class:此 class 创建一个 window,其中包含 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;
}
}
下面是菜单 class,我在其中尝试使用上面 class 中的 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;
}
}
可能值得注意的是,这些 class 都没有完成,因此一些注释和空构造函数不会保持这种状态。
您正在为控制台(或程序)编写代码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
不会 "block" 当前执行的代码相反,如果 returns(几乎)立即可见,这意味着 createPrompt
方法将 return 并且您在用户甚至有机会做出之前评估 getOutput
方法一个选择。
相反,您应该使用模态对话框。这将在对话框可见时阻止代码的执行,并将继续阻止它直到对话框关闭(这一切都是以一种实际上不会阻止事件调度线程的方式完成的,所以 UI不会出现"dead")
有关详细信息,请参阅 How to Make Dialogs
您可以使用多种方法来执行此操作,但我喜欢使用一种方法来显示提示和 return 结果,例如这样...
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;
}