【问题标题】:My setText isn't updating my JTextField?我的 setText 没有更新我的 JTextField?
【发布时间】:2012-03-16 19:34:22
【问题描述】:

所以我在这里遗漏了一些东西,但我无法让我的 setAttributes 方法像它应该的那样更新敏捷文本字段。调用该方法和调用的类可以正常传递所需的值,如 system.out.println(agility) 所见。但是,在下面的行中,我尝试设置 agilityAmount txtfield,但它不显示。我做错了什么?

    class GraphicalInterface extends JFrame implements Runnable, KeyListener
{


    GraphicalInterface() {
    }
    JFrame mainwindow;

    //declare panels
    JPanel panel1=new JPanel();
    JPanel panel2=new JPanel();
    JPanel invictusWelcome=new JPanel();
    JPanel gameStartQuit=new JPanel();
    JPanel panel3=new JPanel();
    JPanel panel4=new JPanel();
    JPanel panel5=new JPanel();
    JPanel panel6=new JPanel();
    JPanel panel7=new JPanel(new GridLayout(7, 1));
    JPanel mappanel=new JPanel();
    JPanel attribpanel=new JPanel(new GridLayout(12,0));

    //declare buttons
    JButton startGame;
    JButton quitButton;
    JButton mapButton;
    JButton townButton;
    JButton castleButton;
    JButton forestButton;
    JButton exitButton;
    JButton fighterSelect;
    JButton hybridSelect;
    JButton magicSelect;
    JButton characterConfirm;

    //declare labels
    JLabel agility=new JLabel("Agility:");
    JLabel health=new JLabel("Health: ");
    JLabel mana=new JLabel("Mana: ");
    JLabel strength=new JLabel("Strength: ");
    JLabel intelligence=new JLabel("Intelligence: ");
    JLabel charNameLabel=new JLabel("Name: ");
    JTextField charNameLabelField=new JTextField();
    JTextField healthAmount=new JTextField();
    JTextField manaAmount=new JTextField();
    JTextField currentHealthAmount=new JTextField();
    JTextField currentManaAmount=new JTextField();
    JTextField strengthAmount=new JTextField();
    JTextField agilityAmount=new JTextField();
    JTextField intelligenceAmount=new JTextField();


    JTextField description;

    //declares attribute values
    String name="";
    int agi=0;
    int str=0;
    int intel=0;
    int currentHealth=0;
    int maxHealth=0;
    int currentMana=0;
    int maxMana=0;
    int location=0;


    //Initialize Listener Object
    Listener aListener= new Listener();
    Location locationObj= new Location();


    //images
    private Image map;
    private Image attribBackground;
    private Image charMagic;
    private Image charHybrid;
    private Image charFighter;
    private Image town;
    private Image cave;
    private Image forest;
    private Image castle;
    private Image characterIcon;

    protected void initializeWindows()
    {
        mainwindow=new JFrame("Invictus Maneo");
        mainwindow.setVisible(true);
        mainwindow.setPreferredSize(new Dimension(900, 700));
        mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainwindow.setResizable(false);

        panel1.setVisible(true);
        panel1.setPreferredSize(new Dimension(800, 550));
        mainwindow.getContentPane().add(panel1, BorderLayout.NORTH);

        panel2.setVisible(true);
        panel2.setPreferredSize(new Dimension(800, 100));

        //Initializes buttons
        startGame=new JButton("Begin Adventure");
        panel2.add(startGame, BorderLayout.SOUTH);
        startGame.addActionListener(aListener);

        quitButton=new JButton("Quit");
        panel2.add(quitButton, BorderLayout.SOUTH);
        quitButton.addActionListener(aListener);

        mapButton=new JButton("Map");
        mapButton.addActionListener(aListener);

        townButton=new JButton("Town");
        townButton.addActionListener(aListener);

        castleButton=new JButton("Castle");
        castleButton.addActionListener(aListener);

        exitButton=new JButton("Exit Map");
        exitButton.addActionListener(aListener);

        forestButton=new JButton("" +"Forest");
        forestButton.addActionListener(aListener);

        fighterSelect=new JButton("Fighter");
        fighterSelect.addActionListener(aListener);
        hybridSelect=new JButton("Hybrid");
        hybridSelect.addActionListener(aListener);
        magicSelect=new JButton("Magic");
        magicSelect.addActionListener(aListener);

        //initialize attribute panel
        //attribpanel=new JPanel(new GridLayout(12,0));
        attribpanel.setVisible(false);
        attribpanel.setPreferredSize(new Dimension(200, 500));
        attribpanel.setBackground(null);


        attribpanel.add(charNameLabel);
        attribpanel.add(charNameLabelField);
        attribpanel.add(health);
        attribpanel.add(healthAmount);
        attribpanel.add(mana);
        attribpanel.add(manaAmount);
        attribpanel.add(strength);
        attribpanel.add(strengthAmount);
        attribpanel.add(agility);
        attribpanel.add(agilityAmount);
        attribpanel.add(intelligence);
        attribpanel.add(intelligenceAmount);

        mainwindow.getContentPane().add(panel2, BorderLayout.SOUTH);

        panel3.setVisible(true);
        panel3.setPreferredSize(new Dimension(200, 600));

        panel4.setVisible(true);
        panel4.setPreferredSize(new Dimension(650, 500));


        panel5.setVisible(true);
        panel5.setPreferredSize(new Dimension(600, 600));
        panel5.setBackground(getBackground());

        panel6.setVisible(false);
        panel6.setPreferredSize(new Dimension(500, 500));

        panel7.setVisible(false);
        panel7.setPreferredSize(new Dimension(100, 200));


        //Adds sub panels to panel 1
        panel1.add(panel3, BorderLayout.NORTH);
        panel1.add(panel4, BorderLayout.NORTH);

        //panel5.setBorder(BorderFactory.createLineBorder (Color.black, 2));
        //panel6.setBorder(BorderFactory.createLineBorder (Color.black, 2));
        panel2.setBorder(BorderFactory.createLineBorder (Color.black, 2));
        attribpanel.setBorder(BorderFactory.createLineBorder (Color.black, 2));
        //panel4.setBorder(BorderFactory.createLineBorder (Color.black, 2));

        panel4.add(panel5, BorderLayout.NORTH);

        loadPictures();

        BufferedImage myPicture;
        try {
            myPicture = ImageIO.read(new File("C:\\test\\GameTitle.jpg"));
            JLabel gameTitle = new JLabel(new ImageIcon( myPicture ));
            panel5.add( gameTitle );
            panel5.repaint();
            panel1.repaint();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        BufferedImage map;
        try {
            map = ImageIO.read(new File("C:\\test\\map.jpg"));
            JLabel mapLabel = new JLabel(new ImageIcon( map ));
            panel6.add( mapLabel );
            panel1.repaint();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        mainwindow.pack();


        }   

    void setAttributePanel(int s, int a, int i, int h, int m)
    {

         agi=a;
         str=s;
         intel=i;
         currentHealth=h;
         maxHealth=h;
         currentMana=m;
         maxMana=m;

         System.out.println(agi+": agility");
         agilityAmount.setText(agi+"");
         attribpanel.repaint();
         panel3.repaint();
         panel1.repaint();


    }




private class Listener implements ActionListener
 {

    public void actionPerformed(ActionEvent a) 
    {
        if (a.getSource()==quitButton)
        {
            System.exit(0);
        }

        else if (a.getSource()==startGame)
        {

            startGame.setVisible(false);
            panel2.add(mapButton);
            mapButton.setVisible(true);
            panel2.repaint();
            panel5.setVisible(false);
            panel3.add(attribpanel);
            attribpanel.setVisible(true);
            Character charChosenObj= new Character();
            charChosenObj.characterCreation();
            attribpanel.repaint();
            panel3.repaint();
        }

        else if (a.getSource()==mapButton)
        {
            System.out.println("Where are you going?");
            panel4.add(panel6, BorderLayout.CENTER);
            panel4.add(panel7);
            panel7.add(townButton);
            panel7.add(forestButton);
            panel7.add(castleButton);
            panel7.add(exitButton);
            panel5.setVisible(false);
            panel6.setVisible(true);
            panel7.setVisible(true);
            panel4.repaint();


        }

        else if (a.getSource()==exitButton)
        {
            panel5.setVisible(true);
            panel6.setVisible(false);
            panel7.setVisible(false);

        }

        else if (a.getSource()==townButton)
        {
            System.out.println("Pick up some milk will ya?");
            locationObj.setLocation(1);

        }

        else if (a.getSource()==forestButton)
        {
            System.out.println("Watch for falling trees.");
            locationObj.setLocation(2);
            Combat combatObj=new Combat();
            combatObj.startCombat();

        }

        else if (a.getSource()==castleButton)
        {
            System.out.println("Are you sure... it's kinda dark in there?");
            locationObj.setLocation(3);


        }

    }

 }

【问题讨论】:

  • 如果你做一个 getText,它会给出正确的值吗?
  • 你在哪里调用setAttributePanel?那是正在做setText的方法,你似乎没有从任何地方调用它?
  • 我和@ringbearer 在一起——你没有提供足够的信息来解决这个问题。据我们所知,您可以拥有多个 GraphicalInterface,一个显示的,另一个不显示的获取其文本集。
  • @ringbearer setAttributePanel() 方法是在他们选择了他们想要的类(战斗机、混合动力、魔术师)之后从我的 Character 类中调用的。然后它将所有属性分配给正确的值。之后调用该方法并传递所有属性。我已经在我的代码中搜索了可能的 gui 副本,但我没有找到任何东西。我会发布我的整个程序,但这将涉及至少 5 个课程。 编辑 现在我通过 GraphicalInterface guiObject= new GraphicalInterface() 在需要它的每个其他类中创建一个新对象。
  • 我还阅读了其他问题,例如我的关于使用“this”通过类构造函数传递对象的问题,但我只是不太明白他们在说什么要使用。我不知道这是否可能是我的问题,不将我的 gui 传递给其他类使用。

标签: java swing jtextfield settext


【解决方案1】:

mainwindow.setVisible(true); 必须是 GUI 构造函数的最后一行

【讨论】:

  • 我确实通过将那行代码切换到最后来解决这个问题,我绝对更喜欢视觉方面。
【解决方案2】:

如果您有一个组件布局和可见,然后更改首选大小(例如通过添加文本),您必须强制一个新的布局周期,例如通过“无效”组件或“验证”容器。

还有其他问题

  • 为什么你的 GraphicalInterface 是一个 JFrame 而你不打算这样使用它
  • 在侦听器中添加组件时,您应该准备好将它们移除到其他地方或仅通过“setVisible”处理可见性
  • 也许更好地使用一些将其子级调整为可用大小的布局,例如 GridBagLayout
  • 正如 mKorbel 所提到的,只有在完全配置容器时才应该“setVisible”。您将改善用户体验:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多