在秋千中打开新窗口 - 内容不可见

时间:2016-06-29 02:56:56

标签: java swing

我对编程完全不熟悉:我的Java Swing应用程序中有两个类,当我按下按钮时会打开一个新窗口,但只有框架可见而不是框架的内容。
我感谢任何帮助。

第一堂课:

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();
            }
        });
    }
}

第二节课:

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);  
   }
}

0 个答案:

没有答案