我的Jframe不会出现。代码没有错误

时间:2014-05-04 13:18:29

标签: java eclipse swing jframe

我不知道最新情况:(我们有设计宠物游戏的任务,比如tamadotchi游戏。我的宠物是老虎。简单的想法。但是......显然eclipse有其他计划:(

enter code here

    import java.awt.BorderLayout;

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Timer;

    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.JProgressBar;
    import javax.swing.JTextArea;

    public class inter {

private JFrame tigerWindow;
private JPanel topPanel, leftPanel, rightPanel, bottomPanel, middlePanel, allPanels;
private JLabel tigerLabel, happyLabel, hungryLabel, energyLabel, hygieneLabel, picLabel;
private JButton play, sleep, feed, hunt, swim, walk, shop, home;
private JProgressBar happyBar, hungryBar, energyBar, hygieneBar;
private JTextArea tigerText;
private JProgressBar happy, hungry, energy, hygiene;
private int interval;
private Timer timer;

private int tigerHungerCount = 5;
private int tigerHygieneCount = 5; 
private int tigerHappyCount = 5;
private int tigerEnergyCount = 5;


        Pet tiger = new Pet ("Meat", "Roll", "Roar", "Yellow", "zzzZZZ", "Swim", "Walk",         "Claw", "Hunt", "Mark Territory", 22);

    public inter() {
        topPanel = new JPanel();
        topPanel.setBackground(Color.lightGray);
        leftPanel = new JPanel();
        leftPanel.setBackground(Color.gray);
        rightPanel = new JPanel();
        rightPanel.setBackground(Color.gray);
        bottomPanel = new JPanel();
        bottomPanel.setBackground(Color.darkGray);
        middlePanel  = new JPanel();
        middlePanel.setBackground(Color.lightGray);
        allPanels = new JPanel();

        allPanels.setLayout(new BoxLayout(allPanels, BoxLayout.Y_AXIS));

        topPanel.setLayout(new GridLayout(2,0));


        play = new JButton("Play");
        sleep = new JButton("Sleep");
        feed = new JButton("Feed");
        hunt = new JButton("Hunt");
        swim = new JButton("Swim");
        walk = new JButton("Roam");
        shop = new JButton("Shop");
        home = new JButton("Home");
        tigerText = new JTextArea();
        bottomPanel.setBorder(BorderFactory.createEmptyBorder(25,25,25, 25));
        bottomPanel.setLayout(new GridLayout(0, 4, 50, 10));
        bottomPanel.add(play);
        bottomPanel.add(sleep);
        bottomPanel.add(feed);
        bottomPanel.add(hunt);
        bottomPanel.add(swim);
        bottomPanel.add(walk);
        bottomPanel.add(shop);
        bottomPanel.add(home);
        middlePanel.add(tigerText);


        leftPanel.setBorder(BorderFactory.createEmptyBorder());     
            ImageIcon image = new ImageIcon("Images/Timmy the Tiger.jpg");
            leftPanel.add(new JLabel(image));
            leftPanel.setVisible(true);

        rightPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
        rightPanel.setLayout(new GridLayout(0, 1));

        tigerText = new JTextArea(); 
        tigerLabel = new JLabel(); 
        middlePanel.setLayout(new GridLayout(2,0)); 
        middlePanel.add(tigerLabel); 
        middlePanel.add(tigerText); 

        happyBar = new JProgressBar();
        happyBar.setMinimum(0);
        happyBar.setMaximum(500);
        happyBar.repaint();
        happyLabel = new JLabel("Happy");
        happyLabel.setForeground(Color.white);

        hungryBar = new JProgressBar(0,500);
        hungryLabel = new JLabel("Hungry");
        hungryBar.repaint();
        hungryLabel.setForeground(Color.white);

        energyBar = new JProgressBar(0,500);
        energyLabel = new JLabel("Energy");
        energyBar.repaint();
        energyLabel.setForeground(Color.white);

        hygieneBar = new JProgressBar(0,500);
        hygieneBar.setIndeterminate(true);
        hygieneLabel = new JLabel("Hygiene");
        hygieneBar.repaint();
        hygieneLabel.setForeground(Color.white);


        rightPanel.add(happyBar);
        rightPanel.add(happyLabel);
        happyLabel.setBounds(5, 5, 5, 5);
        happyBar.setBounds(15, 5, 5, 5);

        rightPanel.add(hungryBar);
        rightPanel.add(hungryLabel);
        hungryLabel.setBounds(5, 5, 5, 5);
        hungryBar.setBounds(15, 5, 5, 5);

        rightPanel.add(energyBar);
        rightPanel.add(energyLabel);
        energyLabel.setBounds(5, 5, 5, 5);
        energyBar.setBounds(15, 5, 5,5);

        rightPanel.add(hygieneBar);
        rightPanel.add(hygieneLabel);
        hygieneLabel.setBounds(5, 5, 5, 5);
        hygieneBar.setBounds(15, 5, 5, 5);

        hygieneBar.setValue(tigerHygieneCount);
        hungryBar.setValue(tigerHungerCount);
        energyBar.setValue(tigerEnergyCount);
        happyBar.setValue(tigerHappyCount);

        allPanels.add(topPanel);
        allPanels.add(leftPanel);
        allPanels.add(middlePanel);
        allPanels.add(bottomPanel);
        allPanels.add(rightPanel);


        tigerWindow = new JFrame();
        tigerWindow.setTitle("Pet Game: Tiger");
        tigerWindow.setSize(800,300);
        tigerWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tigerWindow.setVisible(true);
        tigerWindow.add(allPanels);

        tigerWindow.getContentPane().add(leftPanel,BorderLayout.WEST);
        tigerWindow.getContentPane().add(rightPanel,BorderLayout.EAST);
        tigerWindow.getContentPane().add(bottomPanel,BorderLayout.SOUTH);
        tigerWindow.getContentPane().add(topPanel,BorderLayout.NORTH);

    }

        public JButton getPlayButton() {
            return play;
        }

        public JButton getSleepButton() {
            return sleep;
        }

        public JButton getFeedButton() {
            return feed;
        }

        public JButton getHuntButton() {
            return hunt;
        }

        public JButton getWalkButton() {
            return walk;
        }

        public JButton getSwimButton() {
            return swim;
        }

        public JButton getShopButton() {
            return shop;
        }

        public JButton getHomeButton() {
            return home;
        }

        public JPanel getPanels() {
            return allPanels;
        }

        play.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        tiger.getPlay();
        updateProgressBars();
        tigerText.append("You have played with your pet\n");
        tiger.death();
    }
        });

        sleep.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                tiger.getSleep();
                updateProgressBars();
                tigerText.append("Your dog is asleep: Z Z Z\n");
                tiger.death();
            }
        });

        feed.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                tiger.getFeed();
                updateProgressBars();
                tigerText.append("You have fed your pet\n");
                tiger.death();
            }
        });

        hunt.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                tiger.getHunt();
                updateProgressBars();
                tigerText.append("Your Tiger has gone to hunt");
                tiger.death();
            }
        });

        walk.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                tiger.getRoam();
                updateProgressBars();
                tigerText.append("Your Tiger has begun to roam");
                tiger.death();
            }
        });

        swim.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                tiger.getSwim();
                updateProgressBars();
                tigerText.append("You have washed your dog\n");
                tiger.death();
            }
        });
            }

        {
            home.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){ 
                }
            });

            shop.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){ 
                }
            });

        }

    public void updateProgressBars()
    {
        hungryBar.setValue(tiger.getHungry());
        energyBar.setValue(tiger.getEnergy());
        hygieneBar.setValue(tiger.getHygiene());
        happyBar.setValue(tiger.getHappy());
    }


    public void death()
    {
        tiger.death();
        tigerText.append("Dog has died\n");
    }



    public void play() {
        hungryBar.setValue(tigerHungerCount--);
        energyBar.setValue(tigerEnergyCount--);
        hygieneBar.setValue(tigerHygieneCount--);
        happyBar.setValue(tigerHappyCount++);
        tigerText.append("You have played with your tiger");
        tigerText.append("\n");
    }

    public void sleep() {
        hungryBar.setValue(tigerHungerCount--);
        energyBar.setValue(tigerEnergyCount ++);
        hygieneBar.setValue(tigerHygieneCount --);
        happyBar.setValue(tigerHappyCount ++);
        tigerText.append("You let your tiger sleep");
        tigerText.append("\n");
    }

    public void feed() {
        hungryBar.setValue(tigerHungerCount++);
        energyBar.setValue(tigerEnergyCount --);
        hygieneBar.setValue(tigerHygieneCount --);
        happyBar.setValue(tigerHappyCount ++);
        tigerText.append("You have fed your tiger");
        tigerText.append("\n");
    }

    public void hunt() {
        hungryBar.setValue(tigerHungerCount++);
        energyBar.setValue(tigerEnergyCount --);
        hygieneBar.setValue(tigerHygieneCount --);
        happyBar.setValue(tigerHappyCount ++);
        tigerText.append("You let your tiger hunt");
        tigerText.append("\n");
    }

    public void walk() {
        hungryBar.setValue(tigerHungerCount++);
        energyBar.setValue(tigerEnergyCount --);
        hygieneBar.setValue(tigerHygieneCount --);
        happyBar.setValue(tigerHappyCount ++);
        tigerText.append("You made your tiger roam");
        tigerText.append("\n");
    }

    public void swim() {
        hungryBar.setValue(tigerHungerCount--);
        energyBar.setValue(tigerEnergyCount --);
        hygieneBar.setValue(tigerHygieneCount ++);
        happyBar.setValue(tigerHappyCount ++);
        tigerText.append("You made your tiger go for a swim");
        tigerText.append("\n");
    }

        }

请帮帮忙? 我已经尝试了一切,似乎没有什么可以做的。它在2天前工作正常,接下来我知道它根本没有出现。我没有线索!我没有看到它不起作用的原因。我很困在这里。请帮忙。

2 个答案:

答案 0 :(得分:0)

首先尝试使用带有空标签的简单JFrame,如下所示:

public class JFrameDemo {

    public static void main(String s[]) {
        JFrame frame = new JFrame("JFrame Source Demo");
        // Add a window listner for close button
        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        // This is an empty content area in the frame
        JLabel jlbempty = new JLabel("");
        jlbempty.setPreferredSize(new Dimension(175, 100));
        frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}

如果它正在工作,请继续满足您的需求。

答案 1 :(得分:0)

在我看来,你的inter类没有扩展JFrame。

在声明类块时尝试放置它:

    public class inter extends JFrame {...

我认为这会有所帮助。