如何添加按钮和面板帧在Java AWT [英] how to add buttons and panels in frame in java awt

查看:360
本文介绍了如何添加按钮和面板帧在Java AWT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让Java AWT方案,其中第一行包含文本字段,未来5行包含5个按钮每接下来的4行包含每4个按钮。以及如何设置这些按钮的他们之间的尺寸和空间?我已经使用这个面板3而不是工作试过。

(示例程序由我提出,但没有显示任何)

 `进口java.awt中的*。
类CAL扩展框架{
CAL(){
面板P1 =新面板();
面板P2 =新面板();
面板P3 =新面板();
p1.setLayout(新的GridLayout(2,3));
p2.setLayout(新的GridLayout(2,2));
TextField的K =新的TextField(0,20);
按钮=新按钮(HI);
按钮B =新按钮(HI);
按钮C =新按钮(HI);
按键d =新按钮(HI);
按钮E =新按钮(HI);
按钮L =新按钮(你好);
巴顿G =新按钮(你好);
按键H =新按钮(你好);
按钮i =新按钮(你好);
p1.add(一);
p1.add(二);
p1.add(C);
p1.add(四);
p1.add(E);
p2.add(升);
p2.add(G);
p2.add(H);
p2.add(ⅰ);
架F =新帧();
f.setSize(500,500);
f.add(P3);
f.add(P1);
f.add(P2);显示();
}
公共静态无效的主要(字串[] args){
新的CAL();}
}`


解决方案

  1. 请不要使用AWT组件库为您的图形用户界面,而是使用Swing库的组件,例如JFrame的,JPanel的,一个JButton ...

  2. 要查看顶层窗口的东西,你有你的组件添加到显示顶层窗口,你从来没有做到这一点。换句话说,你需要通过添加(...)方法的面板(这应该是JPanels)添加到主类。将它们添加到你叫˚F A Frame对象,但你显示重新presents当前类的框架,在这个 - 两个非常完全不同的对象。

  3. 一个办法让你的code工作是的的有你的类扩展一个顶层窗口,而是要建立一个顶层窗口(如你正在做的),并显示它添加组件后(如你不这样做)。

  4. 避免去调用precated方法,如显示()。这样做可能是危险的,你的编译器应该给你一个关于这个警告,你应该听从警告。

  5. 了解布局管理器,并使用它们。您目前正在使用它们,因为您的组件附带默认的布局管理器,但不正确地使用它们。

  6. 最重要的是,阅读,你可以在这里找到 的教程,你不能在这个东西猜测。

  7. 请不要在这里发表code是所有左对齐,因为它是非常难以阅读。

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. 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.

这篇关于如何添加按钮和面板帧在Java AWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆