【问题标题】:Adding a JPanel to another JPanel having TableLayout将 JPanel 添加到另一个具有 TableLayout 的 JPanel
【发布时间】:2011-02-25 17:06:18
【问题描述】:

我正在尝试使用TableLayout 在 Java 中开发地图编辑器。我的地图窗口接收一个 Map 对象作为构造函数。从该地图对象中,我能够检索网格和网格中的每个项目以及其他 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);
    }
}

【问题讨论】:

  • 你有没有尝试不覆盖paint方法来查看它是否被渲染?
  • 同样的事情。我什至无法在普通的 JPanel 中绘制它。所以我猜映射类搞砸了。
  • 添加了链接;如果不正确,请恢复。

标签: java map mapping


【解决方案1】:

默认情况下,JComponent 没有首选大小。因此,当您将其添加到另一个面板时,就没有什么可绘制的了。

调用 drawImage() 方法不会为组件指定大小。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    相关资源
    最近更新 更多