如何添加两个组件,使它们彼此独立?

时间:2016-09-24 03:26:12

标签: java layout jframe layout-manager

所以我正在创建一个屏幕,屏幕周围会弹跳。我希望将文本放在屏幕的中央,而不会破坏弹跳球的物理特性。我的解决方案是有两个独立的布局管理器(一个用于球,一个用于文本),但它会弄乱整个面板。这是我的代码:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.util.Random;

    import javax.swing.JFrame;
    import javax.swing.JPanel;

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

        static Random random = new Random();

        static int max       = 370;
        static int min       = 0;

        static int x         = min + random.nextInt(max - min + 1);
        static int y         = min + random.nextInt(max - min + 1);

        static int num       = min + random.nextInt(max - min + 1);
        static int xValues[] = {num,num,num,num,num,num,num,num,num};
        static int yValues[] = {num,num,num,num,num,num,num,num,num};

        static int xa[]      = {1,-1,1,-1,1,-1,1,-1,1};
        static int ya[]      = {1,-1,1,-1,1,-1,1,-1,1};

        public static void main(String[] args) throws InterruptedException {

            JFrame frame     = new JFrame("Mini Tennis");
            Game04 game      = new Game04();
            frame.setLayout(new BorderLayout());
            frame.add(game);

            frame.setSize(400,400);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            for(int i = 0; i < 9; i++) {
                num = min + random.nextInt(max - min + 1);
                xValues[i] = num;
                num = min + random.nextInt(max - min + 1);
                yValues[i] = num;
            }

            while(true) {
                game.moveBall();
                game.repaint();
                Thread.sleep(30);
            }
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);

            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Sets anti-antialiasing 
                    RenderingHints.VALUE_ANTIALIAS_ON);           // for drawing the tool

            for(int i = 0; i < 9; i++) {
                g2d.setColor(Color.GREEN);
                g2d.fillOval(xValues[i],yValues[i],30,30);
            }
        }

        private void moveBall() {

            for(int i = 0; i < 9; i++) {
                if(xValues[i] + xa[i] < 0)
                    xa[i] = 1;
                if(xValues[i] + xa[i] > getWidth() - 30) // 30px is size of the oval object
                    xa[i] = -1;
                if(yValues[i] + ya[i] < 0)
                    ya[i] = 1;
                if(yValues[i] + ya[i] > getHeight() - 30) // 30px is size of the oval object
                    ya[i] = -1;

                xValues[i] = xValues[i] + xa[i];
                yValues[i] = yValues[i] + ya[i];

            }
        }
    }

以下是我尝试添加的代码,用于在屏幕上创建文本:

        JTextArea quoteText = new JTextArea();
        quoteText.setText("Default Text");

        JPanel borderPane = new JPanel();
        borderPane.add(quoteText, BorderLayout.CENTER);

        JPanel masterPane = new JPanel();

        masterPane.add(game, null);
        masterPane.add(borderPane, BorderLayout.CENTER);
        frame.add(masterPane);
唉,它搞砸了一切。我知道有更好的解决方案。

2 个答案:

答案 0 :(得分:1)

我建议使用覆盖Graphics方法的paint()对象渲染球的方式与渲染球的方式相同。它有drawString方法,可以在不添加全新组件和中断布局的情况下实现所需效果。

答案 1 :(得分:1)

使用构造函数添加JTextArea

  public Game04() {

        //layout manager will is used for JTextArea only
        setLayout(new GridBagLayout());
        JTextArea quoteText = new JTextArea();
        quoteText.setText("Default Text");
        add(quoteText);
  }


+1对于格式正确的问题。
与你无关的问题:我会将代码的结构更改为:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class Game04 extends JFrame {

    static Random random = new Random();

    static int max       = 370;
    static int min       = 0;

    static int x         = min + random.nextInt((max - min) + 1);
    static int y         = min + random.nextInt((max - min) + 1);

    static int num       = min + random.nextInt((max - min) + 1);
    static int xValues[] = {num,num,num,num,num,num,num,num,num};
    static int yValues[] = {num,num,num,num,num,num,num,num,num};

    static int xa[]      = {1,-1,1,-1,1,-1,1,-1,1};
    static int ya[]      = {1,-1,1,-1,1,-1,1,-1,1};

    Game04(){

        super("Mini Tennis");

        Game04Panel game  = new Game04Panel();
        setLayout(new BorderLayout());
        add(game, BorderLayout.CENTER);

        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

        for(int i = 0; i < 9; i++) {
            num = min + random.nextInt((max - min) + 1);
            xValues[i] = num;
            num = min + random.nextInt((max - min) + 1);
            yValues[i] = num;
        }

        while(true) {
            game.moveBall();
            game.repaint();

            try {
                Thread.sleep(30);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {

        new Game04();
    }

    class Game04Panel extends JPanel {

        public Game04Panel() {

            //layout manager will is used for JTextArea only
            setLayout(new GridBagLayout());
            JTextArea quoteText = new JTextArea();
            quoteText.setText("Default Text");
            add(quoteText);
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);

            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // Sets anti-antialiasing
                    RenderingHints.VALUE_ANTIALIAS_ON);           // for drawing the tool

            for(int i = 0; i < 9; i++) {
                g2d.setColor(Color.GREEN);
                g2d.fillOval(xValues[i],yValues[i],30,30);
            }
        }

        private void moveBall() {

            for(int i = 0; i < 9; i++) {
                if((xValues[i] + xa[i]) < 0) {
                    xa[i] = 1;
                }
                if((xValues[i] + xa[i]) > (getWidth() - 30)) {
                    xa[i] = -1;
                }
                if((yValues[i] + ya[i]) < 0) {
                    ya[i] = 1;
                }
                if((yValues[i] + ya[i]) > (getHeight() - 30)) {
                    ya[i] = -1;
                }

                xValues[i] = xValues[i] + xa[i];
                yValues[i] = yValues[i] + ya[i];

            }
        }
    }
}