简体   繁体   中英

Change attribute in other jframe when a button is clicked in another JFrame

I have 2 jframes, 1 is kinda like the main menu, i want an attribute to change in the level jframe when a button is pressed so i tried:

SpeelVeld frame = new SpeelVeld();
    frame.level = 1;
    System.out.println(frame.level);

I used the sout to see what really happens because it wasnt working, but i see that the level goes from 0 to 1 back to 0 and goes on and on, does someone know why and how to fix?

SpeelVeld frame = new SpeelVeld();


    frame.setBounds(0,0,519,591);
    frame.setLocationRelativeTo(null);
    frame.getContentPane().setBackground(Color.WHITE);
    frame.setTitle("RWINA");
    frame.setVisible(true);
    frame.setLevel(1);

this is in the main method of my original GameProject file.

How can i make a jdialog

I have 2 jframes, 1 is kinda like the main menu,

You shouldn't use 2 JFrames for this. The dependent sub-window, likely your main menu window, should in fact be a JDialog, probably a non-modal dialog from the looks of it.

I want an attribute to change in the level jframe when a button is pressed so i tried:

 SpeelVeld frame = new SpeelVeld(); frame.level = 1; System.out.println(frame.level); 

and here's a big problem. Understand that in this code, you're creating a new SpeelVeld object, the stress being on the word new . Changing the state of this object will have no effect on the other SeelVeld object that is currently being displayed. Do do that, your second window will need a valid reference to the displayed SeelVeld object. How to do this will depend all on code not yet shown, but often it can be done simply by passing in the displayed SpeelVeld object into the main menu object by use of a constructor parameter or setter method.

For example:

import java.awt.Dialog.ModalityType;    
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

// JPanel for our main GUI
public class SpeelVeldFoo {

    private static void createAndShowGui() {
        // JPanel used by the main JFrame
        SpeelVeldPanel speelVeldPanel = new SpeelVeldPanel();

        // JPanel used by the main menu JDialog. Pass the above into it
        MainMenuPanel mainMenuPanel = new MainMenuPanel(speelVeldPanel); 

        // create your JFrame
        JFrame frame = new JFrame("Speel Veld");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(speelVeldPanel); // add the JPanel
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        // create your non-modal JDialog
        JDialog menuDialog = new JDialog(frame, "Main Menu", ModalityType.MODELESS);
        menuDialog.add(mainMenuPanel); // add the JPanel that holds its "guts"
        menuDialog.pack();
        menuDialog.setLocationByPlatform(true);
        menuDialog.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            createAndShowGui();
        });
    }
}

@SuppressWarnings("serial")
class SpeelVeldPanel extends JPanel {
    private int level = 1; // simple example just has a level int
    private JLabel levelLabel = new JLabel("1"); // and displays it in a JLabel

    public SpeelVeldPanel() {
        add(new JLabel("Level:"));
        add(levelLabel);

        int ebGap = 50;
        setBorder(BorderFactory.createEmptyBorder(ebGap, 2 * ebGap, ebGap, 2 * ebGap));
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        // whenever level is changed, update the display
        this.level = level;
        levelLabel.setText(String.valueOf(level));
    }
}

// class for the JPanel held by the JDialog
@SuppressWarnings("serial")
class MainMenuPanel extends JPanel {
    private JSpinner levelSpinner = new JSpinner(new SpinnerNumberModel(1, 1, 5, 1));
    private SpeelVeldPanel speelVeldPanel = null; // reference to the main GUI

    // note the parameter.... you pass in the displayed main GUI so you can
    // change it
    public MainMenuPanel(final SpeelVeldPanel speelVeldPanel) {
        this.speelVeldPanel = speelVeldPanel; // set the field

        // respond when the spinner's data changes
        levelSpinner.addChangeListener(new LevelListener());

        add(new JLabel("Set the Speel Veld's level:"));
        add(levelSpinner);

        int ebGap = 10;
        setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
    }

    private class LevelListener implements ChangeListener {

        @Override
        public void stateChanged(ChangeEvent e) {
            // when the spinner's data changes
            int level = (int) levelSpinner.getValue(); // get the data
            speelVeldPanel.setLevel(level); // and send it to the main GUI
        }
    }
}

You'll note that I don't like extending JFrame or JDialog if I can avoid it. My feeling is that one can paint oneself into a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.

You probably want the JFrame to be the top-level container, then have a JPanel that holds your menu. The menu could be whatever you want, I'm using a JTextArea. Then, you need a JButton for the JPanel or JFrame that when pressed, changes the text in the JTextArea. Here is an implementation that you could work from. I'm using the ActionEvent as the trigger for when to mess with the JTextArea:

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.JTextArea;

public class SimpleSwing {
    public static void main(String[] args) {
        JFrame mainFrame = new JFrame();
        JPanel mainMenuPanel = new JPanel();
        JTextArea textAttribute = new JTextArea("Original Text");
        JButton changeAttributeButton = new JButton("Change Attribute");

        changeAttributeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textAttribute.setText("Whatever new text you want");
            }
        });

        mainMenuPanel.add(textAttribute);
        mainMenuPanel.add(changeAttributeButton);
        mainFrame.add(mainMenuPanel);

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(500, 500);
        mainFrame.setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM