1

私がやりたいことは、ボタンをアプリケーションの左下に置くことです。誰かがそれを行う方法の例を教えてもらえますか?

これは私が持っているものです:

アプリケーションイメージ

これが私のコードです:

        super("Test");

    /**Create Components**/
    JPanel addPanel = new JPanel();
    JButton addButton= new JButton("Add");

    /**Add Components**/
    addPanel.add(addButton);
    this.add(addPanel);

    /**Set Components Properties**/
    addButton.setLocation(12, 371);
    addButton.setPreferredSize(new Dimension(116, 40));
    addPanel.setLocation(12, 371);
    addPanel.setPreferredSize(new Dimension(116, 40));

    /**Frame Properties**/
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setPreferredSize(new Dimension(dimension1, dimension2));
    this.setResizable(false);   
    this.pack();
    this.setVisible(true);
4

2 に答える 2

8

nullまず、JFrame を使用している場合はフレームのレイアウトを に設定し、パネルnullを使用している場合はレイアウトのパネルを に設定してから、setBounds()メソッドを使用します。

button.setBounds(x,y,width,height);

私があなたのために作ったこの例を見てください:

import javax.swing.*;
import java.awt.*;
public class ButtonLocationDemo extends JFrame{

 private JButton button;
 public ButtonLocationDemo(){
      JPanel p = new JPanel();
      button = new JButton("Button");
      p.setLayout(null);
      button.setBounds(40,100,100,60);
      p.add(button);

      getContentPane().add(p);
      //setLayout(null);
      setDefaultCloseOperation(3);
      setSize(400,400);
      setVisible(true);

     }
   public static void main(String...args){
       new ButtonLocationDemo();
       }
}
于 2013-05-26T07:01:33.340 に答える
6

BorderLayout を試す

addPanel.setLayout(new BorderLayout());
addPanel.add(addButton,BorderLayout.SOUTH);

addPanel の内部でも、 Grid Layoutを使用して別のパネル (たとえば、bottomLeft) を使用できます。

bottomLeft.setLayout(new GridLayout(1,3,200,0));
bottomLeft.add(addPanel)
于 2013-05-26T07:02:16.143 に答える