how to add buttons and panels in frame in java awt

亡梦爱人 提交于 2019-12-11 04:47:23

问题


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();}
}`

回答1:


  1. Don't use AWT library components for your GUI, but rather use the Swing library's components such as JFrame, JPanel, JButton...
  2. 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.
  3. 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).
  4. 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.
  5. 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.
  6. Most important, read the tutorials which you can find here, as you can't guess at this stuff.
  7. Don't post code here that is all left justified as it is very hard to read.



回答2:


you need to replace GridLayout value of p1 and p2 to

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 :

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.

Why dont you use Java swing. it is better and have advanced features.

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();}
}


来源:https://stackoverflow.com/questions/23032879/how-to-add-buttons-and-panels-in-frame-in-java-awt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!