如何将功能添加到我的JButtons

时间:2016-05-04 12:06:54

标签: java jbutton

我试图制作一个texttwist类型的游戏,你可以获得随机字母,并且必须将它们变成单词,有点像拼字游戏。我需要弄清楚如何将功能添加到我的jbuttons中,例如,清除按钮清除猜测面板,或提交按钮提交它等等。

package gui;

import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import gui.event_handlers.GuessButtonHandler;
import gui.event_handlers.InventoryButtonHandler;
import model.Model;
import model.Observer;

public class GUI implements Observer {

    private JFrame _window;
    private JPanel _wordsToGuessPanel;
    private JPanel _inventoryPanel;
    private JPanel _guessPanel;
    private Model _model;
    private JButton _submit;

    public GUI(Model m) {
        _model = m;
        _model.addObserver(this);

        _window = new JFrame("TwistyWord");
        _window.getContentPane().setLayout(new BoxLayout(_window.getContentPane(), BoxLayout.Y_AXIS));

        _wordsToGuessPanel = new JPanel();
        _window.getContentPane().add(_wordsToGuessPanel);

        JPanel middlePanel = new JPanel();
        middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.X_AXIS));
        _window.getContentPane().add(middlePanel);

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
        _window.getContentPane().add(controlPanel);

        JButton shuffle = new JButton("shuffle");
        setButtonProperties(shuffle);
        controlPanel.add(shuffle);

        JButton clear = new JButton("clear");
        setButtonProperties(clear);
        controlPanel.add(clear);

        _submit = new JButton("submit");
        setButtonProperties(_submit);
        controlPanel.add(_submit);

        _inventoryPanel = new JPanel();
        _inventoryPanel.setLayout(new BoxLayout(_inventoryPanel, BoxLayout.X_AXIS));

        _guessPanel = new JPanel();
        _guessPanel.setLayout(new BoxLayout(_guessPanel, BoxLayout.X_AXIS));

        middlePanel.add(new JLabel("INVENTORY: "));
            middlePanel.add(_inventoryPanel);
            middlePanel.add(new JLabel("GUESS: "));
        middlePanel.add(_guessPanel);

        _model.start();

        _window.setVisible(true);
        _window.pack();
        _window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }

    @Override
    public void update() {
        _inventoryPanel.removeAll();
        ArrayList<Character> inventoryLetters = _model.getInventoryLetters();
        for (int i=0; i<inventoryLetters.size(); i=i+1) {
            JButton b = new JButton(""+inventoryLetters.get(i));
            setButtonProperties(b);
            _inventoryPanel.add(b);
            b.addActionListener(new InventoryButtonHandler(_model, i));
        }
        _guessPanel.removeAll();
        ArrayList<Character> guessLetters = _model.getGuessLetters();
        for (int i=0; i<guessLetters.size(); i=i+1) {
            JButton b = new JButton(""+guessLetters.get(i));
            setButtonProperties(b);
            _guessPanel.add(b);
            b.addActionListener(new GuessButtonHandler(_model, i));
        }
        _window.pack();
        _window.repaint();
    }

    public void setButtonProperties(JButton button) {
        button.setFont(new Font("Courier", Font.BOLD, 44));
        button.setBackground(Color.WHITE);
        button.setForeground(Color.BLACK);
        button.setOpaque(true);
        button.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.DARK_GRAY, Color.LIGHT_GRAY));
    }

}

1 个答案:

答案 0 :(得分:-1)

 yourbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               //you logic here
            }
        });
    }