1

我在使用 JPanel 时遇到问题。我是 Swing 和一切的新手,所以其中一些可能是不正确的。任何帮助,将不胜感激。当调用battleGUISelection 方法时,问题出现在第二个类中。有时它会显示,但其他时候,例如在通过teachingMove 方法添加新移动后,它会显示为空白。JLabels 将完全不可见,并且在我将光标悬停在它们上之前,按钮不会出现。

这是我到目前为止...

import java.util.*;

@SuppressWarnings("unchecked")
public class Battle {

public int hp;
public String name;
public ArrayList<String> moves = new ArrayList<String>();   
public ArrayList<Integer> damages = new ArrayList<Integer>();

public Battle(String name_in)
{
    name = name_in;

    moves.add("punch");
    moves.add("kick");
    moves.add("slam");

    damages.add(10);
    damages.add(15);
    damages.add(20);
}



public void setHp(Battle player, int amt)
{
    player.hp = amt;
}

public void revive(Battle player, String difficulty_in)
{
    if(difficulty_in.equals("beginner"))
    {
        player.hp = 50;
    }
    else if(difficulty_in.equals("intermediate"))
    {
        player.hp = 75;
    }
    else if(difficulty_in.equals("expert"))
    {
        player.hp = 100;
    }
}
}

和gui类..

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;

public class GUIFrameTest extends JFrame {

        JPanel panel = new JPanel();
        JButton begButton = new JButton("Beginner");
        JButton intermButton = new JButton("Intermediate");
        JButton expertButton = new JButton("Expert");
        JButton okButton = new JButton("OK");
        JLabel title = new JLabel("SELECT DIFFICULTY");
        JLabel label = new JLabel();
        JLabel txtLabel = new JLabel();
        TextField name = new TextField();    

    static Battle newPlayer = new Battle("");
    static Battle enemyBeginner = new Battle("OPPONENT");
    static Battle enemyIntermediate = new Battle("OPPONENT");
static Battle enemyExpert = new Battle("OPPONENT");
static Battle enemy;
static String difficulty;

public GUIFrameTest(String windowName) 
{
    super(windowName);

    final Container c = getContentPane();
    panel.setLayout(null);

    //panel.setBounds(0, 0, 500, 500);

    begButton.setBounds(0, 25, 100, 50);
    intermButton.setBounds(125, 25, 110, 50);
    expertButton.setBounds(255, 25, 100, 50);
    label.setBounds(0, 100, 200, 50);
    name.setBounds(188, 225, 125, 25);
    txtLabel.setBounds(188, 178, 125, 50);
    okButton.setBounds(200, 255, 100, 25);
    title.setBounds(0, 0, 150, 15);

    txtLabel.setText("Enter name of player: ");


    begButton.addActionListener(new AbstractAction("add"){
        @Override
        public void actionPerformed(ActionEvent e){
            label.setText("Difficulty set to beginner");
            difficulty = "beginner";
            newPlayer.setHp(newPlayer, 50);
            enemyBeginner.setHp(enemyBeginner, 50);
            enemyBeginner.moves.add("punch");
            enemyBeginner.moves.add("kick");
            enemyBeginner.damages.add(10);
            enemyBeginner.damages.add(10);
            enemy = enemyBeginner;
        }
    });
    intermButton.addActionListener(new AbstractAction("add"){
        @Override
        public void actionPerformed(ActionEvent e){
            label.setText("Difficulty set to intermediate");
            difficulty = "intermediate";
            newPlayer.setHp(newPlayer, 75);
            enemyIntermediate.setHp(enemyIntermediate, 75);
            enemyIntermediate.moves.add("punch");
            enemyIntermediate.moves.add("kick");
            enemyIntermediate.moves.add("slam");
            enemyIntermediate.damages.add(20);
            enemyIntermediate.damages.add(15);
            enemyIntermediate.damages.add(25); 
            enemy = enemyIntermediate;
        }
    });
    expertButton.addActionListener(new AbstractAction("add"){
        @Override
        public void actionPerformed(ActionEvent e){
            label.setText("Difficulty set to expert");
            difficulty = "expert";
            newPlayer.setHp(newPlayer, 100);
            enemyExpert.setHp(enemyExpert, 100);
            enemyExpert.moves.add("super punch");
            enemyExpert.moves.add("smash");
            enemyExpert.moves.add("jump kick");
            enemyExpert.damages.add(20);
            enemyExpert.damages.add(25);
            enemyExpert.damages.add(30); 
            enemy = enemyExpert;

        }
    });
    okButton.addActionListener(new AbstractAction("add"){
        @Override
        public void actionPerformed(ActionEvent e){
            newPlayer.name = name.getText();
            panel.removeAll();
            panel.updateUI();
            battleGUISelection(c);
        }
    });

    panel.add(begButton);
    panel.add(intermButton);
    panel.add(expertButton);
    panel.add(label);
    panel.add(name);
    panel.add(txtLabel);
    panel.add(okButton);
    panel.add(title);

    panel.setBackground(Color.WHITE);

    c.add(panel);           
}

public static void battleGUISelection(final Container c)
{       
    int yCoord = 100;

    final JPanel nextPanel = new JPanel();
    JLabel title = new JLabel();

    nextPanel.updateUI();

    ArrayList<JLabel> moveLabelList = new ArrayList<JLabel>();
    ArrayList<JLabel> damageLabelList = new ArrayList<JLabel>();

    title.setText("Moves Known: ");
    title.setBounds(200, 75, 100, 30);

    nextPanel.setLayout(null);  

    JButton battleButton = new JButton("Battle");
    JButton moveButton = new JButton("Teach Moves");
    JButton healButton = new JButton("Heal");

    battleButton.setBounds(0, 360, 500, 30);
    moveButton.setBounds(0, 390, 500, 30);
    healButton.setBounds(0, 420, 500, 30);

    moveButton.addActionListener(new AbstractAction("add"){
        @Override
        public void actionPerformed(ActionEvent e){
            nextPanel.removeAll();
            nextPanel.updateUI();
            teachMove(c, nextPanel);

        }
    });

    nextPanel.add(battleButton);
    nextPanel.add(moveButton);
    nextPanel.add(healButton);
    nextPanel.add(title);

    for(int i=0; i<newPlayer.moves.size(); i++)
    {
        moveLabelList.add(new JLabel(newPlayer.moves.get(i).toString()));
        moveLabelList.get(i).setBounds(185, yCoord, 100, 15);
        yCoord=yCoord+25;
        nextPanel.add(moveLabelList.get(i));
    }

    yCoord = 100;

    for(int x=0; x<newPlayer.damages.size(); x++)
    {
        damageLabelList.add(new JLabel(newPlayer.damages.get(x).toString() + " damage"));
        damageLabelList.get(x).setBounds(280, yCoord, 100, 15);
        yCoord=yCoord+25;
        nextPanel.add(damageLabelList.get(x));
    }


    nextPanel.setBackground(Color.WHITE);
    //nextPanel.setVisible(true);
    c.setVisible(true); 
    c.add(nextPanel);
}

public static void teachMove(final Container x, final JPanel newPanel)
{
    final int confirmYOrN = 0;

    final JTextField moveName = new JTextField();
    final JSlider moveDamage = new JSlider(0, 30);
    final JLabel confirm = new JLabel("Great! Your move has been added.");

    JLabel entryLabel = new JLabel("Enter the new move's name: ");
    JLabel damageEntryLabel = new JLabel("Enter the new move's damage: ");
    JButton enterButton = new JButton("OK");

    confirm.setBounds(160, 350, 200, 15);

    enterButton.addActionListener(new AbstractAction("add"){
        @Override
        public void actionPerformed(ActionEvent e){
            final String mName = moveName.getText();
            final int mDam = moveDamage.getValue();
            newPlayer.moves.add(mName);
            newPlayer.damages.add(mDam);

            newPanel.removeAll();
            newPanel.updateUI();

            battleGUISelection(x);

        }
    });

    moveDamage.setMajorTickSpacing(10);
    moveDamage.setMinorTickSpacing(5);
    moveDamage.setPaintTicks(true);
    moveDamage.setPaintLabels(true);

    entryLabel.setBounds(160, 125, 175, 25);
    damageEntryLabel.setBounds(160, 200, 200, 25);
    moveName.setBounds(160, 155, 175, 25);
    moveDamage.setBounds(160, 230, 175, 45);
    enterButton.setBounds(225, 290, 60, 50);

    newPanel.add(entryLabel);
    newPanel.add(moveName);
    newPanel.add(damageEntryLabel);
    newPanel.add(moveDamage);
    newPanel.add(enterButton);

    x.add(newPanel);
    x.setVisible(true); 
}
public static void main(String[] args)
{
    GUIFrameTest window = new GUIFrameTest("BattleGUI");
     //Window properties
    window.setBounds(300, 300, 500, 500);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}    
}
4

3 回答 3

1

我已经为您提供了基本的 GUI 代码并更改为使用一系列布局......

public class GUICompoundLayout extends JFrame {

    JPanel panel = new JPanel();
    JButton begButton = new JButton("Beginner");
    JButton intermButton = new JButton("Intermediate");
    JButton expertButton = new JButton("Expert");
    JButton okButton = new JButton("OK");
    JLabel title = new JLabel("SELECT DIFFICULTY");
    JLabel label = new JLabel();
    JLabel txtLabel = new JLabel();
    TextField name = new TextField(10);
    static Battle newPlayer = new Battle("");
    static Battle enemyBeginner = new Battle("OPPONENT");
    static Battle enemyIntermediate = new Battle("OPPONENT");
    static Battle enemyExpert = new Battle("OPPONENT");
    static Battle enemy;
    static String difficulty;

    public GUICompoundLayout(String windowName) {
        super(windowName);

        JPanel pnlTop = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel pnlMiddle = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JPanel pnlBody = new JPanel(new GridBagLayout());

        pnlTop.setBackground(Color.WHITE);
        pnlMiddle.setBackground(Color.WHITE);
        pnlBody.setBackground(Color.WHITE);

        pnlTop.add(title);
        pnlMiddle.add(begButton);
        pnlMiddle.add(intermButton);
        pnlMiddle.add(expertButton);

        txtLabel.setText("Enter name of player: ");

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets(2, 2, 2, 2);
        pnlBody.add(txtLabel, gbc);

        gbc.gridy++;
        pnlBody.add(name, gbc);

        gbc.gridy++;
        pnlBody.add(okButton, gbc);

        setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;

        add(pnlTop, gbc);
        gbc.gridy++;
        add(pnlMiddle, gbc);
        gbc.gridy++;
        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add(pnlBody, gbc);

//        begButton.setBounds(0, 25, 100, 50);
//        intermButton.setBounds(125, 25, 110, 50);
//        expertButton.setBounds(255, 25, 100, 50);
//        label.setBounds(0, 100, 200, 50);
//        name.setBounds(188, 225, 125, 25);
//        txtLabel.setBounds(188, 178, 125, 50);
//        okButton.setBounds(200, 255, 100, 25);
//        title.setBounds(0, 0, 150, 15);
//
//        txtLabel.setText("Enter name of player: ");


        begButton.addActionListener(new AbstractAction("add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("Difficulty set to beginner");
                difficulty = "beginner";
                newPlayer.setHp(newPlayer, 50);
                enemyBeginner.setHp(enemyBeginner, 50);
                enemyBeginner.moves.add("punch");
                enemyBeginner.moves.add("kick");
                enemyBeginner.damages.add(10);
                enemyBeginner.damages.add(10);
                enemy = enemyBeginner;
            }
        });
        intermButton.addActionListener(new AbstractAction("add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("Difficulty set to intermediate");
                difficulty = "intermediate";
                newPlayer.setHp(newPlayer, 75);
                enemyIntermediate.setHp(enemyIntermediate, 75);
                enemyIntermediate.moves.add("punch");
                enemyIntermediate.moves.add("kick");
                enemyIntermediate.moves.add("slam");
                enemyIntermediate.damages.add(20);
                enemyIntermediate.damages.add(15);
                enemyIntermediate.damages.add(25);
                enemy = enemyIntermediate;
            }
        });
        expertButton.addActionListener(new AbstractAction("add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                label.setText("Difficulty set to expert");
                difficulty = "expert";
                newPlayer.setHp(newPlayer, 100);
                enemyExpert.setHp(enemyExpert, 100);
                enemyExpert.moves.add("super punch");
                enemyExpert.moves.add("smash");
                enemyExpert.moves.add("jump kick");
                enemyExpert.damages.add(20);
                enemyExpert.damages.add(25);
                enemyExpert.damages.add(30);
                enemy = enemyExpert;

            }
        });
        okButton.addActionListener(new AbstractAction("add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                newPlayer.name = name.getText();
                panel.removeAll();
                panel.updateUI();
                battleGUISelection(getContentPane());
            }
        });

//        panel.add(begButton);
//        panel.add(intermButton);
//        panel.add(expertButton);
//        panel.add(label);
//        panel.add(name);
//        panel.add(txtLabel);
//        panel.add(okButton);
//        panel.add(title);

//        panel.setBackground(Color.WHITE);

//        c.add(panel);
    }

    public static void battleGUISelection(final Container c) {
        int yCoord = 100;

        final JPanel nextPanel = new JPanel();
        JLabel title = new JLabel();

        nextPanel.updateUI();

        ArrayList<JLabel> moveLabelList = new ArrayList<JLabel>();
        ArrayList<JLabel> damageLabelList = new ArrayList<JLabel>();

        title.setText("Moves Known: ");
        title.setBounds(200, 75, 100, 30);

        nextPanel.setLayout(null);

        JButton battleButton = new JButton("Battle");
        JButton moveButton = new JButton("Teach Moves");
        JButton healButton = new JButton("Heal");

        battleButton.setBounds(0, 360, 500, 30);
        moveButton.setBounds(0, 390, 500, 30);
        healButton.setBounds(0, 420, 500, 30);

        moveButton.addActionListener(new AbstractAction("add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                nextPanel.removeAll();
                nextPanel.updateUI();
                teachMove(c, nextPanel);

            }
        });

        nextPanel.add(battleButton);
        nextPanel.add(moveButton);
        nextPanel.add(healButton);
        nextPanel.add(title);

        for (int i = 0; i < newPlayer.moves.size(); i++) {
            moveLabelList.add(new JLabel(newPlayer.moves.get(i).toString()));
            moveLabelList.get(i).setBounds(185, yCoord, 100, 15);
            yCoord = yCoord + 25;
            nextPanel.add(moveLabelList.get(i));
        }

        yCoord = 100;

        for (int x = 0; x < newPlayer.damages.size(); x++) {
            damageLabelList.add(new JLabel(newPlayer.damages.get(x).toString() + " damage"));
            damageLabelList.get(x).setBounds(280, yCoord, 100, 15);
            yCoord = yCoord + 25;
            nextPanel.add(damageLabelList.get(x));
        }


        nextPanel.setBackground(Color.WHITE);
        //nextPanel.setVisible(true);
        c.setVisible(true);
        c.add(nextPanel);
    }

    public static void teachMove(final Container x, final JPanel newPanel) {
        final int confirmYOrN = 0;

        final JTextField moveName = new JTextField();
        final JSlider moveDamage = new JSlider(0, 30);
        final JLabel confirm = new JLabel("Great! Your move has been added.");

        JLabel entryLabel = new JLabel("Enter the new move's name: ");
        JLabel damageEntryLabel = new JLabel("Enter the new move's damage: ");
        JButton enterButton = new JButton("OK");

        confirm.setBounds(160, 350, 200, 15);

        enterButton.addActionListener(new AbstractAction("add") {
            @Override
            public void actionPerformed(ActionEvent e) {
                final String mName = moveName.getText();
                final int mDam = moveDamage.getValue();
                newPlayer.moves.add(mName);
                newPlayer.damages.add(mDam);

                newPanel.removeAll();
                newPanel.updateUI();

                battleGUISelection(x);

            }
        });

        moveDamage.setMajorTickSpacing(10);
        moveDamage.setMinorTickSpacing(5);
        moveDamage.setPaintTicks(true);
        moveDamage.setPaintLabels(true);

        entryLabel.setBounds(160, 125, 175, 25);
        damageEntryLabel.setBounds(160, 200, 200, 25);
        moveName.setBounds(160, 155, 175, 25);
        moveDamage.setBounds(160, 230, 175, 45);
        enterButton.setBounds(225, 290, 60, 50);

        newPanel.add(entryLabel);
        newPanel.add(moveName);
        newPanel.add(damageEntryLabel);
        newPanel.add(moveDamage);
        newPanel.add(enterButton);

        x.add(newPanel);
        x.setVisible(true);
    }

    public static void main(String[] args) {
        GUICompoundLayout window = new GUICompoundLayout("BattleGUI");
        //Window properties
        window.setBounds(300, 300, 500, 500);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
}

这是我最终得到的用户界面......

在此处输入图像描述

布局管理器是您的朋友。 花点时间学习它们,它们将为您节省数小时的挫败感和烦恼。

于 2012-09-17T01:49:36.927 回答
0

问题是你已经设置

nextPanel.setLayout(null);

这导致面板的首选尺寸为零。考虑为此面板使用布局管理器,例如BoxLayout 。

于 2012-09-16T22:38:14.880 回答
0

很难看到,因为它是很多代码,但尝试在面板上调用 repaint() 而不是 updateUI()。

于 2012-09-16T22:28:36.983 回答