将主类和主类中的其他函数分离为单独的类文件

时间:2015-03-05 15:50:16

标签: java class

我只有一个'mainclass'类,在类中编写了其他函数,因为代码增加了,现在很难对我做任何修改。所以我希望将一个mainClass分开并将不同类中的其余函数分开并在mainClass中调用它们,这样任何更改/更新,我只能更改将反映在mainClass中的子类。自从我新手!请告诉我如何拆分成子类并在主类中调用那些子类。

public class mainClass extends JFrame{

    private JFrame frmScms;
    private JTabbedPane tabbedPane;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    mainClass window = new mainClass();
                    window.frmScms.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public mainClass() {
        initialize();
    }

    private void initialize() {
        frmScms = new JFrame();
        frmScms.setTitle("SCMS");
        frmScms.setBounds(100, 100, 1080, 684);
        frmScms.setExtendedState(MAXIMIZED_BOTH);
        frmScms.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmScms.getContentPane().setLayout(null);
        frmScms.setResizable(true);

        //Set the frame icon to an image loaded from a file.
        frmScms.setIconImage(new ImageIcon("images/company-logo.gif").getImage());

        //creating pages
        createPageScreen1();
        createPageScreen2();
        createPageScreen3();

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(2, 20, 1900, 1080);

        //tool tip for screen-1 to 6
        String tooltip1 = "Screen-1";
        String tooltip2 = "Screen-2";
        String tooltip3 = "Screen-3";


        // Create a tabbed pane
        tabbedPane.addTab("Screen-1", null, panel1, tooltip1);
        tabbedPane.addTab("Screen-2", null, panel2, tooltip2);
        tabbedPane.addTab("Screen-3", null, panel3, tooltip3);
        frmScms.getContentPane().add(tabbedPane);

        //warning message before close using X 
        frmScms.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frmScms.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                JFrame frame = (JFrame)e.getSource();
                int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });
    }

    //function for panel - page - 1
    public void createPageScreen1() {
        panel1 = new JPanel();
        panel1.setLayout( new BorderLayout() );


        ImageIcon image = new ImageIcon("images/Front1.png");
        JLabel label2 = new JLabel(image, JLabel.CENTER);
        label2.setBounds( 10, 55, 1600, 900 );
        panel1.add(label2);
    }

    //function for panel - page - 2
    public void createPageScreen2() {
        panel2 = new JPanel();
        panel2.setLayout( null );

        JLabel label1 = new JLabel( "LOG:" );
        label1.setBounds( 10, 15, 150, 20 );
        panel2.add( label1 );

        JLabel label2 = new JLabel( "Well 1 PMW Pressure Sensor - Tag xx123" );
        label2.setBounds( 10, 45, 300, 20 );
        panel2.add( label2 );
    }

    //function for panel - page - 3
    public void createPageScreen3() {
        panel3 = new JPanel();
        panel3.setLayout( null );

        JLabel label1 = new JLabel( "GENERAL SETTINGS:" );
        label1.setBounds( 10, 15, 150, 20 );
        panel3.add( label1 );

        JLabel label2 = new JLabel( "Tag Name:" );
        label2.setBounds( 10, 45, 300, 20 );
        panel3.add( label2 );

        JTextField jtext1 = new JTextField( "Tag xx123" );
        jtext1.setBounds(95, 45, 100, 20);
        panel3.add( jtext1 );

        JLabel label3 = new JLabel( "Description:" );
        label3.setBounds( 10, 65, 300, 20 );
        panel3.add( label3 );

        JTextField jtext2 = new JTextField( "Tag1" );
        jtext2.setBounds(95, 65, 100, 20);
        panel3.add( jtext2 );
    }
}
  1. 主类应该调用JTabbedPane(作为单独的)类;
  2. JTabbedPane类应该调用createPageScreen1(作为单独的)类;
  3. JTabbedPane类应该调用createPageScreen2(作为单独的)类;
  4. JTabbedPane类应该调用createPageScreen3(作为单独的)类;
  5. 请告诉我方向,谢谢!

2 个答案:

答案 0 :(得分:1)

您只需要封装由自定义类创建面板的方法,这些类可以扩展一些Swing组件,例如: JPanel

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import static java.awt.Frame.MAXIMIZED_BOTH;

public class MainClass extends JFrame{

    private JFrame frmScms;
    private JTabbedPane tabbedPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainClass window = new MainClass();
                    window.frmScms.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MainClass() {
        initialize();
    }

    private void initialize() {
        frmScms = new JFrame();
        frmScms.setTitle("SCMS");
        frmScms.setBounds(100, 100, 1080, 684);
        frmScms.setExtendedState(MAXIMIZED_BOTH);
        frmScms.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmScms.getContentPane().setLayout(null);
        frmScms.setResizable(true);

        //Set the frame icon to an image loaded from a file.
        frmScms.setIconImage(new ImageIcon("images/company-logo.gif").getImage());

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(2, 20, 1900, 1080);

        //tool tip for screen-1 to 6
        String tooltip1 = "Screen-1";
        String tooltip2 = "Screen-2";
        String tooltip3 = "Screen-3";


        // Create a tabbed pane
        tabbedPane.addTab("Screen-1", null, new PageScreen1(), tooltip1);
        tabbedPane.addTab("Screen-2", null, new PageScreen2(), tooltip2);
        tabbedPane.addTab("Screen-3", null, new PageScreen3(), tooltip3);
        frmScms.getContentPane().add(tabbedPane);

        //warning message before close using X 
        frmScms.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frmScms.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                JFrame frame = (JFrame)e.getSource();
                int result = JOptionPane.showConfirmDialog(frame, "Are you sure you want to close the application?", "Please Confirm",JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });
    }
}

class PageScreen1 extends JPanel {
    private PageScreen1() {
        setLayout( new BorderLayout() );

        ImageIcon image = new ImageIcon("images/Front1.png");
        JLabel label2 = new JLabel(image, JLabel.CENTER);
        label2.setBounds( 10, 55, 1600, 900 );
        add(label2);
    }
}

class PageScreen2 extends JPanel {
    private PageScreen2() {
        setLayout( null );
        JLabel label1 = new JLabel( "LOG:" );
        label1.setBounds( 10, 15, 150, 20 );
        add( label1 );

        JLabel label2 = new JLabel( "Well 1 PMW Pressure Sensor - Tag xx123" );
        label2.setBounds( 10, 45, 300, 20 );
        add( label2 );
    }
}

class PageScreen3 extends JPanel {

    private PageScreen3() {
        setLayout( null );
        JLabel label1 = new JLabel( "GENERAL SETTINGS:" );
        label1.setBounds( 10, 15, 150, 20 );
        add( label1 );

        JLabel label2 = new JLabel( "Tag Name:" );
        label2.setBounds( 10, 45, 300, 20 );
        add( label2 );

        JTextField jtext1 = new JTextField( "Tag xx123" );
        jtext1.setBounds(95, 45, 100, 20);
        add( jtext1 );

        JLabel label3 = new JLabel( "Description:" );
        label3.setBounds( 10, 65, 300, 20 );
        add( label3 );

        JTextField jtext2 = new JTextField( "Tag1" );
        jtext2.setBounds(95, 65, 100, 20);
        add( jtext2 );   
    }
}

答案 1 :(得分:1)

这是将代码分解为类的一种方法。您移动代码以将JTabbedPane的每个JPanel创建到其自己的类中。

MainClass:

package com.ggl.split;

import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class MainClass {

    private JFrame frmScms;

    private JTabbedPane tabbedPane;

    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainClass window = new MainClass();
                    window.frmScms.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MainClass() {
        initialize();
    }

    private void initialize() {
        frmScms = new JFrame();
        frmScms.setTitle("SCMS");
        frmScms.setBounds(100, 100, 1080, 684);
        frmScms.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frmScms.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmScms.getContentPane().setLayout(null);
        frmScms.setResizable(true);

        // Set the frame icon to an image loaded from a file.
        frmScms.setIconImage(new ImageIcon("images/company-logo.gif")
                .getImage());

        // creating pages
        panel1 = new Page1().getPanel1();
        panel2 = new Page2().getPanel2();
        panel3 = new Page3().getPanel3();

        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBounds(2, 20, 1900, 1080);

        // tool tip for screen-1 to 6
        String tooltip1 = "Screen-1";
        String tooltip2 = "Screen-2";
        String tooltip3 = "Screen-3";

        // Create a tabbed pane
        tabbedPane.addTab("Screen-1", null, panel1, tooltip1);
        tabbedPane.addTab("Screen-2", null, panel2, tooltip2);
        tabbedPane.addTab("Screen-3", null, panel3, tooltip3);
        frmScms.getContentPane().add(tabbedPane);

        // warning message before close using X
        frmScms.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frmScms.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                JFrame frame = (JFrame) e.getSource();
                int result = JOptionPane.showConfirmDialog(frame,
                        "Are you sure you want to close the application?",
                        "Please Confirm", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION)
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        });
    }

}

Page1类:

package com.ggl.split;

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Page1 {

    private JPanel panel1;

    public Page1() {
        createPageScreen1();
    }

    // function for panel - page - 1
    private void createPageScreen1() {
        panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());

        ImageIcon image = new ImageIcon("images/Front1.png");
        JLabel label2 = new JLabel(image, JLabel.CENTER);
        label2.setBounds(10, 55, 1600, 900);
        panel1.add(label2);
    }

    public JPanel getPanel1() {
        return panel1;
    }

}

Page2课程:

package com.ggl.split;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class Page2 {

    private JPanel panel2;

    public Page2() {
        createPageScreen2();
    }

    // function for panel - page - 2
    private void createPageScreen2() {
        panel2 = new JPanel();
        panel2.setLayout(null);

        JLabel label1 = new JLabel("LOG:");
        label1.setBounds(10, 15, 150, 20);
        panel2.add(label1);

        JLabel label2 = new JLabel("Well 1 PMW Pressure Sensor - Tag xx123");
        label2.setBounds(10, 45, 300, 20);
        panel2.add(label2);
    }

    public JPanel getPanel2() {
        return panel2;
    }

}

Page3类:

package com.ggl.split;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Page3 {

    private JPanel panel3;

    public Page3() {
        createPageScreen3();
    }

    // function for panel - page - 3
    private void createPageScreen3() {
        panel3 = new JPanel();
        panel3.setLayout(null);

        JLabel label1 = new JLabel("GENERAL SETTINGS:");
        label1.setBounds(10, 15, 150, 20);
        panel3.add(label1);

        JLabel label2 = new JLabel("Tag Name:");
        label2.setBounds(10, 45, 300, 20);
        panel3.add(label2);

        JTextField jtext1 = new JTextField("Tag xx123");
        jtext1.setBounds(95, 45, 100, 20);
        panel3.add(jtext1);

        JLabel label3 = new JLabel("Description:");
        label3.setBounds(10, 65, 300, 20);
        panel3.add(label3);

        JTextField jtext2 = new JTextField("Tag1");
        jtext2.setBounds(95, 65, 100, 20);
        panel3.add(jtext2);
    }

    public JPanel getPanel3() {
        return panel3;
    }

}