java - 使用 JButton Click 打开新的 JFrame - Java Swing

标签 java swing jframe

我正在尝试使用按钮单击事件打开一个新的 JFrame 窗口。这个网站上有很多信息,但对我没有任何帮助,因为我认为与其说是我拥有的代码,不如说是它的执行顺序(但我不确定)。

这是包含我要启动事件的按钮的框架的代码:

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

    } 

如果不想深究上面的代码,按钮ActionListener在这里:

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

这是包含我要打开的 JFrame 的类中的代码:

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

}
}

有人能给我指出正确的方向吗?

最佳答案

正如 PM 77-3 所指出的

我有:

 public void ViewCoursesGUI(){
     panels();
 }        

当我应该有:

public ViewCourseGUI(){
     panels();
 }        

语法和拼写错误的组合。

关于java - 使用 JButton Click 打开新的 JFrame - Java Swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24090637/

相关文章:

java - 当父类(super class)扩展 JFrame 时

java - 在 Android Studio 中调用 MediaRecorder 会使应用程序崩溃

java - 似乎无法让 JOptionPane 下拉菜单正常工作

java - 如何将 JTextField 与 GridLayout 的其余部分分开

java - JavaFX 中的 SecondaryLoop,如 Swing?

java - 无法引用 Action 监听器 (Jframe) 内的非变量

java - 使用 Prowide-Core 生成人类可读的 SWIFT 消息?

java - 无法创建 Web 应用程序存档 : No such file or directory

java - 在生成事件的源元素的父元素中居中 JoptionPaneMessageDialog

Java JDialogs之间如何传递信息?