2

私はJavaが初めてです。現在中央にある a を移動しようとしてJButtonいるので、場所を静的な場所に変更しましたが、移動していません。何か案が?

public Main(BufferedImage image) {
    this.image = image;
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    // Draw image centered.
    int x = (getWidth() - image.getWidth())/2;
    int y = 0;//(getHeight() - image.getHeight())/2;
    g.drawImage(image, x, y, this);
}

public static void main(String[] args) throws IOException {
    String path = "img/visualizerLogo3.jpg";
    BufferedImage image = ImageIO.read(new File(path));
    Main contentPane = new Main(image);
    contentPane.setOpaque(true);
    contentPane.setLayout(new GridBagLayout());
    JButton submit = new JButton("Load File");
    submit.setLocation(600, 800);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(contentPane);
    f.setSize(1200,1000);
    //f.setLocation(200,200);
    f.setVisible(true);
    f.add(submit);
}
4

1 に答える 1

5

GridBagLayout は JButton の位置を指示しています。自由に配置するには、コンテンツ ペインのレイアウトを null に設定する必要があります (デフォルトでは水平方向の FlowLayout です)。

public static void main(String[] args) throws IOException {
    String path = "img/visualizerLogo3.jpg";
    BufferedImage image = ImageIO.read(new File(path));
    Main contentPane = new Main(image);
    contentPane.setOpaque(true);
    contentPane.setLayout(null);
    JButton submit = new JButton("Load File");
    submit.setLocation(600, 800);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(contentPane);
    f.setSize(1200,1000);
    //f.setLocation(200,200);
    f.setVisible(true);
    f.add(submit);
}
于 2012-06-11T01:36:50.317 に答える