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

时间:2014-12-31 22:29:12

标签: java swing layout jpanel components

我遇到了一个问题。问题在于向JFrame添加多个组件,所有组件都在不同的类中。我必须将两个组件DrawBoard和QuestionBox添加到JPanel'面板中。在董事会班。 DrawBoard和QuestionBox都将执行不同的功能。

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

跑步时我得到这个结果。

The result when ran

游戏课

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);
    }

DrawBoard Class

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);
    }

}

1 个答案:

答案 0 :(得分:2)

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

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

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

你应该使用......

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

enter image description here

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元素,如任务栏或停靠栏