0

I am completely new to programming: I have two classes in my Java Swing application, and when I press the button a new window opens, but only the frame is visible and not the content of the frame.
I appreciate any help.

The first class:

public class Learning {

    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;

    public Learning(){
        prepareGUI();
    }

    public static void main(String[] args) {
        Learning swingControlDemo = new Learning();  
        swingControlDemo.showEventDemo();       
    }

    private void prepareGUI() {
        mainFrame = new JFrame("software component architecture quizess");
        mainFrame.setSize(600,400);
        mainFrame.setLayout(new GridLayout(3, 1));

        headerLabel = new JLabel("",JLabel.CENTER );
        statusLabel = new JLabel("",JLabel.CENTER);        

        statusLabel.setSize(350,100);
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent){
                System.exit(0);
            }  
        });
        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        mainFrame.setVisible(true);  
     }

     private void showEventDemo() {
        headerLabel.setText("Learning invironment for CBSC and SOA"); 

        JButton okButton = new JButton("Drag and Drop");
        JButton submitButton = new JButton("multiple choice quizess");
        JButton cancelButton = new JButton("true false quizzes");
        JButton newButton = new JButton("Component Description Language");

        okButton.setActionCommand("Drag and Drop");
        submitButton.setActionCommand("multiple choice quizess");
        cancelButton.setActionCommand("true false quizzes");
        newButton.setActionCommand("true false quizzes");

        controlPanel.add(okButton);
        controlPanel.add(submitButton);
        controlPanel.add(cancelButton);       
        controlPanel.add(newButton);  
        mainFrame.setVisible(true);  

        newButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                mainFrame.dispose();
                new DLC1();
            }
        });
    }
}

And the second class:

public class DLC1 {

   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   private JLabel msglabel;

   public DLC1(){
      prepareGUI();
   }

   public static void main(String[] args){
       DLC1 swingLayoutDemo = new DLC1();  
      swingLayoutDemo.showBorderLayoutDemo();       
   }

   private void prepareGUI(){
      mainFrame = new JFrame("component description language");
      mainFrame.setSize(600,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);        

      statusLabel.setSize(350,100);
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showBorderLayoutDemo(){
      headerLabel.setText("Layout in action: BorderLayout");      

      JPanel panel = new JPanel();
      panel.setBackground(Color.darkGray);
      panel.setSize(300,300);
      BorderLayout layout = new BorderLayout();
      layout.setHgap(10);
      layout.setVgap(10);
      panel.setLayout(layout);        

      panel.add(new JButton("dlgggggggggc"),BorderLayout.CENTER);
      panel.add(new JButton("Line Start"),BorderLayout.LINE_START); 
      panel.add(new JButton("Line End"),BorderLayout.LINE_END);
      panel.add(new JButton("East"),BorderLayout.EAST);   
      panel.add(new JButton("West"),BorderLayout.WEST); 
      panel.add(new JButton("North"),BorderLayout.NORTH); 
      panel.add(new JButton("South"),BorderLayout.SOUTH); 

      controlPanel.add(panel);

      mainFrame.setVisible(true);  
   }
}
Tim
  • 6,406
  • 22
  • 34
rezan
  • 17
  • 4
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jun 29 '16 at 03:02
  • 1
    *"when I press the button"* Which button? For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/) (there should only be a single button in an MCVE/SSCCE). – Andrew Thompson Jun 29 '16 at 03:04

0 Answers0