简体   繁体   中英

Can't update the old JPane to Panel

The main purpose of my small app is to update the Panel, which is in the only one Frame, by pressing 'Enter' button pressed on keyboard.

Here are the classes: Main - starts the app; MainFrame - is the Frame with methods to update it; PanelWithNoThreads - is the class of Panel, which is created while init of the Frame take place; PanelWithNoFramesContr - is controlling everything what happens with PanelWithNoThreads . PanelWithThread and PanelWithThreadContr - is the screen and it's controller, quite the same things as the previous pare is.

I'll put the code in the end of this post.

SO, THE PROBlEM IS...

  1. When we start the app, everything works, and PanelWithNoThreads appears.
  2. I press ENTER and after that my Frame shows me white screen...

So, how to solve this problem.

class Main

    public class Main {
        static MainFrame mainFrame;
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            mainFrame = new MainFrame();
        }  
    }

class MainFrame:
public class MainFrame extends JFrame implements KeyListener{

    JPanel theMainPanel = new JPanel();
    PanelWithThreadContr pwntContr;
    static String idOfPanel = "empty_panel";


    public MainFrame(){
        setSize(800,600);
        setExtendedState(MAXIMIZED_BOTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        theMainPanel.setBackground(Color.BLACK);
        theMainPanel.setForeground(Color.WHITE);
        add(theMainPanel);
        System.out.println("LOG/MainFrame : added the empty Panel");
        //default change to the Panel with no Threads
        PanelWithNoFramesContr ntpc = new PanelWithNoFramesContr();
        changeThePane(ntpc.pwnt, ntpc.getId(), ntpc);
    }

    private void changeThePane(JPanel inputPanel, String id, KeyListener keyListener){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                remove(theMainPanel);

                theMainPanel = inputPanel;
                add(theMainPanel);
                addKeyListener(keyListener);
                repaint();
                theMainPanel.validate();
                theMainPanel.repaint();
                theMainPanel.setVisible(true);
            }
        });


    }

    public void implementThePane(String id){
        if(id.contains("one_thread")){
            pwntContr = new PanelWithThreadContr();
            changeThePane(pwntContr.pwt, pwntContr.id, pwntContr);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }
}

class PanelWithNoThreads:

public class PanelWithNoThreads extends javax.swing.JPanel {

    /**
     * Creates new form PanelWithNoThreads
     */
    public PanelWithNoThreads() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        HelloLabel = new javax.swing.JLabel();

        HelloLabel.setFont(new java.awt.Font("Osaka", 0, 48)); // NOI18N
        HelloLabel.setText("Hello!!!");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(HelloLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(HelloLabel)
                .addContainerGap(164, Short.MAX_VALUE))
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JLabel HelloLabel;
    // End of variables declaration                   
    public void setColorsToHelloLabel(Color backColor, Color foreColor){
        HelloLabel.setBackground(backColor);
        HelloLabel.setForeground(foreColor);
    }
}

class PanelWithNoThreadsContr:

public class PanelWithNoThreadsContr extends Controller implements KeyListener{

    PanelWithNoThreads pwnt = new PanelWithNoThreads();
    private String id = "no_threads";

    public PanelWithNoThreadsContr() {
        //settings
        pwnt.setBackground(Color.BLACK);
        pwnt.setForeground(Color.WHITE);
        pwnt.setColorsToHelloLabel(Color.BLACK, Color.GREEN);

    }



    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("LOG/no_thread : Pressed 'ENTER'");
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            Main.mainFrame.implementThePane("one_thread");
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }   
}

class PanelWithThread:

public class PanelWithThread extends javax.swing.JPanel {

    /**
     * Creates new form PanelWithThread
     */
    public PanelWithThread() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        threadedLabel = new javax.swing.JLabel();

        threadedLabel.setFont(new java.awt.Font("ArcadeClassic", 0, 36)); // NOI18N
        threadedLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        threadedLabel.setText("NO_TEXT");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(threadedLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(threadedLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    public javax.swing.JLabel threadedLabel;
    // End of variables declaration                   
}

class PanelWithThreadContr:

public class PanelWithThreadContr extends Controller implements KeyListener{

    PanelWithThread pwt = new PanelWithThread();
    String id = "one_thread";

    public PanelWithThreadContr() {
        System.out.println("LOG/one_thread : creating");
        pwt.setBackground(Color.BLACK);
        pwt.setForeground(Color.WHITE);
        pwt.threadedLabel.setText("*****");
        pwt.threadedLabel.setForeground(Color.GREEN);
        pwt.threadedLabel.setBackground(Color.BLACK);
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){

        }
    }

}

您需要在每个自定义JPanel的构造函数中调用super()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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