[英]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.