即使将按钮可见性设置为true,我的刽子手游戏的JButton也不会出现

时间:2016-06-07 21:56:26

标签: java

您好我正在用Java创建一个带有GUI的刽子手游戏,当我将字母的JButtons添加到GUI时我遇到了问题,因为它们没有出现。不知道为什么。我开始添加按钮的代码位于GamePanel类中,游戏,GameWord,GameMain和GameFrame类以及GamePanel类的部分未显示,因为它们不需要理解和回答问题。

GamePanel类:

//some imports were omitted because they are irrelevant to the post

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JButton;

public class GamePanel extends JPanel implements ActionListener
{
     //some declarations were omitted because they are irrelevant to the post

     private JButton a;
     private JButton b;
     private JButton c;
     private JButton d;
     private JButton e;
     private JButton f;
     private JButton g;
     private JButton h;
     private JButton i;
     private JButton j;
     private JButton k;
     private JButton l;
     private JButton m;
     private JButton n;
     private JButton o;
     private JButton p;
     private JButton q;
     private JButton r;
     private JButton s;
     private JButton t;
     private JButton u;
     private JButton v;
     private JButton w;
     private JButton x;
     private JButton y;
     private JButton z;

     public GamePanel(Game aGame)
     {
          // certain initialization's were omitted because they are irrelevant
          this.addLetters();

     }

     public void paint(Graphics g)
     {
         //paint code goes here some parts were omitted as they are irrelevant to the post
         repaint();

     }
     public void addLetters()
     {
         a = new JButton("A");
         b = new JButton("B");
         c = new JButton("C");
         d = new JButton("D");
         e = new JButton("E");
         f = new JButton("F");
         g = new JButton("G");
         h = new JButton("H");
         i = new JButton("I");
         j = new JButton("J");
         k = new JButton("K");
         l = new JButton("L");
         m = new JButton("M");
         n = new JButton("N");
         o = new JButton("O");
         p = new JButton("P");
         q = new JButton("Q");
         r = new JButton("R");
         s = new JButton("S");
         t = new JButton("T");
         u = new JButton("U");
         v = new JButton("V");
         w = new JButton("W");
         x = new JButton("X");
         y = new JButton("Y");
         z = new JButton("Z");

         a.addActionListener(this);
         b.addActionListener(this);
         c.addActionListener(this);
         d.addActionListener(this);
         e.addActionListener(this);
         f.addActionListener(this);
         g.addActionListener(this);
         h.addActionListener(this);
         i.addActionListener(this);
         j.addActionListener(this);
         k.addActionListener(this);
         l.addActionListener(this);
         m.addActionListener(this);
         n.addActionListener(this);
         o.addActionListener(this);
         p.addActionListener(this);
         q.addActionListener(this);
         r.addActionListener(this);
         s.addActionListener(this);
         t.addActionListener(this);
         u.addActionListener(this);
         v.addActionListener(this);
         w.addActionListener(this);
         x.addActionListener(this);
         y.addActionListener(this);
         z.addActionListener(this);

         a.setBounds(340, 250, 5, 5);
         b.setBounds(350, 250, 5, 5);
         c.setBounds(360, 250, 5, 5);
         d.setBounds(370, 250, 5, 5);
         e.setBounds(380, 250, 5, 5);
         f.setBounds(390, 250, 5, 5);
         g.setBounds(400, 250, 5, 5);
         h.setBounds(410, 250, 5, 5);
         i.setBounds(420, 250, 5, 5);
         j.setBounds(430, 250, 5, 5);
         k.setBounds(440, 250, 5, 5);
         l.setBounds(450, 250, 5, 5);
         m.setBounds(460, 250, 5, 5);
         n.setBounds(340, 350, 5, 5);
         o.setBounds(350, 350, 5, 5);
         p.setBounds(360, 350, 5, 5);
         q.setBounds(370, 350, 5, 5);
         r.setBounds(380, 350, 5, 5);
         s.setBounds(390, 350, 5, 5);
         t.setBounds(400, 350, 5, 5);
         u.setBounds(410, 350, 5, 5);
         v.setBounds(420, 350, 5, 5);
         w.setBounds(430, 350, 5, 5);
         x.setBounds(440, 350, 5, 5);
         y.setBounds(450, 350, 5, 5);
         z.setBounds(460, 350, 5, 5);

         a.setVisible(true);
         b.setVisible(true);
         c.setVisible(true);
         d.setVisible(true);
         e.setVisible(true);
         f.setVisible(true);
         g.setVisible(true);
         h.setVisible(true);
         i.setVisible(true);
         j.setVisible(true);
         k.setVisible(true);
         l.setVisible(true);
         m.setVisible(true);
         n.setVisible(true);
         o.setVisible(true);
         p.setVisible(true);
         q.setVisible(true);
         r.setVisible(true);
         s.setVisible(true);
         t.setVisible(true);
         u.setVisible(true);
         v.setVisible(true);
         w.setVisible(true);
         x.setVisible(true);
         y.setVisible(true);
         z.setVisible(true);


         this.add(a);
         this.add(b);
         this.add(c);
         this.add(d);
         this.add(e);
         this.add(f);
         this.add(g);
         this.add(h);
         this.add(i);
         this.add(j);
         this.add(k);
         this.add(l);
         this.add(m);
         this.add(n);
         this.add(o);
         this.add(p);
         this.add(q);
         this.add(r);
         this.add(s);
         this.add(t);
         this.add(u);
         this.add(v);
         this.add(w);
         this.add(x);
         this.add(y);
         this.add(z);

     }        
     public void actionPerformed(ActionEvent e)
     {
          //actionPerformed code omitted because it is irrelevant
     }

}

3 个答案:

答案 0 :(得分:1)

(实际上并不是答案;只是为了向您展示使用循环实现现有逻辑的容易程度)

int x = 340;
for (char c = 'A'; c <= 'Z'; ++c, x += 10) {
  JButton button = new JButton(Character.toString(c));

  final char lower = Character.toLowercase(c);
  button.addActionListener(new ActionListener() {
    @Override public void actionPerformed(ActionEvent e) {
      game.checkGuess(lower);
    }
  });

  button.setBounds(x, 250, 5, 5);
  button.setVisible(true);

  this.add(button);
}

请注意,我已经定义了一个匿名的每个按钮ActionListener,因此无需保留对数组中按钮的引用。

答案 1 :(得分:-1)

我发现你必须打电话给$('body').on('click', '.username', function(e) { console.log('hello!'); }); 它才能显示按钮,而super.paint(g);你需要拨打void addLetters(),这样你就可以设置尺寸和位置按钮。我还通过循环添加了按钮以消除不必要的重复。我的this.setLayout(null);paint()方法位于

之下

paint()方法

addLetters()

addLetters()方法

/**
 * paint the graphics to the screen
 */
public void paint(Graphics g)
{
     //paint code goes here some parts were omitted as they are irrelevant to the post
     super.paint(g);
     repaint();

}

答案 2 :(得分:-2)

我是安迪·特纳在评论中提到的第二个:你的代码有很多不必要的重复,因为你没有使用数组和/或集合,比如ArrayLists,但正如他所指出的那样,这不是你麻烦的原因。

一个问题是你在组件上使用setBounds(...)但忘记了布局管理器通常会忽略边界,位置和大小属性。请记住,JPanels默认使用FlowLayout,因此将根据此布局管理器和组件的首选大小放置和调整组件,并忽略您的边界。一种解决方案是使用null布局,但这导致丑陋的GUI在除了一个平台之外的所有平台上看起来很糟糕,并且难以调试和维护。不,学会使用然后使用布局管理器。请记住,您可以嵌套JPanel,每个都使用自己的布局,从而使用简单的布局管理器实现复杂的布局。

此外,你要重写JPanel的paint方法,但不要调用super的方法,这可能会导致严重的副作用。而是覆盖paintComponent,而不是绘制,并在覆盖中调用super的方法。

关于测试代码的注意事项,调用super.paint(g);作为paint方法的第一行显示按钮,但位置错误。

请注意,不是直接在JPanel上绘制,而是使用挂钩创建BufferedImages,创建图像的ImageIcons,然后通过调用JLabel上的setIcon(...)在JLabel中绘制它们。

例如,您可以使用JPanel保存GridLayout中的按钮:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class Hangman2 extends JPanel {
    private static final int GAP = 5;
    // best to create ImageIcons where you draw your Hangman in, and
    // then display it in a JLabel by calling setIcon(...) on the label.
    private JLabel imageLabel = new JLabel();
    private List<Icon> iconList = createIcons();

    public Hangman2() {
        JLabel titleLabel = new JLabel("Welcome to Hangman", SwingConstants.CENTER);
        titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 40f));

        JPanel buttonPanel = createButtonPanel();

        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
        setLayout(new BorderLayout());
        add(titleLabel, BorderLayout.PAGE_START);
        add(buttonPanel, BorderLayout.PAGE_END);

        // placeholder component just to show something in the center of the GUI
        add(Box.createRigidArea(new Dimension(200, 200)), BorderLayout.CENTER);
    }



    private List<Icon> createIcons() {
        List<Icon> iconList = new ArrayList<>();

        // TODO: create hangman drawing icons and place into array

        return iconList;
    }

    private JPanel createButtonPanel() {
        int rows = 2;
        int cols = 16;
        JPanel buttonPanel = new JPanel(new GridLayout(rows, cols, GAP, GAP));
        // number of empty JLabels in the beginning and end of the bottom row
        int labelCount = (2 * 16 - ('Z' - 'A' + 1)) / 2;
        for (char c = 'A'; c <= 'Z'; c++) {
            if (c - 'A' == cols) { // at 2nd row, add empty JLabels
                for (int i = 0; i < labelCount; i++) {
                    buttonPanel.add(new JLabel());
                }
            }
            JButton button = new JButton("" + c);
            buttonPanel.add(button);
        }
        for (int i = 0; i < labelCount; i++) {
            buttonPanel.add(new JLabel());
        }
        return buttonPanel;
    }



    private static void createAndShowGui() {
        JFrame frame = new JFrame("Hangman");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new Hangman2());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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