如何在单击JButton调用/显示其他JFrame时清除JFrame?

时间:2016-02-04 14:35:05

标签: java netbeans jframe

我有这个代码。当我运行它时,它会显示一个JFrame“菜单”,有两个选项。当我点击一个按钮时,它现在在同一窗口中显示菜单和新JFrame。当我点击另一个按钮时,它会添加显示在同一个窗口上的现有内容并添加并添加到内部。我想只使用一个窗口/ JFrame。如何清除过去JFrame?这可能吗?

package finals;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Finals extends JFrame implements ActionListener{
    JLabel In1, In2, choice,menu;
    JTextField In, Inn, choicee, info, menuu, lol,loll;
    JButton PtoS, StoP,stop,ptos;
    public Finals(){

        setTitle("Menu");
        setSize(200,150);

        menu = new JLabel("Select a Program:");

        PtoS = new JButton("Proposition to Sentence");
        StoP = new JButton("Sentence to Proposition");

        Container pane = getContentPane();
        pane.setLayout(new FlowLayout());

        pane.add(menu);
        pane.add(PtoS);
        pane.add(StoP);

        PtoS.addActionListener(this);
        StoP.addActionListener(this);


        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e){

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

            setTitle("Sentence to Proposition");
            setSize(500,500);

            In1 = new JLabel("Enter First Sentence:", SwingConstants.RIGHT);
            choice = new JLabel("Enter Operator:", SwingConstants.RIGHT);
            In2 = new JLabel("Enter Second Sentence", SwingConstants.RIGHT);

            lol = new JTextField("[a]= AND [b]= OR [c]= NOT");
            lol.setEditable(false);
            loll = new JTextField();
            loll.setEditable(false);
            In = new JTextField();
            choicee = new JTextField();
            Inn = new JTextField();

            Container cont = getContentPane();
            cont.setLayout(new GridLayout(5,5));

            cont.add(In1);
            cont.add(In);
            cont.add(lol);
            cont.add(loll);
            cont.add(choice);
            cont.add(choicee);
            cont.add(In2);
            cont.add(Inn);

            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        else if(e.getSource() == PtoS){

            setTitle("Proposition to Sentence");
            setSize(500,500);

            In1 = new JLabel("Enter p:", SwingConstants.RIGHT);
            choice = new JLabel("Enter Operator:", SwingConstants.RIGHT);
            In2 = new JLabel("Enter q:", SwingConstants.RIGHT);

            lol = new JTextField("[a]= AND [b]= OR [c]= NOT");
            lol.setEditable(false);
            loll = new JTextField();
            loll.setEditable(false);
            In = new JTextField();
            choicee = new JTextField();
            Inn = new JTextField();   

            Container cont = getContentPane();
            cont.setLayout(new GridLayout(5,2));

            cont.add(In1);
            cont.add(In);
            cont.add(lol);
            cont.add(loll);
            cont.add(choice);
            cont.add(choicee);
            cont.add(In2);
            cont.add(Inn);

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

    public static void main(String[] args){
        Finals X = new Finals();
    }
}

0 个答案:

没有答案