如何在另一个JPanel中管理不同的JPanels

时间:2019-07-18 12:18:15

标签: java jpanel

我目前正在开发(或至少尝试使用)GUI的Blackjack程序。在我的JFrame窗口中,我尝试组织JPanel部分,以使它们每个都仅包含一个或多个相同类型的元素(例如:JLabel或按钮)。问题是我不确定我是否完全理解了JPanel。对我来说,它有点像CSS中的一个元素,但事实并非如此。我的代码有些局限,因为其中一个JPanels包含另外两个JPanels。我希望这两个面板位于(BorderLayout.North和BorderLayout.South)中。您能帮我了解JPanel的工作原理,以及如何改进我的代码以使其正常工作吗?

我已经拥有三个主要的JPanels(样式,style2,style3),它们已经成功添加到JFrame窗口(测试)中,并且在其中也包含了一些按钮和标签。这是我的代码:

package com;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Windows extends JFrame{

JFrame test = new JFrame();
JLabel text = new JLabel("Welcome in this Blackjack game");
JLabel textAction = new JLabel("Here are your cards");
JPanel style = new JPanel();
JPanel style2 = new JPanel();
//sub JPanels
JPanel style2a = new JPanel();
JPanel style2b = new JPanel();
//sub sub JPanels
JPanel style2b1 = new JPanel();
JPanel style2b2 = new JPanel();
//
JPanel style3 = new JPanel();
JButton b1 = new JButton("Stand");
JButton b2 = new JButton("Hit");
JButton b3 = new JButton("Double");
JButton b4 = new JButton("Split");
JButton b5 = new JButton("Insurance");
Font font = new Font("Helvetica", 10, 20);

public Windows(){
   style.setBackground(Color.BLACK);
   style2.setBackground(Color.PINK);

   style2a.setBackground(Color.CYAN);
   style2b.setBackground(Color.BLUE);
   style2b.setSize(500, 500);
   style2b1.setBackground((Color.RED));
   style2b2.setBackground(Color.YELLOW);
   style3.setBackground(Color.BLACK);
   style3.add(b1);
   style3.add(b2);
   style3.add(b3);
   style3.add(b4);
   style3.add(b5);
   text.setForeground(Color.WHITE);
   text.setFont(font);
   style.add(text);
   textAction.setFont(font);
   textAction.setForeground(Color.WHITE);
   style2a.add(textAction);

   b1.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to stand. You won't receive any more cards.");
       }
   });
   b2.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to hit. You will receive another card");
       }
   });
   b3.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to double. You will receive one last card and you have to double your bet");
       }
   });
   b4.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to split. Each deck of card will be considered as individual, however you have to double your bet ");
       }
   });
   b5.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
           textAction.setText("You choose to raise an insurance. Please double your bet.");
       }
   });


   test.add(style, BorderLayout.PAGE_START);
   test.add(style2, BorderLayout.CENTER);
   style2.add(style2a, BorderLayout.NORTH);
   style2.add(style2b, BorderLayout.SOUTH);
   test.add(style3, BorderLayout.PAGE_END);

   test.setSize(1000, 1000);
   test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   test.setVisible(true);
   test.setLocation(500, 0);
   test.setResizable(false);
}

}

我希望将JPanels style2a(CYAN)和style2b(BLUE)放在一列而不是图片中的一行中 My code so far

0 个答案:

没有答案