Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Gridbag布局和绘图

我正在做一个小游戏,一开始我想用JCheckBox来选择语言(之后他们很少有人来设置游戏),然后在上面画一个jlabel带有游戏名称的图片,或者在那里画一张图片,问题是,我不知道其他任何方法如何用复选框将面板居中,然后使用GridBagLayout。当我使用此方法时,我无法在框架上绘制任何内容,如果可能的话,我也想删除复选框周围的灰线,感谢您的帮助,谢谢

这是我的第二个问题,我还不能添加图片,所以这里有一个图片链接:

here is the picture

下面是框架的代码

private GamePlan plan;
private JFrame frame;
private String language;
private JPanel panel;
private JCheckBox englishBox;
private JCheckBox germanBox;

public Settings(GamePlan plan){
    this.plan = plan;
    frame = new JFrame();
    frame.setSize(600, 500);
    frame.setLocation(200, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridBagLayout());
    frame.setResizable(false);
    frame.setVisible(true);
    panel = new JPanel(new GridLayout(2, 1));
    englishBox = new JCheckBox("English", false);
    germanBox = new JCheckBox("German", false);
    englishBox.addActionListener(new EnglishLanguage());
    germanBox.addActionListener(new GermanLanguage());
    panel.add(englishBox);
    panel.add(germanBox);
    englishBox.setOpaque(false);
    germanBox.setOpaque(false);
    panel.setOpaque(false);
    frame.add(panel);
    frame.getContentPane().setBackground(new Color(216,252,202));
}

共 (1) 个答案

  1. # 1 楼答案

    " the problem is that i dont know any other way how to center the panel with checkboxes then to use GridBagLayout and when i use this, i cannot draw anything to the frame"

    如果没有一个完整的例子,我真的说不出你做错了什么。我甚至不知道你想在哪里添加图像。但不要试图在画框上画画。取而代之的是画一个JPanel

    下面是一个例子,你可以从中获得一些见解

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.GridBagLayout;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.border.TitledBorder;
    
    public class ImageByDrawing {
        public ImageByDrawing() {
            ImagePanel imagePanel = new ImagePanel();
            imagePanel.setBorder(new TitledBorder("Drawn Image onto JPanel"));
    
            JCheckBox germanBox = new JCheckBox("German");
            germanBox.setOpaque(false);
            JCheckBox englishBox = new JCheckBox("English");
            englishBox.setOpaque(false);
            JPanel boxPanel = new JPanel();
            boxPanel.setBorder(new TitledBorder("JPanel with default FlowLayout"));
            boxPanel.setOpaque(false);
            boxPanel.add(germanBox);
            boxPanel.add(englishBox);
    
            JPanel centerPanel = new JPanel(new BorderLayout());
            centerPanel.add(imagePanel, BorderLayout.CENTER);
            centerPanel.add(boxPanel, BorderLayout.SOUTH);
            centerPanel.setBorder(new TitledBorder("JPanel with BorderLayout"));
            centerPanel.setOpaque(false);
    
            JPanel mainPanel = new JPanel(new GridBagLayout());
            mainPanel.add(centerPanel);
            mainPanel.setBorder(new TitledBorder("JPanel with GridBagLayout"));
            mainPanel.setBackground(new Color(216,252,202));
    
            JFrame frame = new JFrame();
            frame.add(mainPanel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(600, 600);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public class ImagePanel extends JPanel {
            BufferedImage img;
            int dWidth;
            int dHeight;
            public ImagePanel() {
                try {
                    img = ImageIO.read(getClass().getResource("/resources/stackblack.jpg"));
                    dWidth = img.getWidth();
                    dHeight = img.getHeight();
                } catch (IOException ex) {
                    Logger.getLogger(ImageByDrawing.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); 
                g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
            }
            @Override
            public Dimension getPreferredSize() {
                return (img == null) ? new Dimension(300, 300) : new Dimension(dWidth, dHeight);
            }
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    new ImageByDrawing();
                }
            });
        }
    }
    

    我也不知道你为什么喜欢画画。用JLabelImageIcon也可以很容易地做到这一点

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridBagLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.border.TitledBorder;
    
    public class ImageByDrawing {
    
        public ImageByDrawing() {
            ImageIcon icon = new ImageIcon(getClass().getResource("/resources/stackblack.jpg"));
            JLabel label = new JLabel(icon);
            label.setBorder(new TitledBorder("JLabel with ImageIcon"));
    
            JCheckBox germanBox = new JCheckBox("German");
            germanBox.setOpaque(false);
            JCheckBox englishBox = new JCheckBox("English");
            englishBox.setOpaque(false);
            JPanel boxPanel = new JPanel();
            boxPanel.setBorder(new TitledBorder("JPanel with default FlowLayout"));
            boxPanel.setOpaque(false);
            boxPanel.add(germanBox);
            boxPanel.add(englishBox);
    
            JPanel centerPanel = new JPanel(new BorderLayout());
            centerPanel.add(label, BorderLayout.CENTER);
            centerPanel.add(boxPanel, BorderLayout.SOUTH);
            centerPanel.setBorder(new TitledBorder("JPanel with BorderLayout"));
            centerPanel.setOpaque(false);
    
            JPanel mainPanel = new JPanel(new GridBagLayout());
            mainPanel.add(centerPanel);
            mainPanel.setBorder(new TitledBorder("JPanel with GridBagLayout"));
            mainPanel.setBackground(new Color(216, 252, 202));
    
            JFrame frame = new JFrame();
            frame.add(mainPanel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(600, 600);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new ImageByDrawing();
                }
            });
        }
    }
    

    问题的最后一部分,正如@Jere所指出的,您可以使用setFocusPainted作为复选框germanBox.setFocusPainted(false);