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.
(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();}
}`
解决方案
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 displayed top 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 call f, but you display the Frame that represents the current class, the this -- two very completely different objects.
One way to get your code working is to not have 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.