Java 具有多个面板的GridBagLayout

Java 具有多个面板的GridBagLayout,java,swing,jframe,layout-manager,gridbaglayout,Java,Swing,Jframe,Layout Manager,Gridbaglayout,我想创建一个包含多个面板的JFrame。但是,我希望有两个单独的面板,一个用于按钮,另一个用于文本。我创建了这两个框架,但现在我尝试使用GridBagLayout以我希望的方式将它们组织到JFrame中。问题是文本面板不会移动到我放置的gridx,gridy package gameTest; import java.util.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.Action

我想创建一个包含多个面板的
JFrame
。但是,我希望有两个单独的面板,一个用于按钮,另一个用于文本。我创建了这两个框架,但现在我尝试使用
GridBagLayout
以我希望的方式将它们组织到
JFrame
中。问题是文本面板不会移动到我放置的
gridx,gridy

package gameTest;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SpaceBattle 
{
public int turnCount = 1;
public int buttonCount = 0;
public JFrame bFrame = new JFrame("Space Battle");
public SpaceBattle(ArrayList<Integer> pStats,ArrayList<Integer> eStats)
{       


    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new GridBagLayout());

    JPanel buttonPanel = new JPanel();
    JPanel textPanel = new JPanel();
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 2;
    mainPanel.add(buttonPanel,c);

    bFrame.setBackground(Color.BLACK);
    bFrame.setLayout(new BorderLayout());
    JLabel turnNum = new JLabel("Turn " + turnCount);
    c.gridx = 0;
    c.gridy = 5;
    mainPanel.add(turnNum,c);
    turnNum.setFont(new Font("Serif", Font.BOLD, 24));

    bFrame.setSize(800,400);
    bFrame.setVisible(true);

    JButton attackButton = new JButton("Attack");
    JLabel atkText = new JLabel();
    JLabel dfsText = new JLabel();

    JButton fleeButton = new JButton("Flee");
    JLabel fleeText = new JLabel("You get out by the skin of your teeth.");

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    bFrame.setLocation(dim.width/2-bFrame.getSize().width/2, dim.height/2-bFrame.getSize().height/2);
    attackButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            if(eStats.get(2) > 0)
            {

                turnCount++;
                turnNum.setText("Turn " + turnCount);

                //This greatly improves readability of the code
                double tempShieldAbsorb = (double)eStats.get(0);
                double tempShieldDurability = (double)eStats.get(1);
                double tempArmor = (double)eStats.get(2);
                double tempAttack = (double)pStats.get(3);
                double tempAccuracy = (double)pStats.get(4);
                double tempMaxShields = (double)eStats.get(5);

                double tempDmg = tempAttack * (tempAccuracy / 100); //takes the player's dmg and accuracy to determine how much damage it deals per turn.
                double tempArmorNegationPercent = (double)(100/(100+tempArmor));


                if(tempShieldDurability > 0) //makes sure the shields are up
                {
                    if(tempShieldAbsorb < tempDmg) //checks to see if the damage is greater than the shields can take
                    {
                        tempArmor -= (tempDmg - tempShieldAbsorb) * tempArmorNegationPercent; 
                        tempShieldDurability -= tempShieldAbsorb; //if the damage is greater than the shields absorbtion, the shields durability will be hit a max of the shields absorbtion, the rest goes to the armor
                    }                       
                    else if(tempShieldAbsorb>=tempDmg) //if the damage is less than the shields can take, the shield's durability will be hit by the damage
                    {
                        tempShieldDurability -= tempDmg;
                    }
                }
                else
                {
                    tempArmor = tempArmor - (tempDmg * tempArmorNegationPercent); //if there is no more durability on the shields, the armor is hit harder
                }                                       

                tempShieldDurability = Math.round(tempShieldDurability);

                if(tempShieldDurability < 0) //If the tempShields is ever negative it will set it to 0.
                {
                    tempShieldDurability = 0;
                    eStats.set(1, 0);
                }
                else
                {
                    eStats.set(1, (int)tempShieldDurability);
                }
                if(tempArmor < 0) //if the tempArmor is ever negative it will set it to 0.
                {
                    eStats.set(2, 0);
                    tempArmor = 0;
                }
                else
                {
                    eStats.set(2, (int)tempArmor);
                }

                double shieldPercent = (tempShieldDurability/tempMaxShields) * 100;
                if(shieldPercent < 0) //if the shield percentage ever goes negative this will instantly set the shield percent to 0.
                {
                    shieldPercent = 0;
                }
                if(tempArmor<=0)
                {
                    atkText.setText("You've won the battle!");
                    buttonPanel.setVisible(false);
                }
                else
                {
                    atkText.setText("Enemy Armor: " + Math.round(tempArmor) + " \n" + "Enemy Shields: " + Math.round(shieldPercent) + "%");
                }
                c.gridx = 0;
                c.gridy = 0;
                textPanel.add(atkText,c);
                bFrame.setVisible(true);                            
            }
            else
            {
                System.out.println("Finished battle. You don't need to attack.");
                atkText.setText("You've won the battle.");
            }

            if(pStats.get(2) > 0)
            {
                double shieldAbsorb = (double)pStats.get(0);
                double shieldDurability = (double)pStats.get(1);
                double armor = (double)pStats.get(2);
                double attack = (double)eStats.get(3);
                double accuracy = (double)eStats.get(4);
                double maxShields = (double)pStats.get(5);

                double dmg = attack * (accuracy / 100); //takes the player's dmg and accuracy to determine how much damage it deals per turn.
                double armorNegationPercent = (double)(100/(100+armor));


                if(shieldDurability > 0) //makes sure the shields are up
                {
                    if(shieldAbsorb < dmg) //checks to see if the damage is greater than the shields can take
                    {
                        armor -= (dmg - shieldAbsorb) * armorNegationPercent; 
                        shieldDurability -= shieldAbsorb; //if the damage is greater than the shields absorbtion, the shields durability will be hit a max of the shields absorbtion, the rest goes to the armor
                    }                       
                    else if(shieldAbsorb>=dmg) //if the damage is less than the shields can take, the shield's durability will be hit by the damage
                    {
                        shieldDurability -= dmg;
                    }
                }
                else
                {
                    armor = armor - (dmg * armorNegationPercent); //if there is no more durability on the shields, the armor is hit harder
                }                                       

                shieldDurability = Math.round(shieldDurability);

                if(shieldDurability < 0) //If the tempShields is ever negative it will set it to 0.
                {
                    shieldDurability = 0;
                    pStats.set(1, 0);
                }
                else
                {
                    pStats.set(1, (int)shieldDurability);
                }
                if(armor < 0) //if the tempArmor is ever negative it will set it to 0.
                {
                    pStats.set(2, 0);
                    armor = 0;
                }
                else
                {
                    pStats.set(2, (int)armor);
                }

                double pShieldPercent = (shieldDurability/maxShields) * 100;
                if(pShieldPercent < 0) //if the shield percentage ever goes negative this will instantly set the shield percent to 0.
                {
                    pShieldPercent = 0;
                }
                if(armor<=0)
                {
                    dfsText.setText("You've lost this one!");
                    buttonPanel.setVisible(false);
                }
                else
                {
                    dfsText.setText("Ship Armor: " + Math.round(armor) + " \n" + "Shields: " + Math.round(pShieldPercent) + "%");
                }
                c.gridx = 0;
                c.gridy = 1;
                textPanel.add(dfsText,c);
                bFrame.setVisible(true);                            
            }
            else
            {
                System.out.println("Finished battle. You don't need to attack.");

            }
        }

    });

    fleeButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            c.gridx = 3;
            c.gridy = 3;
            textPanel.add(fleeText,c);
        }
    });
    c.gridx = 0;
    c.gridy = 3;
    buttonPanel.add(attackButton,c);
    c.gridx = 0;
    c.gridy = 1;
    buttonPanel.add(fleeButton,c);
    bFrame.getContentPane().add(mainPanel, BorderLayout.WEST);
    bFrame.getContentPane().add(textPanel, BorderLayout.EAST);
    bFrame.setVisible(false);
}
}
package测试;
导入java.util.*;
导入java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
公共级太空战
{
公共int营业额=1;
public int buttonCount=0;
公共JFrame bFrame=新JFrame(“太空战”);
公共太空战(ArrayList pStats、ArrayList eStats)
{       
JPanel mainPanel=新的JPanel();
setLayout(新的GridBagLayout());
JPanel buttonPanel=新的JPanel();
JPanel textPanel=新的JPanel();
GridBagConstraints c=新的GridBagConstraints();
c、 gridx=0;
c、 gridy=2;
主面板。添加(按钮面板,c);
B机架立根(颜色:黑色);
setLayout(新的BorderLayout());
JLabel turnNum=新JLabel(“转动”+转动次数);
c、 gridx=0;
c、 gridy=5;
主面板。添加(turnNum,c);
setFont(新字体(“衬线”,Font.BOLD,24));
b机架设置尺寸(800400);
bFrame.setVisible(真);
JButton attackButton=新JButton(“攻击”);
JLabel-atkText=新的JLabel();
JLabel dfsText=新的JLabel();
JButton fleeButton=新JButton(“逃离”);
JLabel fleeText=新的JLabel(“你从牙齿的皮肤里出来”);
维度dim=Toolkit.getDefaultToolkit().getScreenSize();
b帧设置位置(dim.width/2-bFrame.getSize().width/2,dim.height/2-bFrame.getSize().height/2);
attackButton.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
如果(eStats.get(2)>0)
{
turnCount++;
turnNum.setText(“转动”+转动次数);
//这大大提高了代码的可读性
double tempShieldAbsorb=(双)eStats.get(0);
double tempshield耐久性=(double)eStats.get(1);
double tempArmor=(double)eStats.get(2);
双时间攻击=(双)pStats.get(3);
双温度精度=(双)pStats.get(4);
double tempMaxShields=(double)eStats.get(5);
double tempDmg=tempAttack*(TempAccurance/100);//获取玩家的dmg和精确度,以确定其每回合造成的伤害。
双tempArmorNegationPercent=(双)(100/(100+tempArmorNegationPercent));
if(tempshieldtiminability>0)//确保防护罩已打开
{
if(tempshieldamble=tempDmg)//如果伤害小于护盾所能承受的,护盾的耐久性将受到伤害
{
TEMPSHIELD耐久性-=tempDmg;
}
}
其他的
{
temparmorne=temparmorne-(tempDmg*tempArmorNegationPercent);//如果护盾没有更多耐久性,护甲会受到更猛烈的打击
}                                       
Tempshield耐久性=数学圆(Tempshield耐久性);
if(tempshieldtiminability<0)//如果tempShields为负值,则将其设置为0。
{
tempshield耐久性=0;
集合(1,0);
}
其他的
{
设置(1,(内部)临时防护罩耐久性);
}
if(temparror<0)//如果temparror为负值,则将其设置为0。
{
组(2,0);
tempArmor=0;
}
其他的
{
设置(2,(内部)临时装甲);
}
双防护罩百分比=(临时防护罩耐久性/临时防护罩)*100;
if(shieldPercent<0)//如果屏蔽百分比为负值,则会立即将屏蔽百分比设置为0。
{
屏蔽百分比=0;
}
如果(0)
{
双屏蔽吸收=(双)pStats.get(0);
双屏蔽耐久性=(双)pStats.get(1);
双装甲=(双)pStats.get(2);
双重攻击=(双重)eStats.get(3);
双精度=(双)eStats.get(4);
双maxShields=(双)pStats.get(5);
double dmg=攻击*(精准度/100);//获取玩家的dmg和精准度,以确定其每回合造成的伤害。
双倍护甲率=(双倍)(100/(100+护甲));
如果(防护罩耐久性>0)//确保防护罩已打开
{
if(shieldAbsorb