无法使用 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] 的大小?
this is my code:
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);
}
}
}
}
I want to resize one of the buttons.
I tried to write:
arr[10].setPreferredSize(new Dimension(20,40));
it did not work.
i tried to write:
arr[10].resize(10,20);
it did not work.
so how can i resize button arr[10]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试设置按钮的最小和最大尺寸。您使用的布局管理器可能无法支持首选尺寸。
Try setting the min and max size of the button. The layout manager you are using might not be able to honor the preferred size.
对于按钮面板,您指定了
GridLayout
,它忽略首选大小。作为替代方案,您可以使用
GridBagLayout
或
MigLayout
,可以跨列。附录:请注意,您可以在
GridLayout
。在您的示例中,这表示三列和所需的行数:For your button panel, you specified
GridLayout
, which ignores the preferred size. As an alternative, you can useGridBagLayout
orMigLayout
, which can span columns.Addendum: Note that you can indicate an arbitrary number of rows or columns in
GridLayout
. In your example, this would signify three columns with as many rows as required: