Java 无法在弹出JFrame中编辑JTextField
好的,我在弹出JFrame中的文本字段有问题。。。 我实际上有两个独立的程序,一个是游戏,一个是地图编辑器。我决定让游戏有一种内置的地图编辑器,所以我将地图编辑器中的类添加到游戏项目中的一个新包中,做了一些小的调整(比如从地图编辑器中删除main()方法),然后让事情开始工作。地图编辑器会弹出一个新的JFrame,当我单击“new”按钮时,它会打开一个新的JFrame,其中有两个文本字段和一个按钮,请求新地图的宽度和高度。我无法编辑文本字段中的值。。。我不知道为什么。。。 弹出代码:Java 无法在弹出JFrame中编辑JTextField,java,swing,popup,jframe,jtextfield,Java,Swing,Popup,Jframe,Jtextfield,好的,我在弹出JFrame中的文本字段有问题。。。 我实际上有两个独立的程序,一个是游戏,一个是地图编辑器。我决定让游戏有一种内置的地图编辑器,所以我将地图编辑器中的类添加到游戏项目中的一个新包中,做了一些小的调整(比如从地图编辑器中删除main()方法),然后让事情开始工作。地图编辑器会弹出一个新的JFrame,当我单击“new”按钮时,它会打开一个新的JFrame,其中有两个文本字段和一个按钮,请求新地图的宽度和高度。我无法编辑文本字段中的值。。。我不知道为什么。。。 弹出代码: priva
private class newMap extends JFrame implements ActionListener{
JLabel wlbl = new JLabel("Map width: ");
JTextField w = new JTextField("12");
JLabel hlbl = new JLabel("Map height: ");
JTextField h = new JTextField("8");
JButton create = new JButton("Create map");
public newMap(Component p){
super("New Map");
setSize(100,75);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setLayout(null);
setVisible(true);
int bw = 96, bh = 24, s = 4, x = s;
wlbl.setBounds(x,s,bw,bh);
x+=s+bw;
add(wlbl);
w.setBounds(x,s,bw,bh);
x+=s+bw;
add(w);
hlbl.setBounds(x,s,bw,bh);
x+=s+bw;
add(hlbl);
h.setBounds(x,s,bw,bh);
x+=s+bw;
add(h);
create.setBounds(x,s,bw,bh);
x+=s+bw;
create.addActionListener(this);
add(create);
setSize(getWidth()-this.getContentPane().getWidth()+x,
getHeight()-this.getContentPane().getHeight()+s+s+bh);
setLocationRelativeTo(p);
}
public void actionPerformed(ActionEvent e){
try{
mapBox.create(Integer.parseInt(w.getText()),Integer.parseInt(h.getText()));
}catch(NumberFormatException ex){
return;
}
dispose();
}
}
显然,我已经尝试过w.requestFocusInWidnow()和w.requestFocus()等方法,对于框架和我在网上找到的其他一些解决方案也是如此,但没有一个对我有效
问题已解决:
在解释了我的问题之后,我意识到问题必须出现在游戏的代码中的某个地方,可能是在JFrame的类中,我注意到我做了一些奇怪的事情,而不是使用KeyListener实现来处理输入。(我以前尝试将游戏创建为JApplet,但是在JApplet上的KeyListener遇到了问题)。我将我的主类设置为KeyEventDispatcher,或者类似的东西。感谢大家的帮助,这是一个愚蠢的问题(我只是在错误的地方寻找解决方案)
因此KeyEventDispatcher没有正确地将关键操作“分派”到其他组件。
我只是回到了传统的KeyListener,因为它在JFrame中工作得很好。我怀疑问题在于您如何调用这个新窗口,可能您有一个无限循环或其他构造冻结了Swing事件调度线程或EDT,使您的GUI没有响应。如果您能够创建并发布,我们将非常清楚这是一个供我们编译、测试、运行以及可能的修改和更正的工具 我还认为,您的代码还有其他与主要问题无关的重要问题:
- 您不应该在应该使用JOptionPanes或JDialogs等对话框的地方使用JFrames
- 应避免使用绝对定位,而应使用布局管理器
- 您应该努力创建符合Java命名标准的代码,包括提供以大写字母开头的类名称。当要求他人阅读和理解您的代码并帮助您时,这一点尤为重要
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class TestNewMap extends JPanel {
private MapBox mapBox = new MapBox();
private NewMap newMap = new NewMap(this);
private HoverNewMap hoverNewMap = new HoverNewMap();
public TestNewMap() {
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
btnPanel.add(new JButton(new AbstractAction("Show Your NewMap") {
@Override
public void actionPerformed(ActionEvent evt) {
newMap.setVisible(true);
}
}));
btnPanel.add(new JButton(new AbstractAction("Show Hover's NewMap") {
@Override
public void actionPerformed(ActionEvent evt) {
hoverNewMap.setMapWidth(mapBox.getWidthValue());
hoverNewMap.setMapHeight(mapBox.getHeightValue());
Object[] options = { "Create Map", "Cancel" };
Object initialValue = options[0];
int result = JOptionPane.showOptionDialog(TestNewMap.this,
hoverNewMap, "Get Map Dimensions",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, initialValue);
if (result == 0) {
try {
int w = hoverNewMap.getMapWidth();
int h = hoverNewMap.getMapHeight();
mapBox.create(w, h);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(TestNewMap.this,
"Please only enter int values", "Non-int Input Error",
JOptionPane.ERROR_MESSAGE);
hoverNewMap.setMapHeight(0);
hoverNewMap.setMapWidth(0);
}
}
}
}));
int borderGap = 5;
setBorder(BorderFactory.createEmptyBorder(borderGap, borderGap,
borderGap, borderGap));
setLayout(new BorderLayout(borderGap, borderGap));
add(btnPanel, BorderLayout.NORTH);
add(mapBox, BorderLayout.CENTER);
}
private static void createAndShowGui() {
TestNewMap mainPanel = new TestNewMap();
JFrame frame = new JFrame("TestNewMap");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
private class MapBox extends JPanel {
private static final int INIT_WIDTH = 12;
private static final int INIT_HEIGHT = 8;
private JTextField widthField = new JTextField(
String.valueOf(INIT_WIDTH), 5);
private JTextField heightField = new JTextField(
String.valueOf(INIT_HEIGHT), 5);
public MapBox() {
add(new JLabel("Width:"));
add(widthField);
add(Box.createHorizontalStrut(10));
add(new JLabel("Height:"));
add(heightField);
}
public void create(int width, int height) {
widthField.setText(String.valueOf(width));
heightField.setText(String.valueOf(height));
}
public int getWidthValue() {
return Integer.parseInt(widthField.getText());
}
public int getHeightValue() {
return Integer.parseInt(heightField.getText());
}
}
private class HoverNewMap extends JPanel {
private JTextField mapWidth = new JTextField(5);
private JTextField mapHeight = new JTextField(5);
public HoverNewMap() {
add(new JLabel("Width:"));
add(mapWidth);
add(Box.createHorizontalStrut(15));
add(new JLabel("Height:"));
add(mapHeight);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
}
public void setMapWidth(int w) {
mapWidth.setText(String.valueOf(w));
}
public void setMapHeight(int h) {
mapHeight.setText(String.valueOf(h));
}
public int getMapWidth() throws NumberFormatException {
int w = Integer.parseInt(mapWidth.getText());
return w;
}
public int getMapHeight() throws NumberFormatException {
int h = Integer.parseInt(mapHeight.getText());
return h;
}
}
private class NewMap extends JFrame implements ActionListener {
JLabel wlbl = new JLabel("Map width: ");
JTextField w = new JTextField("12");
JLabel hlbl = new JLabel("Map height: ");
JTextField h = new JTextField("8");
JButton create = new JButton("Create map");
public NewMap(Component p) {
super("New Map");
setSize(100, 75);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setLayout(null);
// setVisible(true);
int bw = 96, bh = 24, s = 4, x = s;
wlbl.setBounds(x, s, bw, bh);
x += s + bw;
add(wlbl);
w.setBounds(x, s, bw, bh);
x += s + bw;
add(w);
hlbl.setBounds(x, s, bw, bh);
x += s + bw;
add(hlbl);
h.setBounds(x, s, bw, bh);
x += s + bw;
add(h);
create.setBounds(x, s, bw, bh);
x += s + bw;
create.addActionListener(this);
add(create);
setSize(getWidth() - this.getContentPane().getWidth() + x, getHeight()
- this.getContentPane().getHeight() + s + s + bh);
setLocationRelativeTo(p);
}
public void actionPerformed(ActionEvent e) {
try {
mapBox.create(Integer.parseInt(w.getText()),
Integer.parseInt(h.getText()));
} catch (NumberFormatException ex) {
return;
}
dispose();
}
}
}
我怀疑问题在于您如何调用这个新窗口,可能您有一个无限循环或其他构造冻结了Swing事件调度线程或EDT,使您的GUI没有响应。我们最好知道您是否可以创建并发布一个供我们编译、测试、运行并可能修改和更正的窗口 我还认为,您的代码还有其他与主要问题无关的重要问题:
- 您不应该在应该使用JOptionPanes或JDialogs等对话框的地方使用JFrames
- 应避免使用绝对定位,而应使用布局管理器
- 您应该努力创建符合Java命名标准的代码,包括提供以大写字母开头的类名称。当要求他人阅读和理解您的代码并帮助您时,这一点尤为重要
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class TestNewMap extends JPanel {
private MapBox mapBox = new MapBox();
private NewMap newMap = new NewMap(this);
private HoverNewMap hoverNewMap = new HoverNewMap();
public TestNewMap() {
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
btnPanel.add(new JButton(new AbstractAction("Show Your NewMap") {
@Override
public void actionPerformed(ActionEvent evt) {
newMap.setVisible(true);
}
}));
btnPanel.add(new JButton(new AbstractAction("Show Hover's NewMap") {
@Override
public void actionPerformed(ActionEvent evt) {
hoverNewMap.setMapWidth(mapBox.getWidthValue());
hoverNewMap.setMapHeight(mapBox.getHeightValue());
Object[] options = { "Create Map", "Cancel" };
Object initialValue = options[0];
int result = JOptionPane.showOptionDialog(TestNewMap.this,
hoverNewMap, "Get Map Dimensions",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
options, initialValue);
if (result == 0) {
try {
int w = hoverNewMap.getMapWidth();
int h = hoverNewMap.getMapHeight();
mapBox.create(w, h);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(TestNewMap.this,
"Please only enter int values", "Non-int Input Error",
JOptionPane.ERROR_MESSAGE);
hoverNewMap.setMapHeight(0);
hoverNewMap.setMapWidth(0);
}
}
}
}));
int borderGap = 5;
setBorder(BorderFactory.createEmptyBorder(borderGap, borderGap,
borderGap, borderGap));
setLayout(new BorderLayout(borderGap, borderGap));
add(btnPanel, BorderLayout.NORTH);
add(mapBox, BorderLayout.CENTER);
}
private static void createAndShowGui() {
TestNewMap mainPanel = new TestNewMap();
JFrame frame = new JFrame("TestNewMap");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
private class MapBox extends JPanel {
private static final int INIT_WIDTH = 12;
private static final int INIT_HEIGHT = 8;
private JTextField widthField = new JTextField(
String.valueOf(INIT_WIDTH), 5);
private JTextField heightField = new JTextField(
String.valueOf(INIT_HEIGHT), 5);
public MapBox() {
add(new JLabel("Width:"));
add(widthField);
add(Box.createHorizontalStrut(10));
add(new JLabel("Height:"));
add(heightField);
}
public void create(int width, int height) {
widthField.setText(String.valueOf(width));
heightField.setText(String.valueOf(height));
}
public int getWidthValue() {
return Integer.parseInt(widthField.getText());
}
public int getHeightValue() {
return Integer.parseInt(heightField.getText());
}
}
private class HoverNewMap extends JPanel {
private JTextField mapWidth = new JTextField(5);
private JTextField mapHeight = new JTextField(5);
public HoverNewMap() {
add(new JLabel("Width:"));
add(mapWidth);
add(Box.createHorizontalStrut(15));
add(new JLabel("Height:"));
add(mapHeight);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
}
public void setMapWidth(int w) {
mapWidth.setText(String.valueOf(w));
}
public void setMapHeight(int h) {
mapHeight.setText(String.valueOf(h));
}
public int getMapWidth() throws NumberFormatException {
int w = Integer.parseInt(mapWidth.getText());
return w;
}
public int getMapHeight() throws NumberFormatException {
int h = Integer.parseInt(mapHeight.getText());
return h;
}
}
private class NewMap extends JFrame implements ActionListener {
JLabel wlbl = new JLabel("Map width: ");
JTextField w = new JTextField("12");
JLabel hlbl = new JLabel("Map height: ");
JTextField h = new JTextField("8");
JButton create = new JButton("Create map");
public NewMap(Component p) {
super("New Map");
setSize(100, 75);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setLayout(null);
// setVisible(true);
int bw = 96, bh = 24, s = 4, x = s;
wlbl.setBounds(x, s, bw, bh);
x += s + bw;
add(wlbl);
w.setBounds(x, s, bw, bh);
x += s + bw;
add(w);
hlbl.setBounds(x, s, bw, bh);
x += s + bw;
add(hlbl);
h.setBounds(x, s, bw, bh);
x += s + bw;
add(h);
create.setBounds(x, s, bw, bh);
x += s + bw;
create.addActionListener(this);
add(create);
setSize(getWidth() - this.getContentPane().getWidth() + x, getHeight()
- this.getContentPane().getHeight() + s + s + bh);
setLocationRelativeTo(p);
}
public void actionPerformed(ActionEvent e) {
try {
mapBox.create(Integer.parseInt(w.getText()),
Integer.parseInt(h.getText()));
} catch (NumberFormatException ex) {
return;
}
dispose();
}
}
}
我喜欢自己做事情,我不太喜欢使用布局或JFrame子类,但如果需要,我可以将其更改为类似的内容。对于SSCCE,我完全会这样做,除了减少它时没有问题。例如,如果我复制地图编辑器的所有代码,将其粘贴到新项目中ct,然后简单地添加一个主方法来启动第一个JFrame,那么我就没有问题了。@user1828361:使用布局管理器并不会阻止您自己做事情,而是会使维护和更新代码变得更容易。至于在减少代码时不会出现问题,我认为这是您的主要任务--to尝试通过减少代码直到错误消失来隔离错误。至于不喜欢子类JFrame——但这正是你在上面所做的,所以你的陈述让我困惑。嗯,我找到了答案,将其编辑到问题中,是的,我确实需要隔离问题…@user1828361:你能在显示窗口的位置显示代码吗是根据你上面发布的代码创建的?我隔离了问题,并修复了它,如果你想知道问题是什么,请阅读问题的编辑。我喜欢自己做事情,我不太喜欢使用布局或JFrame子类,但如果需要,我可以将其更改为类似的内容。在SSCCE方面,我希望我可以这样做,除了这一点,当我减少它时,我没有问题。例如,如果我复制地图编辑器的所有代码,将其粘贴到一个新项目中,然后简单地添加一个启动第一个JFrame的主方法,那么我就没有问题。@user1828361:使用布局管理器不会阻止你自己做事情,但是r它使维护和更新代码变得更加容易。至于减少代码时没有问题,那么我认为这就是你的主要任务——通过减少代码直到错误消失来隔离错误。至于不喜欢子类JFrame——但这就是你上面所做的,所以你的陈述让我困惑。嗯,我想把它删掉,编辑成问题,是的,我确实需要隔离这个问题…@user1828361:你能在显示窗口的地方显示代码吗?这个窗口是根据你上面发布的代码创建的?我隔离了这个问题,并修复了它,如果你想看看问题是什么,请阅读问题的编辑