[英]Cannot edit JTextField in popup JFrame
好的,我在弹出的JFrame中遇到了文本字段的问题……实际上我有2个单独的程序,一个是游戏,一个是地图编辑器。 我决定使游戏具有某种内置的地图编辑器,因此我将地图编辑器中的类添加到了游戏项目中的新程序包中,进行了一些小的调整(例如从地图编辑器中删除main()方法) ),然后使工作正常。 地图编辑器作为新的JFrame弹出,当我单击“新建”按钮时,它将打开一个新的JFrame,其中包含几个TextField和一个按钮,用于请求新地图的宽度和高度。 我无法编辑文本字段中的值...而且我也不知道为什么...弹出代码:
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()之类的事情,并且对于框架和我在网上找到的其他一些解决方案也是如此,但是没有一个对我有用。
解决的问题:在解释了我的问题之后,我意识到问题HAD可能存在于游戏代码中某个地方,可能位于JFrame的类中,并且我注意到我做了一些奇怪的事情,而不是使用KeyListener实现来处理输入。以前,我曾尝试以JApplet的形式创建游戏,但在JApplet上使用Keylisteners遇到麻烦。 我将Main类设为KeyEventDispatcher或类似的东西。 感谢您的帮助,这是一个愚蠢的问题(我只是在错误的位置寻找解决方案)。
因此,KeyEventDispatcher没有正确地将关键操作“分发”到其他组件。 我只是回到传统的KeyListener,因为它在JFrame中对我来说很好用。
我怀疑问题在于您如何调用此新窗口,也许您有一个无限循环或冻结了Swing事件分发线程或EDT的其他某种构造,从而使GUI无响应。 如果您可以创建和发布SSCCE,以便我们进行编译,测试,运行以及可能的修改和更正,我们将非常了解。
同样在我看来,您的代码还有其他重大但与主要问题无关的问题:
例如,一些快速生成的示例代码:
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.