🔒 Closed JAVA exam

Status
Not open for further replies.

Master Oogway

Forum Guru
mga lods, pahelp dito. Di ko kse mapashow sa another panel ung character kasama yung selected weapon. GUI po to.
import javax.swing.*;
import java.awt.*;

public class CharacterSelectionGUI extends JFrame {
private WeaponSelectionGUI weaponSelectionGUI;
private JLabel selectedCharacterLabel;

public CharacterSelectionGUI() {
setTitle("Choose a Character");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);

JPanel characterButtonPanel = new JPanel(new GridLayout(3, 1));

JButton berserkerButton = new JButton("Berserker");
JButton sharpshooterButton = new JButton("Sharpshooter");
JButton taoistButton = new JButton("Taoist");

berserkerButton.addActionListener(e -> selectCharacter("Berserker"));
sharpshooterButton.addActionListener(e -> selectCharacter("Sharpshooter"));
taoistButton.addActionListener(e -> selectCharacter("Taoist"));

characterButtonPanel.add(berserkerButton);
characterButtonPanel.add(sharpshooterButton);
characterButtonPanel.add(taoistButton);

selectedCharacterLabel = new JLabel("Selected Character: None");

JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(e -> dispose());

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(characterButtonPanel, BorderLayout.CENTER);
contentPane.add(selectedCharacterLabel, BorderLayout.PAGE_START);
contentPane.add(cancelButton, BorderLayout.PAGE_END);

setVisible(true);
}

private void selectCharacter(String character) {
weaponSelectionGUI = new WeaponSelectionGUI(this);
weaponSelectionGUI.setCharacter(character);
setVisible(false);
}

public void showGUI() {
setVisible(true);
}

public void setWeapon(String weapon, int damage, int range) {
String character = weaponSelectionGUI.getCharacter();
System.out.println(character + " selected " + weapon + ".");

selectedCharacterLabel.setText("Selected Character: " + character + ", Weapon: " + weapon +
", Damage: " + damage + ", Range: " + range);

pack();
setLocationRelativeTo(null);
}

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

class WeaponSelectionGUI extends JFrame {
private CharacterSelectionGUI characterSelectionGUI;

private String selectedCharacter;

public WeaponSelectionGUI(CharacterSelectionGUI characterSelectionGUI) {
this.characterSelectionGUI = characterSelectionGUI;

setTitle("Choose a Weapon");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);

JPanel weaponButtonPanel = new JPanel(new GridLayout(3, 1));

JButton swordButton = new JButton("Sword");
JButton bowButton = new JButton("Bow");
JButton staffButton = new JButton("Staff");

swordButton.addActionListener(e -> selectWeapon("Sword", 10, 2));
bowButton.addActionListener(e -> selectWeapon("Bow", 8, 5));
staffButton.addActionListener(e -> selectWeapon("Staff", 6, 3));

weaponButtonPanel.add(swordButton);
weaponButtonPanel.add(bowButton);
weaponButtonPanel.add(staffButton);

JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(e -> cancelSelection());

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(weaponButtonPanel, BorderLayout.CENTER);
contentPane.add(cancelButton, BorderLayout.PAGE_END);

setVisible(true);
}

public void setCharacter(String character) {
selectedCharacter = character;
}

public String getCharacter() {
return selectedCharacter;
}

private void selectWeapon(String weapon, int damage, int range) {
characterSelectionGUI.setWeapon(weapon, damage, range);
dispose();
}

private void cancelSelection() {
characterSelectionGUI.showGUI();
dispose();
}
}
 
To display the currently selected weapon along with the character, you can add another label to the GUI and update it when the user selects a weapon.

1. Add a new JLabel instance to the CharacterSelectionGUI class to display the selected weapon.
Java:
private JLabel selectedWeaponLabel;

2. Initialize the label in the constructor of the CharacterSelectionGUI class and add it to the GUI in the BorderLayout.PAGE_START position.
Java:
selectedWeaponLabel = new JLabel("Selected Weapon: None");
contentPane.add(selectedWeaponLabel, BorderLayout.PAGE_START);

3. Update the setWeapon method in the CharacterSelectionGUI class to set the text of the selectedWeaponLabel to the selected weapon.
Java:
selectedWeaponLabel.setText("Selected Weapon: " + weapon + ", Damage: " + damage + ", Range: " + range);

4. Run the application and select a character and a weapon. The selected character and weapon should be displayed in the GUI.
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 1K
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
950
Guests online
1,368
Total visitors
2,318

Forum statistics

Threads
2,272,867
Posts
28,945,952
Members
1,236,445
Latest member
zy2024
Back
Top