簡體   English   中英

如何在JFrame中放置1個JPanel和1個自定義JPanel

[英]How to put 1 JPanel and 1 Custom JPanel in a JFrame

我想在我的應用程序中並排放置2個JPanels。 一種將在右側包含有關我的自定義板的信息,而另一種將在左側繪制有關該自定義板的信息。 第一個JPanel是經典的,但是第二個是自定義面板。 似乎我在將自定義面板放入框架時遇到了問題。

我在gui類中創建了一個名為BoardPanel的類來繪制自定義板。 我不知道這是否是最好的方法。 我應該創建一個單獨的類嗎?

這是gui類的代碼:

public class BattleshipGUI extends JFrame {

    private BoardPanel mainPanel;


    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Create my frame
    ///////////////////////////////////////////////////////////////////////////////////////////////
    public BattleshipGUI() {

        JPanel container = new JPanel(new BorderLayout()); //the container panel that contains the 2 other panels

        mainPanel = new BoardPanel(); //main panel with my custom painting
        JPanel detailsPanel = new JPanel(new BorderLayout()); //secondary panel with various details about the game

        container.add(mainPanel, BorderLayout.CENTER); //add the 2 panels in the container
        container.add(detailsPanel, BorderLayout.EAST);

        this.add(container); //add container to my frame
        //this.setContentPane(container);



        this.setIconImage(Toolkit.getDefaultToolkit().getImage(BattleshipGUI.class.getResource("/resources/battleship_128.png")));
        this.setTitle("My Battleship Game");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //this.setBounds(100, 100, 850, 700);
        //this.pack();
        this.setSize(850, 600);

        this.setVisible(true);

    }

這是自定義繪畫的內部類的代碼

class BoardPanel extends JPanel {

        private static final int ROWS = 20;
        private static final int COLUMNS = 20;


        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            int sqSize = this.getHeight()/ROWS;

            for(int i=0; i<ROWS; i++) {
                for(int j=0; j<COLUMNS; j++) {
                    int x = j * sqSize;
                    int y = i * sqSize;
                    g.drawRect(x, y, sqSize, sqSize);

                }
            }

        }
    }

除了所有這些,我還有一個問題。 如果我要自定義繪畫,是否可以與WindowsBuilderPro一起使用? 我最初開始使用該工具。 但是,我看到我無法使用該工具繪制一些自定義內容,因此我不得不編寫代碼來做到這一點。 是否可以為自定義繪畫編寫代碼並同時出於不同目的使用該工具,例如添加簡單的文本標簽,甚至編輯該custon繪畫? 我想要看到的預期結果在運行程序時出現。 我的兩個面板的框架。 但是,當我打開WindowsBuilderPro時,我的自定義面板沒有出現,結果有點錯誤。 這就是為什么我對自己的方法以及是否可以編寫代碼並同時使用該工具存在疑問的原因。 謝謝您,很抱歉給您帶來冗長的文字。 我對此很困惑。

擱置

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

public class Test {

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

    public Test() {
        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 class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = gbc.BOTH;
            gbc.gridx = 0;
            gbc.gridy = 0;

            JPanel filler = new JPanel() {
                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(200, 300);
                }
            };
            filler.setBackground(Color.BLUE);

            add(filler, gbc);
            gbc.gridx++;
            add(new BoardPanel(), gbc);
        }

    }

    class BoardPanel extends JPanel {

        private static final int ROWS = 20;
        private static final int COLUMNS = 20;
        private int sqSize = 20;

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(COLUMNS * sqSize, ROWS * sqSize);
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            for (int i = 0; i < ROWS; i++) {
                for (int j = 0; j < COLUMNS; j++) {
                    int x = j * sqSize;
                    int y = i * sqSize;
                    g.drawRect(x, y, sqSize, sqSize);

                }
            }

        }
    }

}

花些時間通讀容器中的布局組件 ,以更好地了解布局管理API的工作原理

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM