背景画像があるJFrameと、いくつかのコマンドを実行する画像JButtonsを修正しようとしています。JFrameの特定の場所に小さなボタンを配置したいので、レイアウトなしでやろうとしましたが、毎回背景画像が前面に来るか、JFrameのサイズがJFrameのサイズと同じです。次のコードでは、JButton は JFrame と同じサイズになります。JButton のサイズと位置を変更しようとしましたが、何もしませんでした。助けてください。
ここにコードがあります
public final class Test extends JComponent
{
private Image background;
private JFrame frame;
private Dimension dimension;
public Test()
{
dimension = new Dimension(15, 15);
frame = new JFrame("Iphone");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.setBounds(641, 0, 344, 655);
frame.setVisible(true);
test = displayButton("tigka");
frame.getContentPane().add(test);
}
public void update(Graphics g)
{
paint(g);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawImage(background, 0, 25, null); // draw background
// label();
test = displayButton("test");
}
public JButton displayButton(String name)
{
JButton button = new JButton(name);
button.setSize(100, 100);
button.setPreferredSize(dimension);
return button;
}