Javaの道 Javaに関する
 ニュースJava基本Servlet・JSPオープンソースFAQ掲示板
Javaの道 >  掲示板 >  掲示板(同じように9個のボタンを作ったのに、なぜか最後の1つだけ大きくなっている。)
閲覧数:967
掲示板(同じように9個のボタンを作ったのに、なぜか最後の1つだけ大きくなっている。)
名前
匿名
題名 同じように9個のボタンを作ったのに、なぜか最後の1つだけ大きくなっている。
質問内容

質問を評価する
(0ポイント)
Eclipse 4.2 Juno Pleiades All in Oneを使っています。

JFrame上にJInternalFrameを配置し、その中に9個のJButtonを配置しています。
JButtonにはそれぞれ60×60のgif画像をアイコンとして設定しています。
タイル状に3×3に並べることからもfor文でアイコンやサイズ・位置を決めています。
余白を0にしてボタンサイズを60×60に設定しているのですが、最後の1つだけどう見ても60×60ではありません。
不思議に思いそのボタンの位置とサイズを出力させてみましたが、どちらも私がfor文で設定したとおりでした。
なぜ他のボタンは問題ないのにそのボタンだけこのようになっているのでしょうか?
ボタンを押したときの動作は、どのボタンを押しても私が設定したとおりの動作をすることを確認しています。

//ソースコードをすべて載せてみます。
package ExFrame3_2;

import java.awt.Color;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

public class ExFrame3_2 extends JFrame{

    static JInternalFrame pad;
    static JButton[][] padButton=new JButton[3][3];
    static int width,height;

    public ExFrame3_2(){
        super("ExFrame_3");
        Container container;
        JDesktopPane desktop;
        container=getContentPane();
        desktop=new JDesktopPane();
        container.add(desktop);
        setSize(450,450);
        //
        pad=new JInternalFrame("操作版",false,false,false,false);
        pad.setDefaultCloseOperation(pad.DO_NOTHING_ON_CLOSE);
        pad.setVisible(true);
        pad.getContentPane().setBackground(new Color(50,128,128));
        width=0;
        height=0;
        for(int x=0;x<3;x++){
            for(int y=0;y<3;y++){
                Icon icon=new ImageIcon("img\\icon"+x+"_"+y+".gif");
                padButton[x][y]=new JButton(icon);
                padButton[x][y].setMargin(new Insets(0,0,0,0));
                padButton[x][y].setSize(60, 60);
                padButton[x][y].setLocation(x*60, y*60);
                if(x==2 && y==2){
                    System.out.println(padButton[2][2].getLocation());
                    System.out.println(padButton[2][2].getSize());
                }
                padButton[x][y].addActionListener(lsn);
                pad.getContentPane().add(padButton[x][y]);
                width=width+padButton[x][y].getSize().width;
                height=height+padButton[x][y].getSize().height;
            }
        }
        desktop.add(pad);

        addWindowListener(new WindowEventHandler());
        setVisible(true);
        setVisible(false);
        width=width+pad.getInsets().left+pad.getInsets().right;
        height=height+pad.getInsets().top+pad.getInsets().bottom;
        System.out.println("("+width+","+height+")");
        pad.setSize(width,height);
        pad.setLocation(width,height);
        setSize(width*3+getInsets().left+getInsets().right,height*3+getInsets().top+getInsets().bottom);
        setVisible(true);
    }

    static ActionListener lsn =new ActionListener(){
        public void actionPerformed(ActionEvent e){
            for(int x=0;x<3;x++){
                for(int y=0;y<3;y++){
                    if(e.getSource()==padButton[x][y]){
                        pad.setLocation(x*width,y*height);
                    }
                }
            }
        }
    };

    class WindowEventHandler extends WindowAdapter{
        public void WindowClosing(WindowEvent e){
            System.exit(0);
        }
    }

    public static void main(String args[]){
        new ExFrame3_2();
    }

}
質問日時 2013-04-26 00:52:32
名前
匿名
回答内容

回答を評価する
(10ポイント)
つ Layout
回答日時 2013-04-27 11:17:57

質問から6ヶ月以上経過しているので、回答を書き込むことはできません。



このページのトップへ
 ニュースJava基本Servlet・JSPオープンソースFAQ掲示板
Javaの道_CopyrightJavaの道