它打印出" works"但它没有设置标签可见。这是为什么?

时间:2016-01-02 11:13:07

标签: java swing

这是我的代码。它从save.txt文件中读取,它获得BMG = 1。它打印出应该具有的作品,但它不会将标签设置为可见。我从另一个班级运行该程序。

public void openFile(){
    try{
        x=new Scanner(new File("save.txt"));
    }catch(Exception e){
        System.out.println("Could not find save file!");
    }
}
public void readFile(){
    while(x.hasNext()){
        String a = x.next();
        if(a.equals("BMG=1")){
            System.out.println("works");
            bronze.setVisible(true);
        }
        System.out.println(a);
    }
}
public void closeFile(){
    x.close();
}

此处为完整代码。对不起,如果它非常混乱它只是因为我测试的东西,我只是不断添加这个:)

        back.setSize(1280,800);
        back.setLocation(0,0);

        bronze.setSize(50,55);
        bronze.setLocation(10,50);
        bronze.repaint();
        silver.setSize(50,55);
        silver.setLocation(10,105);
        silver.repaint();
        gold.setSize(50,55);
        gold.setLocation(10,210);
        gold.repaint();
        timer.setSize(200,40);
        timer.setLocation(10,20);
        add(timer);
        setLayout(null);
        scorelbl.setSize(100, 20);
        scorelbl.setLocation(10, 10);
        add(bronze);
        add(silver);
        bronze.setVisible(false);
        silver.setVisible(false);
        add(scorelbl);
        Random X=new Random();
        int x=X.nextInt(1100-50)+50;
        Random Y=new Random();
        int y=Y.nextInt(650-50)+50;
        easy.setSize(100,30);
        easy.setLocation(400, 10);
        add(easy);

        scorelbl.setForeground(Color.YELLOW);
        timer.setForeground(Color.YELLOW);
        easy.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(started==true){
                    JOptionPane.showMessageDialog(null, "You can't change the difficulty of the game while its running!"
                            ,"Error: Tried Changing Difficulty while running",JOptionPane.INFORMATION_MESSAGE);
                }else{
                wait=4;
                }
            }
        });
        medium.setSize(100,30);
        medium.setLocation(500, 10);
        add(medium);
        medium.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(started==true){
                    JOptionPane.showMessageDialog(null, "You can't change the difficulty of the game while its running!"
                            ,"Error: Tried Changing Difficulty while running",JOptionPane.INFORMATION_MESSAGE);
                }else{
                wait=3;
                }
            }
        });
        hard.setSize(100,30);
        hard.setLocation(600, 10);
        add(hard);
        hard.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(started==true){
                    JOptionPane.showMessageDialog(null, "You can't change the difficulty of the game while its running!"
                            ,"Error: Tried Changing Difficulty while running",JOptionPane.INFORMATION_MESSAGE);
                }else{
                wait=2;
                }
            }
        });
        insane.setSize(100,30);
        insane.setLocation(700, 10);
        add(insane);
        insane.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(started==true){
                    JOptionPane.showMessageDialog(null, "You can't change the difficulty of the game while its running!"
                            ,"Error: Tried Changing Difficulty while running",JOptionPane.INFORMATION_MESSAGE);
                }else{
                wait=1;
                }
            }
        });
        aim.setSize(20, 20);
        aim.setLocation(x,y);
        aim.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                Random X=new Random();
                int x=X.nextInt(1100-50)+50;
                Random Y=new Random();
                int y=Y.nextInt(650-50)+50;
                aim.setLocation(x, y);
                score+=1;
                scorelbl.setText("Your Score is "+score+"!");
                tm.start();
                seconds=0;
                started=true;
                if(score==2 && wait==3){
                    bronze.setVisible(true);
                }
                if(score==3 && wait==3){
                    silver.setVisible(true);
                }
            }
        });
        add(aim);
        add(back);
    }
    public void openFile(){
        try{
            x=new Scanner(new File("save.txt"));
        }catch(Exception e){
            System.out.println("Could not find save file!");
        }
    }
    public void readFile(){
        while(x.hasNext()){
            String a = x.next();
            if(a.equals("BMG=1")){
                System.out.println("works");
                bronze.setVisible(true);
            }
            System.out.println(a);
        }
    }
    public void closeFile(){
        x.close();
    }
    public static void main(String[] args){

    }
    Thread thread= new Thread();
    public void actionPerformed(ActionEvent e) {

        counter--;
        timer.setText("Seconds Until Finish "+counter);
        if(counter==0){
            tm.stop();
            JOptionPane.showMessageDialog(null,"You've finished with a score of "+score+". Press OK to exit!","Time's Up!",JOptionPane.INFORMATION_MESSAGE);
            System.exit(0);
        }
        seconds++;
        if(seconds==wait){
            Random X=new Random();
            int x=X.nextInt(1100-50)+50;
            Random Y=new Random();
            int y=Y.nextInt(650-50)+50;
            aim.setLocation(x, y);
            seconds=0;
        }
    }
}

2 个答案:

答案 0 :(得分:3)

    bronze.setSize(50,55);
    bronze.setLocation(10,50);
    bronze.repaint();
    silver.setSize(50,55);
    silver.setLocation(10,105);
    silver.repaint();
    gold.setSize(50,55);
    gold.setLocation(10,210);
    gold.repaint();

在我看来,你正试图让3个组件共享同一个位置。这不行。

相反,你应该做的是:

  1. 使用单个JLabel。然后,您可以将图标更改为青铜色,银色或金色图标。您可以使用setIcon(...)方法执行此操作。

  2. 使用卡片布局。然后将3个组件添加到卡中,并使用CardLayout中的方法一次显示一个组件。阅读How to Use CardLayout上Swing教程中的部分,了解更多信息和示例。

  3. 此外,请勿使用空布局!!!

    Swing旨在与布局管理器一起使用。当您查看上面的链接时,您还可以查看Layout Managers部分。

答案 1 :(得分:0)

如果它在Android中你必须调用UI主线程来更新UI。

#include <string.h> //you need this for string comparison

char a[10]; //arbitrary size, just make sure it's big enough for the input
scanf("%s", a);
//OR
fgets(a, sizeof(a), stdin); //use fgets if you need to read more than one word, scanf stops reading at whitespace
//don't use == to compare strings
if (!strcmp(a, "yes")){ //use quotes to delimit "words"
    printf("OK\n");
}