按钮和文本字段不会显示在Java中 [英] Button and textfield don't show up in Java

查看:97
本文介绍了按钮和文本字段不会显示在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For school I had to make a JFrame and within that One button and Two textfields. Whatever you put in Textfield one have to get into textfield two when the button is pressed. I got the code to the point I should see the textfields and the button when i run the program. For whatever reason it doesn't.

我到目前为止:

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

我还不能发布图片,但我会提供图片链接此处

I am not yet allowed to post images but I will provide a link to the picture down here

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

I am sorry if this question allready exests but i couldnt's find a similar question. I am new to programming so please dont yell at me when I forgot something or wrote something wrong in it!

推荐答案

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

Here i have modified your code a bit, but did in a similar way. I won't extend JFrame until and unless i don't want to do something creative, but you always CAN.

你已经扩展了 JFrame ,所以没有值得用调用方法frame.foo()
,但只需 foo(),最重要的是 JFrame frame = new HelloWorld(),将不会感觉,如果你已经用 JFrame 扩展了你的课程:

You had already extended JFrame , so no worth of calling methods with frame.foo() but simply foo() , and most important JFrame frame = new HelloWorld() , will make no sense, if you have already extended you class with 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);
        }
      }

此外,您还可以延长面板 JPanel

Also, you can also extend your panel class with 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);
        }
      }

这篇关于按钮和文本字段不会显示在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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