简体   繁体   English

如何将一个代码添加到四个类?

[英]How to add one code to four classes?

I have a code which is done coded but I realized its hard to see in the code so what I want do is to add four classes which one will be for Viewer, UserInput, Player and Controller. 我有一个已完成编码的代码,但是我意识到很难在代码中看到它,因此我想要做的是添加四个类,一个用于Viewer,UserInput,Player和Controller。

  • The viewer is to see the game information (Which is the JPanel most of all) 观看者将看到游戏信息(最重要的是JPanel)
  • UserInput is to let the user choose options with buttons. UserInput是让用户使用按钮选择选项。
  • Player is where the computer information will be (Functions for the CPU) 播放器是计算机信息所在的位置(CPU的功能)
  • Controller is where all the mathematics will be, New game (Restart when pressing New game Button), Make points and so on. 控制器是所有数学的存放地,新游戏(按“新游戏”按钮时重新启动),积分等。

So I made a class where it should start all the classes with by run that class 所以我做了一个课,它应该通过运行该课开始所有课

            SSPPlayer player = new SSPPlayer();
            SSPViewer viewer = new SSPViewer();
            SSPController controller = new SSPController();
            SSPUserInput userInput = new SSPUserInput();

            JFrame frame1 = new JFrame( "SSPViewer" );
            frame1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame1.add( viewer );
            frame1.pack();
            frame1.setVisible( true );

            JFrame frame2 = new JFrame( "SSPUserInput" );
            frame2.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame2.add( userInput );
            frame2.pack(); frame2.setVisible( true );
        }
    }

However, The problem is now that I have made so far two classes which should be in each window for Viewer and UserInput but I could only get viewer to work. 但是,现在的问题是,到目前为止,我已经为Viewer和UserInput在每个窗口中创建了两个类,但是我只能使Viewer正常工作。

Viewer class 观看者类别

public class SSPViewer extends JFrame{

    private JPanel  resultatPanel;
    private JLabel  infoLabel, resultatLabel;
    private JTextField UserResult, CompResult;


    SSPViewer() {
        super("SSPViewer");
        setSize(600, 400);
        UI();
    }

    public void UI() {
        Container container = getContentPane();  

        resultatPanel = new JPanel(new GridLayout(2,2));  
        resultatPanel.setBackground(Color.WHITE);
        UserResult = new JTextField(" Dina vinster:  ");
        resultatPanel.add(användarResultat);
        CompResult= new JTextField(" Datorns vinster:  ");
        resultatPanel.add(datorResultat);
        infoLabel = new JLabel("Börja spela genom att ", JLabel.RIGHT);
        resultatPanel.add(infoLabel);    
        resultatLabel = new JLabel("använda knapparna längst ner!", JLabel.LEFT);
        resultatPanel.add(resultatLabel); 
        container.add("Center",resultatPanel);

        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
    }
}

UserInPut Class UserInPut类别

public class SSPUserInput extends JFrame implements ActionListener {
    final int ROCK = 0, Sciss = 1, Paper= 2;


    private JButton Rock; 
    private JButton Scoss;  
    private JButton Paper; 
    private JButton newgame, exit;
    private JPanel  UserPanel, UserPanel1;

    SSPUserInput() {
        super("SSPUserInput");
        setSize(800 , 600);
        UI1();
    }
    public void UI1() {
        Container container = getContentPane();  

        UserPanel= new JPanel();
        UserPanel.setLayout(new GridLayout(0,4));
        UserPanel.add(new JLabel("Du väljer här:", JLabel.CENTER));
        Rock = new JButton("Rock");
        Sciss  = new JButton("Sciss");
        PAper = new JButton("Paper");

        Rock.addActionListener(this);
        Sciss.addActionListener(this);
        Paper.addActionListener(this);

        UserPanel.add(rock); 
        UserPanel.add(Sciss); 
        UserPanel.add(Paper);

        container.add("South", userpanel);

        UserPanel1= new JPanel();

        newgame= new JButton("New Game");
        exit= new JButton("Exit");
        exit.addActionListener(this);
        UserPanel1.add(exit);
        newgame.addActionListener(this);
        UserPanel1.add(newgame);
        container.add("North", UserPanel1);
    }

    public void actionPerformed(ActionEvent e) {
        int val = 0;

        if(e.getSource() == exit) {

            System.exit(0); 
        }
        else if(e.getSource() == newgame) {

        }
        else if (e.getSource() == Rock) {
            val = Rock;       
        }
        else if (e.getSource() == Sciss) {
            val = Sciss;
        }
        else {
            val = Paper; 
        }


    }

}

So I was wondering now why it doesn't work. 所以我现在想知道为什么它不起作用。 It seems like I have done it right but... 看来我做对了,但是...

It would be easier too me and you if anyone is available teamviewer or skype. 如果有人可以使用teamviewer或skype,我和您也将更容易。

EDIT: If there is something you don't understand in the code. 编辑:如果您在代码中看不懂某些内容。 Just comment! 只是评论!

ERRORS IM GETTING IS FOLLWING: IM正在获取错误:

Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    at java.awt.Container.checkNotAWindow(Unknown Source)
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at Redovisning3.Test.main(Test.java:15)

SLOVED THIS PROBLEM. 解决了这个问题。 To solve this problem which I had before was to just let row 15 and 22 be as a comment for a while until you have done your all classes. 要解决我以前遇到的这个问题,只是让第15和22行作为注释一段时间,直到您完成所有课程为止。

I have done and wrote how to solved the problem I had before (Incase someone is having same trouble as me in the future). 我已经完成并写下了如何解决以前遇到的问题(以防将来有人遇到与我相同的麻烦)。 However there is another problem which now contain a if-else statements. 但是,还有另一个问题,现在包含if-else语句。 So I have a class which allows you to pick SCiss, paper and rock. 因此,我有一堂课,让您选择SCiss,纸张和岩石。 Which of these has they own number. 其中哪个拥有自己的电话号码。

Rock = 0, Sciss = 1, Paper = 2. 岩石= 0,剪刀= 1,纸= 2。

I have following using this in one of the class because it is needed to know what button you are pressing. 我已经在班级之一中使用了此功能,因为需要知道您所按下的按钮。

public void actionPerformed(ActionEvent e) {
    int val = 0  ;

    if(e.getSource() == exit) {

        System.exit(0); 
    }
    else if(e.getSource() == Newgame) {
       //Nothing here yet.
    }
    else if (e.getSource() == Rock) {
        choice = Rock;       
    }
    else if (e.getSource() == Sciss) {
        choice = Sciss;
    }
    else {
        choice = Paper; 
    }
    Sum(choice );

and what I trying to do is when you are pressing one of these three buttons it should save the Number in sum(choice) and then go to another class to make other functions, by that I mean this: 我想做的是当您按下这三个按钮之一时,它应该将Number保存在sum(choice)中,然后转到另一个类以执行其他功能,这是我的意思:

 public void berakna(int val){
        Userchoice = choice ;     
        Compchoice = (int)(Math.random() * 3);
        resultatLabel.setHorizontalAlignment(0); 
        infoLabel.setHorizontalAlignment(0);   
        infoLabel.setText("Result round" + ++round +":");   

        if (Userchoice == Compchoice )
            resultatLabel.setText("Tie!!");   
        else if (Userchoice == Rock &&  Compchoice == Sciss|| 
                Userchoice == Sciss &&  Compchoice == Paper ||
                Userchoice == Paper &&  Compchoice == Rock ) {
            resultLabel.setText("You win! One more time?");
            UserResult.setText(" You won: " + ++UserWin + "pcs");
        }
        else { 
            result.setText(" You lose, Try one more time!");
            CompResult.setText(" Comp won: " + ++CompWin+ "st");
        }
    }
}

I was thinking using return, so I tried but it didn't got me any forward. 我当时在考虑使用return,所以我尝试了一下,但没有让我前进。 So ideas would be greatful. 所以想法会很棒。 There is also no errors. 也没有错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM