繁体   English   中英

在单独的类中将多个组件添加到另一个类中的JFrame

[英]Adding Multiple Components In Seperate Classes to JFrame In Another Class

我遇到了一个问题。 问题在于将多个组件添加到JFrame中,所有组件都在单独的类中。 我必须将两个组件DrawBoard和QuestionBox添加到Board类的JPanel“面板”中。 DrawBoard和QuestionBox都将执行不同的功能。

DrawBoard组件应为600x600像素,而QuestionBox组件应为600x120像素。 DrawBoard位于底部,QuestionBox位于顶部。 我不确定要使用哪种布局。

运行时,我得到这个结果。

运行时的结果

游戏类

package snake;

//This class is used to run the game.
public class Game {

    /**
     * @author HyperBlue
     */

    public static Board board;

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    //Creates an object board from the Board() construct
    board = new Board();

    }


}

董事会班

public class Board implements ActionListener {

    public DrawBoard drawBoard;
    public QuestionBox questionBox;

    public Timer ticker = new Timer(20, this);

    public Board() {

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        JFrame frame = new JFrame("Snake");
        frame.pack();
        Insets insets = frame.getInsets();
        JPanel container = new JPanel();

        questionBox = new QuestionBox();
        drawBoard = new DrawBoard();

        container.setLayout(new BorderLayout());

        container.add(questionBox, BorderLayout.NORTH);
        container.add(drawBoard, BorderLayout.SOUTH);

        frame.setMinimumSize(new Dimension(600+insets.left + insets.right, 720 +insets.bottom + insets.top));

        frame.add(container);

        //Sets the frame in middle of screen
        frame.setLocation((dim.width / 2) - (frame.getWidth() / 2), (dim.height / 2) - (frame.getHeight() / 2));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setVisible(true);
    }

抽屉板类

package snake;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

//Warnings will not be thrown (are suppressed).
@SuppressWarnings("serial")

public class DrawBoard extends JPanel{

    public static Color yellow = new Color(13816442);

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(yellow);
        g.fillRect(0, 0, 600, 600);
    }
}

QuestionBox类别

package snake;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class QuestionBox extends JPanel{

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 600, 120);
    }

}

每个组件都应负责管理其自身的大小,您应该首先覆盖面板的getPreferredSize并返回要使用的大小。

您也不应依靠幻数,而应使用实际的物理值,例如,

g.fillRect(0, 0, 600, 120);

你应该使用...

g.fillRect(0, 0, getWidth(), getHeight());

在此处输入图片说明

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test1 {

    public static void main(String[] args) {
        new Test1();
    }

    public Test1() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            add(new DrawBoard());
            add(new QuestionBox(), BorderLayout.SOUTH);
        }

    }

    public static class DrawBoard extends JPanel {

        public static Color yellow = new Color(13816442);

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 600);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(yellow);
            g.fillRect(0, 0, 600, 600);
        }
    }

    public static class QuestionBox extends JPanel {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(600, 120);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 600, 120);
        }

    }
}

您还应该知道Toolkit.getDefaultToolkit().getScreenSize(); 这不是确定可见屏幕区域的最可靠方法,因为它没有考虑可能占用屏幕空间的各种OS元素,例如任务栏或停靠栏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM