【问题标题】:How can I put textfield and graphics in the same jframe?如何将文本字段和图形放在同一个 jframe 中?
【发布时间】:2014-05-22 15:28:52
【问题描述】:

我试图在同一个 jframe 中拥有一个文本字段和图形,但它无法正常工作。我想将文本字段放在底部,将其余的 jframe 用于图形,而不是当我运行它时,文本字段的行为很奇怪并覆盖了整个区域。有人知道我怎样才能让它按我想要的方式工作吗?

package pack;


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

public class gui extends JPanel implements Runnable{ 

    Thread t = new Thread(this);
    protected JTextField textField;
    private final static String newline = "\n";

    public int x;
    public int y;

    public static void main(String args[])
    {
        new gui();
        new input();
    }

    public void input()
    {
        textField = new JTextField(20);

        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridwidth = 500;
        c.gridheight = 100;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(textField, c);

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
    }
    public void actionPerformed(ActionEvent evt) {
        String text = textField.getText();
        textField.selectAll();
    }

    public gui()
    {
        textField = new JTextField(20);

        JFrame f = new JFrame("lol");
        System.out.println("::");

        f.setTitle("Basic window");
        f.setSize(500, 500);
        f.setLocationRelativeTo(null);

        f.add(this);
        f.setVisible(true);
        f.setFocusable(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(textField);

        run();
    }

    public void run()
    {
        while(true)
        {
            try
            {
                t.sleep(10);
            }
            catch(Exception e){}

            System.out.println(":D");
            x++;
            y++;

            repaint();
        }
    }

    public void paint (Graphics g)
    {
        g.setColor(Color.red);
    }
}

【问题讨论】:

  • 您的代码在编辑器中是这样的吗?

标签: java swing graphics textfield


【解决方案1】:

删除该课程并重新开始新课程。代码结构错误,类名错误,自定义绘制错误,使用线程错误,new input() 没有做任何事情,你不应该使用 Thread.sleep(),你应该不覆盖paint(),你不应该在框架可见后向框架添加组件。

首先阅读 Custom Painting 上的 Swing 教程中的部分。在那里您会找到一个工作示例,该示例将向您展示如何在进行自定义绘画时更好地构建您的课程。使用此演示代码作为您的程序的起点,并对此工作代码进行更改(一次一个)。

然后您可以更改该代码并将 JTextField 添加到框架中。您还需要阅读 Using Layout Managers 上的 Swing 教程,以了解 BorderLayout 的工作原理。所以从一些简单的、有效的开始,然后添加额外的组件。不要试图一次完成所有事情。

【讨论】:

    【解决方案2】:

    你做错了事情。

    • JFrame 默认使用 BorderLayout 并且您在中心添加两个组件,因此只有最后一个组件可见。

    • 您将在JFrameJPanel 中添加JTextField。不知道为什么?


    使用BorderLayout.SOUTHJTextField添加到南边,不要添加到JPanel中如下图:

    public gui() {
        ...
        textField = new JTextField(20);
        JFrame f = new JFrame("lol");        
        f.add(this);        
        f.add(textField, BorderLayout.SOUTH);
        ...
    }
    

    请再次阅读下面的帖子。

    How to Use GridLayout

    How to Use GridBagLayout

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多