简体   繁体   English

我正在使用 java 中的 awt pakage 编写程序,但我的组件没有被添加

[英]I am writing a program using awt pakage in java but my components are not getting added

I am writing a program using the AWT package in which I also implemented ActionListner which shows the number of clicks I made on the button.我正在使用 AWT 包编写一个程序,其中我还实现了 ActionListner,它显示了我在按钮上的点击次数。

package awtlistenerdemo;
import java.awt.*;
import java.awt.event.*;
public class AwtListenerDemo implements ActionListener {
  TextField t1 = new TextField(30);
  Button b;
  int count = 0;
  Frame f;
  AwtListenerDemo() {
    f = new Frame("Action Listener Example");
    b = new Button("Ok");
    f.setLayout(null);
    b.setBounds(100, 100, 60, 20);
    t1.setBounds(100, 200, 80, 30);
    f.add(b);
    f.add(t1);
    b.addActionListener(this);
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == b) {
      count++;
      t1.setText("Button Clicked" + count + "Times");
    }
  }
  public static void main(String[] args) {
    Frame f = new Frame("Action Listener Example");
    f.setVisible(true);
    f.setSize(300, 300);

  }

}
TextField t1 = new TextField(30);
Button b;
int count = 0;
Frame f;
AwtListenerDemo() {
  b = new Button("Ok");
  f = new Frame("Action Listener Example");
  f.setSize(300, 300);
  f.addWindowListener(new WindowAdapter(){  
        public void windowClosing(WindowEvent e) {  
            f.dispose();  
        }  
  });  
  f.setLayout(null);
    
  b.setBounds(100, 100, 60, 20);
  t1.setBounds(100, 200, 80, 30);
    
  f.add(b);
  f.add(t1);
  f.setVisible(true);
    
  b.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
  if (e.getSource() == b) {
    count++;
    t1.setText("Button Clicked" + count + "Times");
  }
}
public static void main(String[] args) {
  new AwtListenerDemo();
}

The main method never constructs an AwtListenerDemo so all you see is the standard, blank frame created in that method. main方法从不构造AwtListenerDemo因此您看到的只是在该方法中创建的标准空白帧。 Once that problem is fixed, some of the statements in the main method need to be moved into the constructor and applied to the frame it creates.一旦这个问题得到解决,main 方法中的一些语句需要移到构造函数中并应用于它创建的框架。

This is the result:这是结果:

在此处输入图片说明

import java.awt.*;
import java.awt.event.*;

public class AwtListenerDemo implements ActionListener {

    TextField t1 = new TextField(30);
    Button b;
    int count = 0;
    Frame f;

    AwtListenerDemo() {
        f = new Frame("Action Listener Example");
        b = new Button("Ok");
        f.setLayout(null);
        b.setBounds(100, 100, 60, 20);
        t1.setBounds(100, 200, 80, 30);
        f.add(b);
        f.add(t1);
        b.addActionListener(this);
        // ADD this!
        f.setSize(300,300);
        // then set it VISIBLE
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b) {
            count++;
            t1.setText("Button Clicked" + count + "Times");
        }
    }

    public static void main(String[] args) {
        // Change the main code to..
        new AwtListenerDemo();
    }
}

General Tips一般提示

  1. See this answer for many good reasons to abandon AWT components in favor of Swing.看到这个答案有很多很好的理由放弃 AWT 组件而支持 Swing。 Added to that is that few people in this day & age have ever used AWT (so can't help with problems) or if they did, have forgotten the finer details (so give faulty advice).除此之外,当今时代很少有人使用过 AWT(因此无法解决问题),或者如果他们使用了,则忘记了更详细的细节(因此请提供错误的建议)。 As mentioned in the linked answer, Swing is built on AWT, but has differences that need to be unlearned when making the transition.作为链接的答复中提到,Swing是建立在AWT,但需要进行过渡时要区别不精通 Seriously, start with Swing or go directly to Java-FX (an entirely different GUI toolkit which is based on neither Swing nor AWT).说真的,从 Swing 开始或直接转到 Java-FX(一个完全不同的 GUI 工具包,它既不基于 Swing 也不基于 AWT)。 Warning: This is the one & only time I will be helping to fix broken AWT code for you.警告:这是我唯一一次帮助您修复损坏的 AWT 代码。 (3) (3)
  2. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的 PLAF 在不同的区域设置。 As such, they are not conducive to pixel perfect layout.因此,它们不利于像素完美布局。 Instead use layout managers, or combinations of them along with layout padding and borders for white space .而是使用布局管理器,或它们的组合以及用于空白的布局填充和边框。 The (beginnings of these) problems can be seen even in that short example, in that: a) Either the button, the text field, or both are not center aligned.即使在那个简短的示例中也可以看到(这些的开始)问题,其中:a) 按钮、文本字段或两者都未居中对齐。 If the frame is resized even slightly, it would be both .如果框架稍微调整大小,它将是both b) The size guess for the text field is also wrong - it is not long enough to display the text! b) 文本域的大小猜测也是错误的——长度不够显示文本!
  3. Relevant to (1), there are still some problems with this code.与(1)相关,这段代码还有一些问题。 I did the minimum needed to answer the question and will do no more.我做了回答问题所需的最低限度,不会再做更多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java AWT组件 - Using Java AWT components AWT:我正在尝试学习 Java,但无法理解以下程序 - AWT: I am trying to learn Java and was unable to understand the following program 为什么我在简单的Java程序中得到ClassCastException? - why am i getting ClassCastException in my simple java program? 使用 Java AWT 组件编写一个程序,以选择的顺序在列表中显示所选项目 - Write a program to display the selected items in a list in the order of selection using Java AWT Components 我在java程序中得到一个NullPointer异常 - I am getting a NullPointer exception in a java program 为什么在此Java程序中出现ArrayIndexOutOfBoundsException? - Why am I getting ArrayIndexOutOfBoundsException in this Java program? 使用Java中的AWT和组件进行绘图 - Drawing with AWT and components in Java 我的 java 程序为某些 AWT 类抛出 java.lang.ClassNotFoundException 我该怎么办? - My java program is throwing java.lang.ClassNotFoundException for some AWT classes what do I do? 为什么我在线程“ AWT-EventQueue-2” java.lang.NoClassDefFoundError中得到异常? - why i am getting Exception in thread “AWT-EventQueue-2” java.lang.NoClassDefFoundError? 运行程序时出现“ java.lang.ArithmeticException:/减零” - I am getting “java.lang.ArithmeticException: / by zero” when I run my program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM