Domanda

I'm having a few problems with my final programming assignment for my 202 class.

First of all, my assignment is to create a game called Togiz Kumalak in Java. As you can see I am far from finished.

My two problems so far are that my buttons and text display aren't visible, and I don't know how to make the numbers inside of my buttons (they represent seeds) into variables that can change from numbers 0-10. Thanks for your help.

I apologize for the mess, I've added things that don't yet function as a result of my ADD.

EDIT: I figured out why my panels weren't visible, it was because I had my fourth panel visible with no content added to it. The second question still stands though.

Edit2: Updated title, and code.

import java.awt.*;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JLabel;

public class Togiz_Kumalak {

    private static void createAndShowUI() {
        CupGui gui = new CupGui();
        TkMenu menu = new TkMenu(gui);
        JFrame frame = new JFrame("Togiz Kumalak");
        frame.getContentPane().add(gui.getMainPanel());

        frame.setJMenuBar(menu.getJMenuBar());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setFocusable(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }

    private Togiz_Kumalak() {
    }
}

class CupGui {

    private   String[][] Cup1_Texts = {
        {"10", "10", "10", "10","10", "10", "10"},};
        private  String[][] Cup2_Texts = {
        {"10", "10", "10", "10","10", "10", "10"},};

    private static final int GAP = 5;
    private static final Font BTN_FONT = new Font(Font.DIALOG, Font.BOLD, 20);
    private JPanel firstPanel = new JPanel();
    private JPanel secondPanel = new JPanel();
    private JPanel thirdPanel = new JPanel();
    private JPanel fourthPanel = new JPanel();
    private JPanel textPanel = new JPanel();
    private JPanel mainPanel = new JPanel();
    private JPanel cupPane1;
    private JPanel cupPane2;
    private JTextField display = new JTextField();
    private JLabel label = new JLabel("test");

    CupGui() {
        display.setFont(BTN_FONT);
        cupPane1 = createBtnPanel(Cup1_Texts, "Player 1");
        cupPane2 = createBtnPanel(Cup2_Texts, "Player 2");
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        firstPanel.add(cupPane1, BorderLayout.CENTER);

        secondPanel.add(cupPane2, BorderLayout.CENTER);
        thirdPanel.add(firstPanel, BorderLayout.LINE_START);
        thirdPanel.add(secondPanel, BorderLayout.LINE_END);
        thirdPanel.add(fourthPanel, BorderLayout.PAGE_START);
        fourthPanel.add(display, BorderLayout.CENTER);


        mainPanel.add(thirdPanel);





        cupPane1.setVisible(true);
        cupPane2.setVisible(true);
        firstPanel.setVisible(true);
        secondPanel.setVisible(true);
        thirdPanel.setVisible(true);
        fourthPanel.setVisible(true);

    }

    public void cupPanelSetVisible(boolean visible) {
        cupPane1.setVisible(visible);
        Window win = SwingUtilities.getWindowAncestor(mainPanel);
        win.pack();
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }
    public JPanel getFirstPanel(){
      return firstPanel;
    }
    public JPanel getSecondPanel(){
      return secondPanel;
    }

    private JPanel createBtnPanel(String[][] texts, String title) {
        JPanel btnPanel = new JPanel();
        int rows = texts.length;
        int cols = texts[0].length;
        btnPanel.setLayout(new GridLayout(rows, cols, GAP, GAP));
        for (int row = 0; row < texts.length; row++) {
            for (int col = 0; col < texts[row].length; col++) {
                JButton btn = new JButton(texts[row][col]);
                btn.setFont(BTN_FONT);
                btnPanel.add(btn);
            }
        }
        btnPanel.setBorder(BorderFactory.createTitledBorder(title));
        return btnPanel;
    }
}

class TkMenu {

    private static final String NEW_GAME =  "New Game";
    private static final String LOAD_GAME = "Load Game";
    private static final String SAVE_GAME = "Save Game";

    private CupGui gui;
    private JMenuBar menuBar = new JMenuBar();
    private JMenuItem newGame;
    private JMenuItem loadGame;
    private JMenuItem saveGame;

    TkMenu(CupGui gui) {
        this.gui = gui;
        newGame = new JMenuItem(NEW_GAME, KeyEvent.VK_T);
        loadGame = new JMenuItem(LOAD_GAME, KeyEvent.VK_S);
        saveGame = new JMenuItem(SAVE_GAME, KeyEvent.VK_S);



        newGame.setEnabled(true);
        JMenu viewMenu = new JMenu("File");
        viewMenu.setMnemonic(KeyEvent.VK_V);
        viewMenu.add(newGame);
        viewMenu.add(loadGame);
        viewMenu.add(saveGame);

        menuBar.add(viewMenu);

    }

    public JMenuBar getJMenuBar() {
        return menuBar;
    }


        }
È stato utile?

Soluzione

Try adding an ActionListener to your buttons and then setting the text equal to a variable of your choosing. You can also get the current text of the button, perform a calculation and then set the button's text again. For example:

 btn.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    int x;
                    x = Integer.parseInt(btn.getText());
                    x--;
                    btn.setText(Integer.toString(x));
                }
            });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top