繁体   English   中英

将JPanel添加到另一个具有TableLayout的JPanel

[英]Adding a JPanel to another JPanel having TableLayout

我正在尝试使用TableLayout在Java中开发地图编辑器。 我的地图窗口收到一个Map对象作为构造函数。 从该地图对象中,我能够检索Grid和网格中的每个项目以及其他getter和setter。 问题是,即使Mapping扩展了JComponent,当我将其放置在面板中时,它也不会被绘制。 我已重写油漆方法以满足我的需要。 这是代码,也许您可​​以帮助我。

public class MapTest extends JFrame implements ActionListener {

    private JPanel mainPanel;
    private JPanel mapPanel;
    private JPanel minimapPanel;
    private JPanel relationPanel;
    private TableLayout tableLayout;
    private JPanel tile;

    MapTest(Map map) {
        mainPanel = (JPanel) getContentPane();
        mapPanel = new JPanel();
        populateMapPanel(map);
        mainPanel.add(mapPanel);
        this.setPreferredSize(new Dimension(800, 600));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    private double[][] generateTableLayoutSize(int x, int y, int size) {
        double panelSize[][] = new double[x][y];
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                panelSize[i][j] = size;
            }
        }
        return panelSize;
    }

    private void populateMapPanel(Map map) {
        double[][] layoutSize = generateTableLayoutSize(map.getMapGrid().getRows(), map.getMapGrid().getColumns(), 50);
        tableLayout = new TableLayout(layoutSize);

        for(int i = 0; i < map.getMapGrid().getRows(); i++)
        {
            for(int j = 0; j < map.getMapGrid().getColumns(); j++)
            {
                tile = new JPanel();
                tile.setName(String.valueOf(((Mapping)map.getMapGrid().getItem(i, j)).getCharacter()));
                tile.add(map.getMapItem(i, j));
                String constraint = i + "," + j;
                mapPanel.add(tile, constraint);
            }
        }
        mapPanel.validate();
        mapPanel.repaint();
    }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
} 

我的地图课程

public class Mapping extends JComponent implements Serializable{

    private BufferedImage image;
    private Character character;

    //default
    public Mapping() {
        super();
        this.image = null;
        this.character = '\u0000';
    }

    //Mapping from image and char
    public Mapping(BufferedImage image, char character) {
        super();
        this.image = image;
        this.character = character;
    }

    //Mapping from file and char
    public Mapping(File file, char character) {
        try {
            this.image = ImageIO.read(file);
            this.character = character;
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }

    public char getCharacter() {
        return character;
    }

    public void setCharacter(char character) {
        this.character = character;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
        repaint();
    }

    @Override
    /*Two mappings are consider the same if
    -they have the same image OR
    -they have the same character OR
    -both of the above*/
    public boolean equals(Object mapping) {
        if (this == mapping) {
            return true;
        }
        if (mapping instanceof Mapping) {
            return true;
        }
        //WARNING! equals might not work for images
        return (this.getImage()).equals(((Mapping) mapping).getImage())
                || (this.getCharacter()) == (((Mapping) mapping).getCharacter());
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //g.drawImage(image, 0, 0, null);
        g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
    }

//    @Override
//    public Dimension getPreferredSize() {
//        if (image == null) {
//            return new Dimension(10, 10); //instead of 100,100 set any prefered dimentions
//        } else {
//            return new Dimension(100, 100);//(image.getWidth(null), image.getHeight(null));
//        }
//    }
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
        character = (Character) in.readObject();
        image = ImageIO.read(ImageIO.createImageInputStream(in));
    }

    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        out.writeObject(character);
        ImageWriter writer = (ImageWriter) ImageIO.getImageWritersBySuffix("jpg").next();
        writer.setOutput(ImageIO.createImageOutputStream(out));
        ImageWriteParam param = writer.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(0.85f);
        writer.write(null, new IIOImage(image, null, null), param);
    }
}

默认情况下,JComponent没有首选大小。 因此,当您将其添加到另一个面板时,没有任何内容可以绘画。

调用drawImage()方法不会为组件提供大小。

我不知道TableLayout的工作方式,但是我会尝试设置组件的首选大小,然后我猜TableLayout能够正常工作。

或者,也许您应该使用GridLayout,它将根据可用的总空间自动调整每个网格的大小。

暂无
暂无

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

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