从边框布局中删除面板并添加新面板

时间:2019-05-13 22:00:22

标签: java

我是Java新生。 İ有一个borderCenter,其中包含centerPanel我只想将borderLayout的内容从centerPanel更改为endPanel 我试图用附加的功能做到这一点。但是,当函数正常工作时,centerPanel的内容将保留在bordorLayout中,但我仍然没有找到要删除或使其可见的内容。

逻辑是,当问题的答案为假时,它想成为百万富翁游戏,而不是显示包含问题和选项的中心面板,我想显示包含两个按钮“ restart”和“ exit”的endPanel

public void endScreen(){


    end.setBackground(Color.BLUE); end.setForeground(Color.white);
    restart.setBackground(Color.black); restart.setForeground(Color.white);restart.setFont(new Font("sansserif", Font.BOLD, 25));
    exit.setBackground(Color.BLACK); exit.setForeground(Color.white);exit.setFont(new Font("sansserif", Font.BOLD, 25));


    end.add(restart);
    end.add(exit);
    end.add(b);
    end.setLayout(null);


    restart.setBounds(200,150, 400, 200);
    exit.setBounds(200,450,400,200);
    b.setBounds(0, 0, 800, 800);



    add (end,BorderLayout.CENTER);

    end.setVisible(true);

}



public class Interface2 extends JFrame implements ActionListener{

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    private final double width = screenSize.getWidth();
    private final double height = screenSize.getHeight();

    //int axx=0;

    private final int WINDOW_HEIGHT = 1200;
    private final int WINDOW_WIDTH = 800;

    JRadioButton optionA;
    JRadioButton optionB;
    JRadioButton optionC;
    JRadioButton optionD;
    JButton resign = new JButton ("resign");
    JButton half;
    JButton change;
    JTextField timer;
    JTextField question;
    Questions QText = new Questions();
    JPanel centerPanel = new JPanel();
    ButtonGroup options1;
    int soruSayac=1;
    JTextField m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11,m12;
    File HighFile = new File ("C:\\highest.txt");
    JPanel end = new JPanel();
    JButton restart= new JButton("RESTART");
    JButton exit = new JButton ("EXİT");
    JLabel b=new JLabel();

    int QFlag;
}

public static void main(String[] args) throws IOException, InterruptedException {

    Questions q2 = new Questions();
    Interface2 i2 = new Interface2();



    int a =6;
    //q2.deneme(a);
    //q2.readHighest();
    i2.Interface23();

    }


public void Interface23() throws IOException, InterruptedException{

    setTitle ("Who wants to be a millionaire");
    setSize (WINDOW_HEIGHT,WINDOW_WIDTH);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);

    setLayout(new BorderLayout());
    setLocationRelativeTo(null);


    //// CENTER



    JLabel a;
    JPanel centerPanel;
    centerPanel = new JPanel();
    centerPanel.setLayout(null);

    Questions QText = new Questions();
    //QText.deneme();
    QText.setOptions();
    QText.mechanism();



    question = new JTextField (QText.QuestionArray[QText.QFlag]);
//( question = new JTextField(QText.mechanism());question.setEditable(false);
    optionA = new JRadioButton("A) " +QText.AArray[(QText.QFlag)]); //optionA.addActionListener(this);
    optionB = new JRadioButton("B) " +QText.BArray[(QText.QFlag)]); //optionB.addActionListener(this);
    optionC = new JRadioButton("C) " +QText.CArray[(QText.QFlag)]); //optionC.addActionListener(this);
    optionD = new JRadioButton("D) " +QText.DArray[(QText.QFlag)]); //optionD.addActionListener(this);
    resign=new JButton("Resign"); resign.addActionListener(this);
    half = new JButton (); half.addActionListener(this);
    change = new JButton (); change.addActionListener(this);

    ButtonGroup options1 = new ButtonGroup();

    options1.add(optionA);options1.add(optionB);
    options1.add(optionC);options1.add(optionD);


    timer = new JTextField (); timer.setEditable(false);




    a=new JLabel();

    Image backg = new ImageIcon(this.getClass().getResource("/arkaplan2.png")).getImage();
    a.setIcon(new ImageIcon(backg));
    Image fifty = new ImageIcon(this.getClass().getResource("/5050.jpg")).getImage();
    half.setIcon(new ImageIcon(fifty));
    Image change1 = new ImageIcon(this.getClass().getResource("/change.jpg")).getImage();
    change.setIcon(new ImageIcon(change1));

    centerPanel.add(change);
    centerPanel.add(optionA);centerPanel.add(optionB);
    centerPanel.add(optionC);centerPanel.add(optionD);
    centerPanel.add(question);centerPanel.add(timer);centerPanel.add(resign);
    centerPanel.add(a); centerPanel.add(half);


    half.setBounds(230, 100, 100, 58); change.setBounds(400, 100, 98, 60);
    optionA.setBounds(112, 475, 250, 75);optionB.setBounds(438, 475, 250, 75);
    optionC.setBounds(112, 600, 250, 75);optionD.setBounds(438, 600, 250, 75);
    question.setBounds(112, 250, 575, 200);timer.setBounds(587, 180, 100,60);
    resign.setBounds(112,180,100, 60);a.setBounds(0, 0, 800, 800);
    question.setEditable(false);

    question.setBackground(Color.black);question.setFont(new Font("sansserif", Font.BOLD, 18));
    optionA.setBackground(Color.black);optionA.setFont(new Font("sansserif", Font.BOLD, 15));
    optionB.setBackground(Color.black);optionB.setFont(new Font("sansserif", Font.BOLD, 15));
    optionC.setBackground(Color.black);optionC.setFont(new Font("sansserif", Font.BOLD, 15));
    optionD.setBackground(Color.black);optionD.setFont(new Font("sansserif", Font.BOLD, 15));

    question.setForeground(Color.white);question.setHorizontalAlignment(SwingConstants.CENTER);
    optionA.setForeground(Color.white);
    optionB.setForeground(Color.white);
    optionC.setForeground(Color.white);
    optionD.setForeground(Color.white);



    add (centerPanel,BorderLayout.CENTER);




    //add(background,BorderLayout.CENTER);
    ////////////////////////////////////////////  END

    end.setBackground(Color.BLUE); end.setForeground(Color.white);
    restart.setBackground(Color.black); restart.setForeground(Color.white);restart.setFont(new Font("sansserif", Font.BOLD, 25));
    exit.setBackground(Color.BLACK); exit.setForeground(Color.white);exit.setFont(new Font("sansserif", Font.BOLD, 25));


    end.add(restart);
    end.add(exit);
    end.add(b);
    end.setLayout(null);

/*  end.setLayout(null);

    end.setBackground(Color.BLUE); end.setForeground(Color.white);
    restart.setBackground(Color.black); restart.setForeground(Color.white);restart.setFont(new Font("sansserif", Font.BOLD, 25));
    exit.setBackground(Color.BLACK); exit.setForeground(Color.white);exit.setFont(new Font("sansserif", Font.BOLD, 25));


    end.add(restart);
    end.add(exit);
    end.add(b);

    restart.setBounds(200,150, 400, 200);
    exit.setBounds(200,450,400,200);
    b.setBounds(0, 0, 800, 800);

    add(end,BorderLayout.CENTER);*/



    restart.setBounds(200,150, 400, 200);
    exit.setBounds(200,450,400,200);
    b.setBounds(0, 0, 800, 800);






    ////////////////////////////////////////////  EAST
    JPanel eastPanel;


    eastPanel= new JPanel();
    eastPanel.setLayout(null);
    eastPanel.setBackground(Color.BLUE);


    /// TEXT FIELDS ///

    Font f=new Font("sansserif", Font.BOLD, 14);



    m12=new JTextField ("1.000.000 TL");m11=new JTextField ("250.000 TL");m10=new JTextField ("125.000 TL");
    m9=new JTextField ("60.000 TL");m8=new JTextField ("30.000 TL");m7=new JTextField ("15.000 TL");
    m6=new JTextField ("7.500 TL");m5=new JTextField ("5.000 TL");m4=new JTextField ("3.000 TL");
    m3=new JTextField ("2.000 TL");m2=new JTextField ("1.000 TL");m1=new JTextField ("500 TL");

    m12.setBackground(Color.BLACK); m11.setBackground(Color.BLACK); m10.setBackground(Color.BLACK);
    m9.setBackground(Color.BLACK);  m8.setBackground(Color.BLACK);  m7.setBackground(Color.BLACK);
    m6.setBackground(Color.BLACK);  m5.setBackground(Color.BLACK);  m4.setBackground(Color.BLACK);
    m3.setBackground(Color.BLACK);  m2.setBackground(Color.BLACK);  m1.setBackground(Color.YELLOW);

    m12.setEditable(false);m11.setEditable(false);m10.setEditable(false);m9.setEditable(false);
    m8.setEditable(false);m7.setEditable(false);m6.setEditable(false);m5.setEditable(false);
    m4.setEditable(false);m3.setEditable(false);m2.setEditable(false);m1.setEditable(false);


    eastPanel.add(m12);eastPanel.add(m11);eastPanel.add(m10);eastPanel.add(m9);eastPanel.add(m8);
    eastPanel.add(m7);eastPanel.add(m6);eastPanel.add(m5);eastPanel.add(m4);
    eastPanel.add(m3);eastPanel.add(m2);eastPanel.add(m1);

    m1.setFont(new Font("sansserif", Font.BOLD, 30));m2.setFont(new Font("sansserif", Font.BOLD, 30));m3.setFont(new Font("sansserif", Font.BOLD, 30));
    m4.setFont(new Font("sansserif", Font.BOLD, 30));m5.setFont(new Font("sansserif", Font.BOLD, 30));m6.setFont(new Font("sansserif", Font.BOLD, 30));
    m7.setFont(new Font("sansserif", Font.BOLD, 30));m8.setFont(new Font("sansserif", Font.BOLD, 30));m9.setFont(new Font("sansserif", Font.BOLD, 30));
    m10.setFont(new Font("sansserif", Font.BOLD, 30));m11.setFont(new Font("sanssserif", Font.BOLD, 30));m12.setFont(new Font("sansserif", Font.BOLD, 35));

    m1.setForeground(Color.WHITE);m4.setForeground(Color.WHITE);m7.setForeground(Color.WHITE);m10.setForeground(Color.WHITE);
    m2.setForeground(Color.WHITE);m5.setForeground(Color.WHITE);m8.setForeground(Color.WHITE);m11.setForeground(Color.WHITE);
    m3.setForeground(Color.WHITE);m6.setForeground(Color.WHITE);m9.setForeground(Color.WHITE);m12.setForeground(Color.WHITE);

    m1.setHorizontalAlignment(SwingConstants.CENTER);m5.setHorizontalAlignment(SwingConstants.CENTER);m9.setHorizontalAlignment(SwingConstants.CENTER);
    m2.setHorizontalAlignment(SwingConstants.CENTER);m6.setHorizontalAlignment(SwingConstants.CENTER);m10.setHorizontalAlignment(SwingConstants.CENTER);
    m3.setHorizontalAlignment(SwingConstants.CENTER);m7.setHorizontalAlignment(SwingConstants.CENTER);m11.setHorizontalAlignment(SwingConstants.CENTER);
    m4.setHorizontalAlignment(SwingConstants.CENTER);m8.setHorizontalAlignment(SwingConstants.CENTER);m12.setHorizontalAlignment(SwingConstants.CENTER);

    m1.setBounds(40,700,320, 55);m2.setBounds(40,635,320, 53);m3.setBounds(40,573,320, 53);m4.setBounds(40,511,320, 53);
    m5.setBounds(40,449,320, 53);m6.setBounds(40,387,320, 53);m7.setBounds(40,325,320, 53);m8.setBounds(40,263,320, 53);
    m9.setBounds(40,201,320, 53);m10.setBounds(40,139,320, 53);m11.setBounds(40,75,320, 53);m12.setBounds(40,10,320, 53);





    eastPanel.setPreferredSize(new Dimension(400,800));
    add (eastPanel,BorderLayout.EAST);


    setVisible(true);
    System.out.println(QText.QFlag);
//  System.out.println(QText.QFlag);
//  System.out.println(QText.QFlag);
//  System.out.println(QText.QFlag);
//  System.out.println(QText.QFlag);
//  System.out.println(QText.QFlag);
    QFlag=QText.QFlag;
    System.out.println("sa"+QFlag);
//  System.out.println(QFlag);
//  System.out.println(QFlag);
//  System.out.println(QFlag);
//  System.out.println(QFlag);
//  System.out.println(QFlag);


}

@Override
public void actionPerformed(ActionEvent e){

boolean cont = false;

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

        try {
            halfJ();
            half.setEnabled(false);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }}
 else if (e.getSource()== change){
        try {
            changeJ();
            change.setEnabled(false);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }}


 else if (e.getSource()==resign){


    System.out.println("neler oluyor"+QFlag);

    if (QFlag <= 10 && (optionA.isSelected())){

        cont=true;  
        soruSayac++;    
        }
    else if (QFlag > 10 && QFlag <=20 && (optionB.isSelected())){
        soruSayac++;
        cont=true;
        }
    else if(QFlag > 20 && QFlag <=30 && optionC.isSelected() ){
        soruSayac++;
        cont=true;
        }
    else if(QFlag > 30 && QFlag <=40 && (optionD.isSelected())){

        soruSayac++;
        cont=true;}
    else{
        cont = false;

    }

if (!(optionA.isSelected()|| optionB.isSelected()|| optionC.isSelected()|| optionD.isSelected ())){

    System.out.println("boşbırakma");
}


//System.out.println(soruSayac);
//System.out.println(cont);

if (cont==true){
    try {
    //  setVisible(false);
    //  System.exit(0);
    //  new Interface2();
    //  soruSayac++;

        JOptionPane.showMessageDialog(null," GREATTT!!! YOUR ANSWER İS TRUE");


        System.out.println(soruSayac);
        System.out.println(cont);
        QText.mechanism();
        QText.setOptions();
        QFlag=QText.QFlag;

        question.setText(QText.QuestionArray[(QFlag)]);
        optionA.setText(QText.AArray[(QFlag)]);
        optionB.setText(QText.BArray[(QFlag)]);
        optionC.setText(QText.CArray[(QFlag)]);
        optionD.setText(QText.DArray[(QFlag)]);

        optionA.setEnabled(true);   
        optionB.setEnabled(true);   
        optionC.setEnabled(true);   
        optionD.setEnabled(true);   

        cont=true;
    } catch (IOException e1) {
        // TODO Auto-generated catch bloc`
        e1.printStackTrace();
    }
    if (soruSayac==12){
        prizeF(soruSayac);
        endScreen();
        cont=false;
        }
}





else if (cont==false){
    prizeF(soruSayac);

    endScreen();




}}


}

0 个答案:

没有答案