Öffnen Sie eine neue JFrame mit JButton Klicken Sie auf den Java - Swing

Ich versuche, öffnen Sie ein neues JFrame-Fenster mit einem button-click-Ereignis. Es gibt viel info auf dieser Website, aber nichts, dass mir hilft, weil ich denke, es ist nicht so viel code habe ich, aber der Auftrag ausgeführt wird (allerdings bin ich unsicher).

Dies ist der code für den Rahmen, halten Sie die Taste, die ich initiieren möchten die Veranstaltung:

package messing with swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.EmptyBorder;


public class ReportGUI extends JFrame{
    //Fields
    private JButton viewAllReports = new JButton("View All Program Details");
    private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course"); 
    private JButton viewTaughtCourses = new JButton("View Courses this Examiner Teaches"); 
    private JLabel courseLabel = new JLabel("Select a Course: ");
    private JLabel examinerLabel = new JLabel("Select an Examiner: "); 
    private JPanel panel = new JPanel(new GridLayout(6,2,4,4));  
    private ArrayList<String> list = new ArrayList<String>(); 
    private ArrayList<String> courseList = new ArrayList<String>();  


     public ReportGUI(){   
           reportInterface();
           allReportsBtn();     
           examinnerFileRead();
           courseFileRead();
           comboBoxes();
     }        

     private void examinnerFileRead(){
         try{
             Scanner scan = new Scanner(new File("Examiner.txt"));

             while(scan.hasNextLine()){
                 list.add(scan.nextLine());
             }
             scan.close();
         }
         catch (FileNotFoundException e){
             e.printStackTrace();
         }
     }
      private void courseFileRead(){
         try{
             Scanner scan = new Scanner(new File("Course.txt"));

             while(scan.hasNextLine()){
                 courseList.add(scan.nextLine());
             }
             scan.close();
         }
         catch (FileNotFoundException e){
             e.printStackTrace();
         }
     }

    private void reportInterface(){         
          setTitle("Choose Report Specifications");                   
          setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
          JPanel panel = new JPanel(new FlowLayout());        
          add(panel, BorderLayout.CENTER);
          setSize(650,200);
          setVisible(true);    
          setResizable(false);
          setLocationRelativeTo(null);
}    
    private void allReportsBtn(){
        JPanel panel = new JPanel(new GridLayout(1,1)); 
        panel.setBorder(new EmptyBorder(70, 50, 70, 25));
        panel.add(viewAllReports);        
        viewAllReports.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                JFrame AllDataGUI = new JFrame();
                new AllDataGUI();
            }
        });         
        add(panel, BorderLayout.LINE_END);
    }       
    private void comboBoxes(){                
        panel.setBorder(new EmptyBorder(0, 5, 5, 10));
        String[] comboBox1Array = list.toArray(new String[list.size()]);
        JComboBox comboBox1 = new JComboBox(comboBox1Array);          
        panel.add(examinerLabel);
        panel.add(comboBox1);          
        panel.add(viewTaughtCourses);
         viewTaughtCourses.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {                 
                JFrame ViewCourseGUI = new JFrame();
                new ViewCourseGUI();
            }
        });  
         String[] comboBox2Array = courseList.toArray(new String[courseList.size()]);
         JComboBox comboBox2 = new JComboBox(comboBox2Array);
         panel.add(courseLabel);         
         panel.add(comboBox2); 
         panel.add(viewPrograms);
         add(panel, BorderLayout.LINE_START); 

    } 

Wenn Sie nicht wollen, Tauchen Sie ein in den obigen code die button-ActionListener ist hier:

 panel.add(viewTaughtCourses);
             viewTaughtCourses.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {                 
                    JFrame ViewCourseGUI = new JFrame();
                    new ViewCourseGUI();
                }
            });  

Dies ist der code in der Klasse halten des JFrame ich das öffnen will:

package messing with swing;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;


public class ViewCourseGUI extends JFrame{ 
    private JButton saveCloseBtn = new JButton("Save Changes and Close");
    private JButton closeButton = new JButton("Exit Without Saving");
    private JFrame frame=new JFrame("Courses taught by this examiner");
    private JTextArea textArea = new JTextArea();



     public void ViewCoursesGUI(){
         panels();
     }        

    private void panels(){        
          JPanel panel = new JPanel(new GridLayout(1,1));
          panel.setBorder(new EmptyBorder(5, 5, 5, 5));
          JPanel rightPanel = new JPanel(new GridLayout(15,0,10,10));
          rightPanel.setBorder(new EmptyBorder(15, 5, 5, 10));
          JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
          panel.add(scrollBarForTextArea); 
          frame.add(panel);
          frame.getContentPane().add(rightPanel,BorderLayout.EAST);
          rightPanel.add(saveCloseBtn);
          rightPanel.add(closeButton);

           frame.setSize(1000, 700);
           frame.setVisible(true);   
           frame.setLocationRelativeTo(null);

}
}

Könnte jemand bitte zeigen Sie mich in die richtige Richtung?

Ich weiß nicht, warum Sie den viewCourseGUI gleich ein neues JFrame. Tun Sie dies: JFrame ViewCourseGUI = new ViewCourseGUI();
Ich gebe es zu gehen. Ich bin mir nicht sicher, entweder, noch zu lernen, diese kleinen Nuancen.
public void ViewCoursesGUI() ist nicht ein Konstruktor wird nicht aufgerufen werden, wenn Sie mit new Schlüsselwort. Entfernen void. Konstruktoren haben kann alle deklarierten Rückgabetyp, auch void.
Kombinieren Sie das mit der Tatsache, dass ich als es ViewCoursesGUI() anstelle von ViewCourseGUI()... Und jetzt funktioniert es. Vielen Dank.
Siehe Die Verwendung von Mehreren JFrames, Gute/Schlechte Praxis?

InformationsquelleAutor Splunk | 2014-06-06

Schreibe einen Kommentar