将数据从一个JPanel传输到另一个JPanel

时间:2016-12-06 14:50:58

标签: java swing

这是我在这个网站上的第一个问题,所以请原谅我任何粗鲁和破碎的英语等。我很好奇地问,是否有可能将数据从一个JPanel传输到另一个JPanel。我目前正在做一个拥有个人资料页面和个人资料编辑页面的swing java项目。我想尝试创建一个方法来在配置文件编辑页面的文本字段中获取文本,然后返回textfield.getText();.然后我将通过从配置文件页面调用确切的方法并将其设置为JLabel来跟随。但是,我不确定如何做到这一点...... 我更新了以下代码。感谢您提前的帮助! 这是个人资料编辑页面:

package Project;
import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProfileEdit extends MasterPanel {
    private JTextField txtbxFullName;
    private JTextField txtAdmissionNo;
    private JTextField txtbxContactNo;
    private JTextField txtFieldDiploma;

    /**
     * Create the panel.
     */
    public ProfileEdit(JFrame mf) {
        super(mf);

        JLabel lblUserProfile = new JLabel("");
        lblUserProfile.setBorder(BorderFactory.createTitledBorder(null,"User Profile",0,0, new Font("Tahoma", Font.PLAIN, 15), Color.WHITE)); 
        lblUserProfile.setForeground(Color.WHITE);
        lblUserProfile.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblUserProfile.setBounds(60, 103, 514, 297);
        add(lblUserProfile);

        JLabel lblDisplayPic = new JLabel("");
        lblDisplayPic.setBorder(new LineBorder(Color.WHITE));
        lblDisplayPic.setIcon(new ImageIcon(ProfileEdit.class.getResource("/javaProject/images/default-avatar.png")));
        lblDisplayPic.setBounds(100, 132, 90, 100);
        add(lblDisplayPic);

        JLabel lblFullName = new JLabel("Full Name:");
        lblFullName.setForeground(Color.WHITE);
        lblFullName.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblFullName.setBounds(235, 132, 76, 14);
        add(lblFullName);



        JLabel lblGender = new JLabel("Gender: ");
        lblGender.setForeground(Color.WHITE);
        lblGender.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblGender.setBounds(235, 168, 60, 14);
        add(lblGender);

        JRadioButton rdbtnMale = new JRadioButton("Male");
        rdbtnMale.setBackground(Color.DARK_GRAY);
        rdbtnMale.setFont(new Font("Tahoma", Font.PLAIN, 13));
        rdbtnMale.setForeground(Color.WHITE);
        rdbtnMale.setBounds(326, 164, 68, 23);
        add(rdbtnMale);

        JRadioButton rdbtnFemale = new JRadioButton("Female");
        rdbtnFemale.setBackground(Color.DARK_GRAY);
        rdbtnFemale.setFont(new Font("Tahoma", Font.PLAIN, 13));
        rdbtnFemale.setForeground(Color.WHITE);
        rdbtnFemale.setBounds(396, 164, 77, 23);
        add(rdbtnFemale);

        ButtonGroup genderRdBtn = new ButtonGroup();
        genderRdBtn.add(rdbtnFemale);
        genderRdBtn.add(rdbtnMale);

        JLabel lblAdmissionNo = new JLabel("Admission No:");
        lblAdmissionNo.setForeground(Color.WHITE);
        lblAdmissionNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblAdmissionNo.setBounds(235, 203, 97, 14);
        add(lblAdmissionNo);

        JLabel lblContactNo = new JLabel("Contact No:");
        lblContactNo.setForeground(Color.WHITE);
        lblContactNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblContactNo.setBounds(235, 240, 77, 14);
        add(lblContactNo);

        JLabel lblDiploma = new JLabel("Diploma In:");
        lblDiploma.setForeground(Color.WHITE);
        lblDiploma.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblDiploma.setBounds(235, 275, 76, 14);
        add(lblDiploma);

        txtbxFullName = new JTextField();
        txtbxFullName.setBounds(326, 130, 147, 20);
        add(txtbxFullName);
        txtbxFullName.setColumns(10);

        txtAdmissionNo = new JTextField();
        txtAdmissionNo.setBounds(326, 201, 147, 20);
        add(txtAdmissionNo);
        txtAdmissionNo.setColumns(10);

        txtbxContactNo = new JTextField();
        txtbxContactNo.setBounds(326, 238, 147, 20);
        add(txtbxContactNo);
        txtbxContactNo.setColumns(10);

        txtFieldDiploma = new JTextField();
        txtFieldDiploma.setBounds(326, 273, 147, 20);
        add(txtFieldDiploma);
        txtFieldDiploma.setColumns(10);

        JButton btnSubmit = new JButton("SUBMIT");
        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Home home = new Home(mf);
                mf.setContentPane(home);
                mf.setVisible(true);
            }
        });
        btnSubmit.setBounds(235, 336, 114, 23);
        add(btnSubmit);

        JButton btnCancel = new JButton("CANCEL");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Home home = new Home(mf);
                mf.setContentPane(home);
                mf.setVisible(true);
            }
        });
        btnCancel.setBounds(359, 336, 114, 23);
        add(btnCancel);

        JButton btnBrowse = new JButton("Browse");
        btnBrowse.setBounds(101, 247, 89, 23);
        add(btnBrowse);
    }



}

以下是个人资料页面:

package Project;
import java.awt.Color;
import Project.ProfileEdit;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import Project.ProfileEdit;

public class Home extends MasterPanel {

    /**
     * Create the panel.
     */
    public Home(JFrame mf) {
        super(mf);


        JLabel lblUserProfile = new JLabel("");
        //createTitledBorder(border, string, int, int, font, color)
        lblUserProfile.setBorder(BorderFactory.createTitledBorder(null,"User Profile",0,0, new Font("Tahoma", Font.PLAIN, 15), Color.WHITE)); 
        //setBounds(left, top, right, bottom)
        lblUserProfile.setBounds(60, 103, 510, 267);
        add(lblUserProfile);

        JLabel lblDisplayPic = new JLabel("");
        lblDisplayPic.setBorder(new LineBorder(Color.WHITE));
        lblDisplayPic.setIcon(new ImageIcon(Home.class.getResource("/javaProject/images/default-avatar.png")));
        lblDisplayPic.setBounds(108, 142, 90, 100);
        add(lblDisplayPic);

        JLabel lblFullName = new JLabel("Full Name:");
        lblFullName.setForeground(Color.WHITE);
        lblFullName.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblFullName.setBounds(251, 131, 76, 14);
        add(lblFullName);



        JLabel lblGender = new JLabel("Gender: ");
        lblGender.setForeground(Color.WHITE);
        lblGender.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblGender.setBounds(251, 151, 60, 14);
        add(lblGender);

        JLabel lblAdmissionNo = new JLabel("Admission No:");
        lblAdmissionNo.setForeground(Color.WHITE);
        lblAdmissionNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblAdmissionNo.setBounds(251, 176, 97, 14);
        add(lblAdmissionNo);

        JLabel lblContactNo = new JLabel("Contact No:");
        lblContactNo.setForeground(Color.WHITE);
        lblContactNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblContactNo.setBounds(251, 195, 77, 14);
        add(lblContactNo);

        JLabel lblDiploma = new JLabel("Diploma In:");
        lblDiploma.setForeground(Color.WHITE);
        lblDiploma.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblDiploma.setBounds(251, 218, 76, 14);
        add(lblDiploma);

        JButton btnEdit = new JButton("Edit ");
        btnEdit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                ProfileEdit ProfileEdit = new ProfileEdit(mf);
                mf.setContentPane(ProfileEdit);
                mf.setVisible(true);

            }
        });
        btnEdit.setBounds(251, 309, 89, 23);
        add(btnEdit);
    }

}

最后,如果你们需要它,这里是主人小组:

package Project;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;

import java.awt.Color;
import javax.swing.JSeparator;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MasterPanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected JFrame myFrame = null;

    /**
     * Create the panel.
     */
    public MasterPanel(JFrame mf) {
        myFrame = mf;
        setBackground(Color.DARK_GRAY);
        setLayout(null);

        //set a new menubar for home
        JMenuBar menuBarHome = new JMenuBar();
        menuBarHome.setForeground(Color.BLACK);
        menuBarHome.setBackground(Color.WHITE);
        // x,y,width,height
        // >x = right, <x = left
        // >y = bottom, <y = up
        menuBarHome.setBounds(80, 40, 97, 21);
        //length and height
        menuBarHome.setSize(48, 20);
        add(menuBarHome);
        JMenu home = new JMenu("HOME");
        home.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {

                Home home = new Home(mf);
                mf.setContentPane(home);
                mf.setVisible(true);

            }
        });
        menuBarHome.add(home);

        //set a new menubar for calendar
        JMenuBar menuBarCalendar = new JMenuBar();
        menuBarCalendar.setForeground(Color.BLACK);
        menuBarCalendar.setBackground(Color.WHITE);
        menuBarCalendar.setBounds(145, 40, 97, 21);
        menuBarCalendar.setSize(75, 20);
        add(menuBarCalendar);
        JMenu calendar = new JMenu("CALENDAR");
        calendar.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                Calendar calendar = new Calendar(mf);
                mf.setContentPane(calendar);
                mf.setVisible(true);

            }
        });
        menuBarCalendar.add(calendar);

        JMenuBar menuBarAcademic = new JMenuBar();
        menuBarAcademic.setForeground(Color.BLACK);
        menuBarAcademic.setBackground(Color.WHITE);
        menuBarAcademic.setBounds(235, 40, 220, 21);
        menuBarAcademic.setSize(72, 20);
        add(menuBarAcademic);
        JMenu academic = new JMenu("ACADEMIC");
        menuBarAcademic.add(academic);

        //add sub menu into the academic
        JMenuItem lectureNotes = new JMenuItem("Lecture Notes");
        lectureNotes.setForeground(Color.BLACK);
        lectureNotes.setBackground(Color.WHITE);
        lectureNotes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                LectureNotes LectureNotes = new LectureNotes(mf);
                mf.setContentPane(LectureNotes);
                mf.setVisible(true);
            }       
        });
        academic.add(lectureNotes);



        JMenuItem resulttracker = new JMenuItem("Result Tracker");
        resulttracker.setForeground(Color.BLACK);
        resulttracker.setBackground(Color.WHITE);
        resulttracker.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                ResultTracker resulttracker = new ResultTracker(mf);
                mf.setContentPane(resulttracker);
                mf.setVisible(true);
            }       
        });
        academic.add(resulttracker);

        JMenuItem studyplan = new JMenuItem("Study Planner");
        studyplan.setForeground(Color.BLACK);
        studyplan.setBackground(Color.WHITE);
        studyplan.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StudyPlanner studyplanner = new StudyPlanner(mf);
                mf.setContentPane(studyplanner);
                mf.setVisible(true);
            }
        });

        academic.add(studyplan);

        JMenuItem timetable = new JMenuItem("Timetable");
        timetable.setForeground(Color.BLACK);
        timetable.setBackground(Color.WHITE);
        timetable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TimeTable timetable = new TimeTable(mf);
                mf.setContentPane(timetable);
                mf.setVisible(true);
            }
        });

        academic.add(timetable);

        JMenuBar menuBarEmail = new JMenuBar();
        menuBarEmail.setForeground(Color.BLACK);
        menuBarEmail.setBackground(Color.WHITE);
        menuBarEmail.setBounds(320, 40, 97, 21);
        menuBarEmail.setSize(47, 20);
        add(menuBarEmail);
        JMenu email = new JMenu("EMAIL");
        menuBarEmail.add(email);

        JMenuItem composeemail = new JMenuItem("Compose");
        composeemail.setForeground(Color.BLACK);
        composeemail.setBackground(Color.WHITE);
        composeemail.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Email email = new Email(mf);
                mf.setContentPane(email);
                mf.setVisible(true);
            }
        });
        email.add(composeemail);
        JMenuItem inboxemail = new JMenuItem("Inbox");
        inboxemail.setForeground(Color.BLACK);
        inboxemail.setBackground(Color.WHITE);
        email.add(inboxemail);


        JMenuBar menuBarContactus = new JMenuBar();
        menuBarContactus.setForeground(Color.BLACK);
        menuBarContactus.setBackground(Color.WHITE);
        menuBarContactus.setBounds(380, 40, 97, 21);
        menuBarContactus.setSize(85, 20);
        add(menuBarContactus);
        JMenu contactus= new JMenu("CONTACT US");
        contactus.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)  {
                ContactUs contactus = new ContactUs(mf);
                mf.setContentPane(contactus);
                mf.setVisible(true);
            }
        });
        menuBarContactus.add(contactus);


        JMenuBar menuBarLogout = new JMenuBar();
        menuBarLogout.setForeground(Color.BLACK);
        menuBarLogout.setBackground(Color.WHITE);
        menuBarLogout.setBounds(480, 40, 97, 21);
        menuBarLogout.setSize(60, 20);
        add(menuBarLogout);
        JMenu logout = new JMenu("LOGOUT");
        logout.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Login login = new Login(mf);
                mf.setContentPane(login);
                mf.setVisible(true);

            }
        });
        menuBarLogout.add(logout);

        JSeparator separatorTop = new JSeparator();
        separatorTop.setBounds(50, 23, 524, 2);
        add(separatorTop);

        JSeparator separatorBottom = new JSeparator();
        separatorBottom.setBounds(50, 79, 524, 2);
        add(separatorBottom);


    }

}

0 个答案:

没有答案