我在使用 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);
}
}