如何重叠Java中的组件?

时间:2017-01-09 14:45:07

标签: java swing

import javax.swing.*;
import java.awt.*;

public class TestBracket extends JComponent
{
    //Constructor
    public TestBracket ()
    {       
        super ();
        this.setPreferredSize(new Dimension(1000, 1000));
    }

    // Paint a Tournament Bracket
    public void paintComponent(Graphics g)
    {   
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        /**The First Pair*/
        g2.drawRect(100, 75, 150, 50);
        g2.drawRect(100, 125, 150, 50);

        g2.drawRect(100, 250, 150, 50);
        g2.drawRect(100, 300, 150, 50);

        g2.drawLine(100, 125, 400, 125); //Links the winner from the 1st
        g2.drawLine(100, 300, 400, 300); //Links the winner from the 2nd

        //Linking the two lines
        g2.drawLine(400, 125, 400, 150);
        g2.drawLine(400, 300, 400, 150);

        //The connector to the winners of the first pair
        g2.drawLine(400, 200, 420, 200);;

        /**The Second Pair*/
        g2.drawRect(100, 500, 150, 50);
        g2.drawRect(100, 550, 150, 50);

        g2.drawRect(100, 675, 150, 50);
        g2.drawRect(100, 725, 150, 50);

        g2.drawLine(100, 550, 400, 550); //Links the winner from the 1st
        g2.drawLine(100, 725, 400, 725); //Links the winner from the 2nd

        //Linking the two lines
        g2.drawLine(400, 550, 400, 550);
        g2.drawLine(400, 725, 400, 550);

        //The Connector to the winners of the second pair
        g2.drawLine(400, 625, 420, 625);

        /**The Third Pair*/
        g2.drawRect(420, 150, 150, 50);
        g2.drawRect(420, 200, 150, 50);

        //Line from the pair that will connect to the fourth pair
        g2.drawLine(420, 200, 650, 200);

        /**The Fourth Pair*/
        g2.drawRect(420, 575, 150, 50);
        g2.drawRect(420, 625, 150, 50);

        //Line from the pair that will connect to the third pair
        g2.drawLine(420, 625, 650, 625);

        /**Connecting the Third and Fourth Pair */
        g2.drawLine(650, 200, 650, 625);

        /**Line to Connect to the Grand Final Box */
        g2.drawLine(650, 388, 670, 388);

        /**The Grand Final Box */
        g2.drawRect(670, 338, 150, 50);
        g2.drawRect(670, 388, 150, 50);

        /**The Line That Separates The Bracket from the Player Names*/

        //g2.drawLine(850, 50, 850, 900);

    }  

}


import javax.swing.*;
import java.awt.*;

public class TestBracketTest {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        JPanel panel = new JPanel();

        JPanel name = new JPanel(null);
        JLabel label = new JLabel("HI");
        label.setOpaque(true);
        label.setLocation(0, 0);
        name.add(label);

        TestBracket brack = new TestBracket();
        panel.add(smile);
        panel.add(label);

        frame.setContentPane(panel);
        frame.setTitle("Tournament Bracket");
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(200, 100);
        frame.setSize(1500, 1000);


    }

}

我正在尝试将JLabel重叠到我的支架视觉中的一个方框中,但它总是把它放在一边。有什么帮助吗?

0 个答案:

没有答案