2010-12-21 25 views
-2

調整大小的按鈕,這是我的代碼:無法使用java.awt中

import java.applet.Applet; 
import java.awt.BorderLayout; 
import java.awt.Button; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.Panel; 
import java.awt.TextField; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 


public class Maapp extends Applet implements ActionListener 
{ 
    private int flag=0; 
    Panel p1=new Panel(); 
    Panel p2=new Panel(); 
    Button[] arr=new Button[12]; 

    TextField textf=new TextField("",25); 
    public void init() 
    { 
     this.setLayout(new BorderLayout()); 
     this.p2.setBackground(Color.DARK_GRAY); 
     this.p2.setLayout(new GridLayout(2,30)); 
     textf.setBackground(Color.BLACK); 
     textf.setForeground(Color.YELLOW); 
     this.p1.setLayout(new GridLayout(4,10)); 
     p2.add(textf); 
     for(int i=0; i<10 ;i++) 
     { 

      arr[i]=new Button(""+i); 
      arr[i].setForeground(Color.WHITE); 
      arr[i].setBackground(Color.DARK_GRAY); 
      p1.add(arr[i]); 
      this.arr[i].addActionListener(this); 
     } 
      arr[10]=new Button("="); 
      p1.add(arr[10]); 
      arr[10].setPreferredSize(new Dimension(20,40)); 
      this.arr[10].addActionListener(this); 


     this.add(p2,BorderLayout.NORTH); 
     this.add(p1,BorderLayout.CENTER); 
    } 





    public void actionPerformed(ActionEvent arg0) 
    { 

     for(int i=0;i<10;i++) 
     { 
      if(arg0.getSource()==arr[i]) 
      { 
       this.textf.setText(this.textf.getText()+i); 
      } 


     } 
    } 
} 

我想調整的一個按鈕。 我試着寫:

arr[10].setPreferredSize(new Dimension(20,40)); 

沒有奏效。 我試圖寫:

arr[10].resize(10,20); 

它沒有工作。

那麼我怎樣才能調整大小按鈕arr[10]

+4

已更改標題,現在好些了嗎? – GvS 2010-12-21 16:54:49

回答

1

嘗試設置按鈕的最小和最大尺寸。您正在使用的佈局管理器可能無法遵守首選大小。

+0

我是新的java ...我怎麼可以最小或最大的按鈕大小? – 2010-12-21 16:57:58

+0

使用API​​。查找setMinimumSize方法以及如何使用它。 – jzd 2010-12-21 16:59:11

1

對於您的按鈕面板,您指定GridLayout,忽略首選大小。作爲替代,您可以使用GridBagLayoutMigLayout,它可以跨越列。

附錄:請注意,您可以在GridLayout中指示任意行數列。在你的例子中,這將表示三列所需的行數:

this.p1.setLayout(new GridLayout(0, 3));