按钮和文本字段不会显示在Java中

Tom*_*ans 7 java swing netbeans jpanel

对于学校,我必须在一个按钮和两个文本区域内制作一个JFrame.无论你在Textfield中放置什么,当按下按钮时,必须进入文本字段2.当我运行程序时,我得到了代码,我应该看到textfields和按钮.无论出于何种原因,它都没有.

我到目前为止:

package helloworld;

import javax.swing.*;
import java.awt.event.*; 

    public class HelloWorld extends JFrame {

        public static void main(String[] args) {

             JFrame frame = new HelloWorld();
             frame.setSize(400, 400);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setTitle("Hello World Button App");

             JPanel panel = new JPanel();
             frame.setContentPane(panel);
             fram.setVisible(true);
         }
       }

       class panel extends JPanel {

          public JButton btn1 = new JButton("Klick!");
          public JTextField txt1 = new JTextField(10);
          public JTextField txt2 = new JTextField(10);

          public panel() {
               add(btn1);
               add(txt1);
               add(txt2);
            }
          }
Run Code Online (Sandbox Code Playgroud)

我还不能发布图片,但我会在这里提供图片的链接

如果这个问题已经出现,我很抱歉,但我找不到类似的问题.我是编程的新手,所以当我忘记某些内容或写错了内容时,请不要对我大喊大叫!

joe*_*han 7

在这里,我已经修改了一些代码,但是以类似的方式进行了修改.JFrame除非我不想做有创意的事情,否则我不会延长,但你总是可以.

你已经扩展了JFrame,所以没有值得调用方法frame.foo() 但是简单foo(),而且最重要的是JFrame frame = new HelloWorld()没有意义,如果你已经扩展了你的课程JFrame:

在此输入图像描述

import javax.swing.*;

public class HelloWorld extends JFrame{

 public static void main(String[] args) {

          SwingUtilities.invokeLater(new Runnable() {
          public void run() {
          new HelloWorld().setVisible(true);
        }
    });

     }
    public  HelloWorld()
    {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Hello World Button App");

        panel pan= new panel();
        add(pan.panel);
        pack();
        setVisible(true);
    }
   }

   class panel {

      private JButton btn1 = new JButton("Klick!");
      private JTextField txt1 = new JTextField(10);
      private JTextField txt2 = new JTextField(10);
      JPanel panel;
      public panel() {
           panel = new JPanel();
           panel.add(btn1);
           panel.add(txt1);
           panel.add(txt2);
        }
      }
Run Code Online (Sandbox Code Playgroud)

此外,您还可以通过以下方式扩展您的panel课程JPanel :

 public  HelloWorld()
    {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Hello World Button App");

          panel pan= new panel();
         add(pan);
         pack();
         setVisible(true);
    }
   }

   class panel extends JPanel {

      private JButton btn1 = new JButton("Klick!");
      private JTextField txt1 = new JTextField(10);
      private JTextField txt2 = new JTextField(10);

      public panel() {

           add(btn1);
           add(txt1);
           add(txt2);
        }
      }
Run Code Online (Sandbox Code Playgroud)

  • @TomSchillemans正如kleopatra所建议的那样,"_extending JSomething几乎总是次优,它们本来就是用来",你应该告诉你的老师,尽可能使用JSomething作为对象,我不会改变它你的代码的概念,你的方便的原因,并且不要忘记命名约定.祝你好运! (2认同)