如何在java awt的框架中添加按钮和面板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23032879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add buttons and panels in frame in java awt
提问by Vaishal
How to make java awt program in which first line contains text-field,next 5 lines contain 5 buttons each and next 4 lines contain 4 buttons each. And how to set the size of those buttons and space between them ? I have tried this using 3 panels but not working.
如何制作java awt程序,其中第一行包含文本字段,接下来的5行每行包含5个按钮,接下来的4行每行包含4个按钮。以及如何设置这些按钮的大小和它们之间的空间?我已经使用 3 个面板尝试过这个,但没有用。
(sample program made by me but is not showing anything)
(我制作的示例程序,但没有显示任何内容)
`import java.awt.*;
class cal extends Frame {
cal(){
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
p1.setLayout(new GridLayout(2,3));
p2.setLayout(new GridLayout(2,2));
TextField k=new TextField("0",20);
Button a=new Button("HI");
Button b=new Button("HI");
Button c=new Button("HI");
Button d=new Button("HI");
Button e=new Button("HI");
Button l=new Button("Hello");
Button g=new Button("Hello");
Button h=new Button("Hello");
Button i=new Button("Hello");
p1.add(a);
p1.add(b);
p1.add(c);
p1.add(d);
p1.add(e);
p2.add(l);
p2.add(g);
p2.add(h);
p2.add(i);
Frame f=new Frame();
f.setSize(500,500);
f.add(p3);
f.add(p1);
f.add(p2);
show();
}
public static void main(String[] args){
new cal();}
}`
回答by Hovercraft Full Of Eels
- Don't use AWT library components for your GUI, but rather use the Swing library's components such as JFrame, JPanel, JButton...
- To view something on the top level window, you have to add your components to the displayedtop level window, and you never do this. In other words you need to add your Panels (which should be JPanels) to the main class via the
add(...)
method. You add them to a Frame object you callf
, but you display the Frame that represents the current class, thethis
-- two very completely different objects. - One way to get your code working is to nothave your class extend a top level window, but instead to create a top level window (as you're doing) and display it after adding components (as you're not doing).
- Avoid calling deprecated methods like
show()
. Doing this can be dangerous, your compiler should give you a warning about this, and you should heed the warning. - Learn about the layout managers and use them. You are currently using them since your components come with default layout managers, but are not using them correctly.
- Most important, read the tutorials which you can find here, as you can't guess at this stuff.
- Don't post code here that is all left justified as it is very hard to read.
- 不要为您的 GUI 使用 AWT 库组件,而是使用 Swing 库的组件,例如 JFrame、JPanel、JButton...
- 要在顶层窗口中查看某些内容,您必须将组件添加到显示的顶层窗口中,而且您永远不会这样做。换句话说,您需要通过该
add(...)
方法将您的面板(应该是 JPanels)添加到主类中。您将它们添加到您调用的 Frame 对象中f
,但是您显示代表当前类的 Frame ,这this
两个完全不同的对象。 - 让您的代码工作的一种方法是不让您的类扩展顶级窗口,而是创建一个顶级窗口(如您所做的那样)并在添加组件后显示它(如您未做的那样)。
- 避免调用诸如
show()
. 这样做可能很危险,你的编译器应该给你一个警告,你应该注意警告。 - 了解布局管理器并使用它们。您当前正在使用它们,因为您的组件带有默认布局管理器,但没有正确使用它们。
- 最重要的是,阅读您可以在此处找到的教程,因为您无法猜测这些内容。
- 不要在此处发布所有左对齐的代码,因为它很难阅读。
回答by Raju Sharma
you need to replace GridLayout value of p1 and p2 to
您需要将 p1 和 p2 的 GridLayout 值替换为
p1.setLayout(new GridLayout(5,5));//To incease gap between components you need to use new GridLayout(5,5,hgap,ygap)
p2.setLayout(new GridLayout(4,4));//similar here.
and you code is not correctly done here remove the show() function and replace it with :
并且您的代码在此处未正确完成,请删除 show() 函数并将其替换为:
f.setLayout(new GridLayout(3,1));// you may want three rows and 1 column for this.
f.setVisible(true);//for frame should be visible.
pls follow the link how to increase gap between components in gridlayout: http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html.
请按照链接如何增加 gridlayout 中的组件之间的差距:http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html 。
Why dont you use Java swing. it is better and have advanced features.
为什么不使用Java Swing。它更好并且具有高级功能。
your modified code will be this:
您修改后的代码将是这样的:
import java.awt.*;
public class Cal extends Frame {
Cal(){
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
p1.setLayout(new GridLayout(5,5));
p2.setLayout(new GridLayout(4,4));
TextField k=new TextField();
Button a=new Button("HI");
Button b=new Button("HI");
Button c=new Button("HI");
Button d=new Button("HI");
Button e=new Button("HI");
Button l=new Button("Hello");
Button g=new Button("Hello");
Button h=new Button("Hello");
Button i=new Button("Hello");
p1.add(a);
p1.add(b);
p1.add(c);
p1.add(d);
p1.add(e);
p2.add(l);
p2.add(g);
p2.add(h);
p2.add(i);
p3.add(k);
Frame f=new Frame();
f.setLayout(new GridLayout(3,1));
f.setSize(500,500);
f.add(p3);
f.add(p1);
f.add(p2);
f.setVisible(true);
}
public static void main(String[] args){
new Cal();}
}