如何从一个GUI类传递到另一个GUI类?

时间:2015-11-09 19:58:46

标签: java

这是我的LoginGUI类,我想从这个GUI类转移到另一个“StudentGUI”类。看起来很简单,但我想不出来

public static class numberPickerDialog extends DialogFragment
        implements NumberPicker.OnValueChangeListener {


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final NumberPicker picker = new NumberPicker(getActivity());
        picker.setMinValue(1);
        picker.setMaxValue(60);
        picker.setValue(25);
        picker.setWrapSelectorWheel(false);

        builder.setMessage("Set Study Time")
                .setPositiveButton("Set", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //cancel
                    }
                });

        final FrameLayout parent = new FrameLayout(getActivity());
        parent.addView(picker, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER));
        builder.setView(parent);

        Dialog dialog = builder.create();
        return dialog;
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        String toastMessage = "test";
        Toast toast = Toast.makeText(getActivity(), toastMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

}

1 个答案:

答案 0 :(得分:1)

如果您想在多个观看次数之间切换,请使用CardLayout这里的一个简单示例

package main.frames;

import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame
{
static JPanel homeContainer;
static JPanel homePanel;
static JPanel otherPanel;
static CardLayout cl;

public MainFrame()
{
    JButton showOtherPanelBtn = new JButton("Show Other Panel");
    JButton backToHomeBtn = new JButton("Show Home Panel");

    cl = new CardLayout(5, 5);
    homeContainer = new JPanel(cl);
    homeContainer.setBackground(Color.black);

    homePanel = new JPanel();
    homePanel.setBackground(Color.blue);
    homePanel.add(showOtherPanelBtn);

    homeContainer.add(homePanel, "Home");

    otherPanel = new JPanel();
    otherPanel.setBackground(Color.green);
    otherPanel.add(backToHomeBtn);

    homeContainer.add(otherPanel, "Other Panel");

    showOtherPanelBtn.addActionListener(e -> cl.show(homeContainer, "Other Panel"));
    backToHomeBtn.addActionListener(e -> cl.show(homeContainer, "Home"));

    add(homeContainer);
    cl.show(homeContainer, "Home");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setTitle("CardLayout Example");
    pack();
    setVisible(true);
}

public static void main(String[] args)
{
    SwingUtilities.invokeLater(MainFrame::new);
}
}