用动作监听器打开另一个类

时间:2017-08-07 08:18:29

标签: java eclipse class actionlistener

您好我正在处理一个程序,但我需要只需单击菜单选项卡就可以更改类(以便更改框架)。

我希望有人修改此内容以获取我的class2.java

JMenu area = new JMenu ("Area");
    menu.add(area);
    JMenuItem convertA= new JMenuItem ("Convertions");
    area.add(convertA);

    class aaction implements ActionListener{
        public void actionPerformed (ActionEvent e) {


        }
    }

class 1.java

/*import needed packages*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.AbstractAction;
import javax.swing.SwingConstants;
import javax.swing.Action;
import javax.swing.InputVerifier;
import javax.swing.*;
import javax.xml.soap.Text;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Frame;
import java.math.*;
import java.lang.*;
import java.text.*;
import java.awt.EventQueue;
import java.awt.SystemColor;
import java.awt.Window;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
@SuppressWarnings("unused")
public class circle_ac {

private JFrame frame;
private JTextField txtRadius;
private JTextField txtTheAreaOf;
private JTextField txtTheCircumfrenceOf;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                circle_ac window = new circle_ac();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public circle_ac() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 * @param arg0 
 * @param arg0 
 * @param arg0 
 */
private void initialize() {
    /*setup the JFrame*/
    frame = new JFrame();
    frame.setResizable(true);
    frame.setBounds(1000, 1000, 2250, 1000);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());        
    frame.add(BorderLayout.CENTER,new JLabel("MHMB 1.3.5"));

    /*setup JMenu*/
    Font f = new Font("sans-serif", Font.PLAIN, 25);
    UIManager.put("Menu.font", f);
    UIManager.put("MenuItem.font", f);

    JMenuBar menu = new JMenuBar ();
    frame.setJMenuBar(menu);

    JMenu circles = new JMenu ("Circles");
    menu.add(circles);
    JMenuItem ac= new JMenuItem("Area And Circumference");
    circles.add(ac);

    JMenu measurements = new JMenu ("Measurements");
    menu.add(measurements);
    JMenuItem convert= new JMenuItem ("Convertions");
    measurements.add(convert);

    JMenu area = new JMenu ("Area");
    menu.add(area);
    JMenuItem convertA= new JMenuItem ("Convertions");
    area.add(convertA);

    class aaction implements ActionListener{
        public void actionPerformed (ActionEvent e) {
            contentPane.removeAll();
            contentPane.setLayout(new BorderLayout()); 
            contentPane.add(BorderLayout.CENTER,frame1);
            this.revalidate();
            this.repaint();
        }
    }

    JMenu close = new JMenu ("Close");
    menu.add(close);
    JMenuItem exit = new JMenuItem ("Exit");
    close.add(exit);

    class eaction implements ActionListener{
        public void actionPerformed (ActionEvent e) {
            System.exit(0);
        }
    }
    exit.addActionListener(new eaction());

    /*setup calculate button*/
    JButton btnCalculate = new JButton("calculate");
    btnCalculate.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

    /*setup calculations*/
            double rad;
            double ans1;
            double ans2;
            double pi=3.1415926535897932384626433832795;
            try {
                rad=Double.parseDouble(txtRadius.getText()) ;
                ans1= pi*(rad*rad);
                ans2= 2*pi*rad;
                DecimalFormat df = new DecimalFormat("#");
                df.setMaximumFractionDigits(25);

                txtTheAreaOf.setText("The Area Is "+df.format(ans1));

                txtTheCircumfrenceOf.setText("The Circumference Is "+df.format(ans2) );

    /*setup error message*/
              }catch(Exception e){
                  JOptionPane.showMessageDialog(null, "Please Enter Valid Radius");
              }
            }
    });

    /*initialize calculate button*/
    btnCalculate.setFont(new Font("Tahoma", Font.PLAIN, 60));
    btnCalculate.setBackground(new Color(227, 227, 227));
    btnCalculate.setForeground(Color.BLACK);
    frame.getContentPane().add(btnCalculate, BorderLayout.SOUTH);

    /*setup Radius text box*/
    txtRadius = new JTextField();
    txtRadius.setFont(new Font("Tahoma", Font.PLAIN, 55));
    txtRadius.setCursor(null);
    txtRadius.setInputVerifier(null);
    txtRadius.setText(" Enter The Radius");
    txtRadius.setBackground(SystemColor.controlHighlight);
    frame.getContentPane().add(txtRadius, BorderLayout.CENTER);
    txtRadius.setColumns(10);

    /*setup Area text box*/
    txtTheAreaOf = new JTextField();
    txtTheAreaOf.setEditable(false);
    txtTheAreaOf.setText("The Area Of The Circle Is                                                          ");
    txtTheAreaOf.setBackground(SystemColor.control);
    txtTheAreaOf.setFont(new Font("Tahoma", Font.PLAIN, 30));
    frame.getContentPane().add(txtTheAreaOf, BorderLayout.WEST);

    /*setup Circumference text box*/
    txtTheCircumfrenceOf =new JTextField();
    txtTheAreaOf.setEditable(false);
    txtTheCircumfrenceOf.setFont(new Font("Tahoma", Font.PLAIN, 30));
    txtTheCircumfrenceOf.setText("The Circumfrence Of The Circle Is                                                  ");
    txtTheCircumfrenceOf.setBackground(SystemColor.control);
    frame.getContentPane().add((Component) txtTheCircumfrenceOf, BorderLayout.EAST);
}
}

class2.java

/*import needed packages*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import javax.swing.AbstractAction;
import javax.swing.SwingConstants;
import javax.swing.Action;
import javax.swing.InputVerifier;
import javax.swing.*;
import javax.xml.soap.Text;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Frame;
import java.math.*;
import java.lang.*;
import java.text.*;
import java.awt.EventQueue;
import java.awt.SystemColor;
import java.awt.Window;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
@SuppressWarnings("unused")
public class area_c {

private JFrame frame1;
private JTextField txtRadius;
private JTextField txtTheAreaOf;
private JTextField txtTheCircumfrenceOf;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                area_c window = new area_c();
                window.frame1.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public area_c() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 * @param arg0 
 * @param arg0 
 * @param arg0 
 */
private void initialize() {
    /*setup the JFrame*/
    frame1 = new JFrame();
    frame1.setResizable(true);
    frame1.setBounds(1000, 1000, 2250, 1000);
    frame1.pack();
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setLayout(new BorderLayout());        
    frame1.add(BorderLayout.CENTER,new JLabel("MHMB 1.3.5"));

    /*setup JMenu*/
    Font f = new Font("sans-serif", Font.PLAIN, 25);
    UIManager.put("Menu.font", f);
    UIManager.put("MenuItem.font", f);

    JMenuBar menu = new JMenuBar ();
    frame1.setJMenuBar(menu);

    JMenu circles = new JMenu ("Circles");
    menu.add(circles);
    JMenuItem ac= new JMenuItem("Area And Circumference");
    circles.add(ac);

    JMenu measurements = new JMenu ("Measurements");
    menu.add(measurements);
    JMenuItem convert= new JMenuItem ("Convertions");
    measurements.add(convert);

    JMenu area = new JMenu ("Area");
    menu.add(area);
    JMenuItem convertA= new JMenuItem ("Convertions");
    area.add(convertA);

    class aaction implements ActionListener{
        public void actionPerformed (ActionEvent e) {

        }
    }

    JMenu close = new JMenu ("Close");
    menu.add(close);
    JMenuItem exit = new JMenuItem ("Exit");
    close.add(exit);

    class eaction implements ActionListener{
        public void actionPerformed (ActionEvent e) {
            System.exit(0);
        }
    }
    exit.addActionListener(new eaction());

System.out.println("Hello World!");

我需要菜单选项卡,使用动作侦听器将其从class1.java更改为class2.java。 这是可能的,如果是这样,我该怎么做。

2 个答案:

答案 0 :(得分:0)

在大多数IDE中,右键单击设计(GUI)窗格中的按钮时,您可以浏览:

活动 - >行动 - >的actionPerformed()。

当你在一个框架中时,你可以将它的可见性设置为隐藏它,如下所示:

this.setVisible(false);

您可以创建另一个类的对象(您要重定向的另一个框架的对象)并设置它的可见性以显示它,如下所示:

area_c of = new area_c();
of.setVisible(true);

因此,您将从一个类转换到另一个不同的帧。

您可以使用JMenuItem对象的对象实现相同的目的:

        class1 = new JPanel();
        class1.setLayout(new BorderLayout());        
        class1.add(BorderLayout.CENTER,new JLabel("Label_Name"));                  
        class2 = new JPanel();
        class2.setLayout(new BorderLayout());        
        class2.add(BorderLayout.CENTER,new JLabel("Label_Name"));
        JMenuItem JMenuItemObject1 =new JMenuItem("Class 1");
        JMenuItem JMenuItemObject2 =new JMenuItem("Class 2");


        JMenuItemObject1.addActionListener(new ActionListener() {            
        public void actionPerformed(ActionEvent e) {
            contentPane.removeAll();
            contentPane.setLayout(new BorderLayout()); 
            contentPane.add(BorderLayout.CENTER,panel1);
            this.revalidate();
            this.repaint();
        }
        });
        JMenuItemObject2.addActionListener(new ActionListener() {            
        public void actionPerformed(ActionEvent e) {
            contentPane.removeAll();
            contentPane.setLayout(new BorderLayout()); 
            contentPane.add(BorderLayout.CENTER,panel2);
            this.revalidate();
            this.repaint();
        }
        });

        area.add(JMenuItemObject1);        
        area.add(JMenuItemObject2);        
        menubar.add(area);        

答案 1 :(得分:0)

我建议将两个类实现为JPanels。然后,您可以将这些JPanel添加到您的JFrame。看看这个小例子,它以简短快捷的方式显示原理。希望它有所帮助:

package test;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class TestFrame extends JFrame {

    private Container contentPane;
    private JPanel panel1,panel2;
    public TestFrame() {
        super("Test");
        this.setJMenuBar(menubar());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = this.getContentPane();   
        createPanels();
        contentPane.setLayout(new BorderLayout());
        this.add(BorderLayout.CENTER,panel1);
        this.pack();
        this.setVisible(true);

    }

    private JMenuBar menubar() {
        JMenuBar menubar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem m1 =new JMenuItem("Selection 1");
        JMenuItem m2 =new JMenuItem("Selection 2");

        m1.addActionListener(new ActionListener() {            
            public void actionPerformed(ActionEvent e) {
                contentPane.removeAll();
                contentPane.setLayout(new BorderLayout()); 
                contentPane.add(BorderLayout.CENTER,panel1);
                refresh();
            }
        });

        m2.addActionListener(new ActionListener() {            
            public void actionPerformed(ActionEvent e) {
                contentPane.removeAll();
                contentPane.setLayout(new BorderLayout()); 
                contentPane.add(BorderLayout.CENTER,panel2);
                refresh();
            }
        });
        fileMenu.add(m1);        
        fileMenu.add(m2);
        menubar.add(fileMenu);        

        return menubar;
    }

    private void refresh() {
        this.revalidate();
        this.repaint();        
    }

    private void createPanels() {
        panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());        
        panel1.add(BorderLayout.CENTER,new JLabel("Hello     World"));                  
        panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());        
        panel2.add(BorderLayout.CENTER,new JLabel("Goodbye World"));        

    }

    public static void main(String[] args) {
        TestFrame t = new TestFrame();
    }
}