在JPanel上放置JTextfield? [英] Put a JTextfield on a JPanel?

查看:159
本文介绍了在JPanel上放置JTextfield?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么文本框没有出现在框架内部的面板上? 我的意思是,还需要采取一些其他措施来制作面板的组件 可见吗?

Why the textfield is not appearing on my panel which is inside my frame? I mean is there some additional action necessary to make the components of the panel visible?

我希望有人能帮助我....

I hope somebody can help me....

public class example1  {

    public static void main(String[] args) {

    JFrame tt=new TT();
    }
}
class TT extends JFrame {

    JTextField textField;
    JPanel panel;
    JButton button1;
    JButton button2;

    public TT() {
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setTitle("Bla Blubb");
        setResizable(false);
        setLayout(null);

        panel=new JPanel();
        panel.setBounds(5, 5, 290, 290);
        add(panel);

        textField=new JTextField();
        textField.setBounds(5, 5, 280, 50);
        panel.add(textField);

            setVisible(true);

      }
}

推荐答案

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

class TT extends JFrame {

    JTextField textField;
    JPanel panel;
    JButton button1;
    JButton button2;

    public TT() {
        //setSize(300, 300);  // better to use pack() (after components added)
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // better to use
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //setLocationRelativeTo(null);  // better to use..
        setLocationByPlatform(true);
        setTitle("Bla Blubb");
        setResizable(false);
        //setLayout(null); // better to use layouts with padding & borders

        // set a flow layout with large hgap and vgap.
        panel = new JPanel(new FlowLayout(SwingConstants.LEADING, 10, 10));
        // panel.setBounds(5, 5, 290, 290); // better to pack()
        add(panel);

        //textField = new JTextField(); // suggest a size in columns
        textField = new JTextField(8);
        //textField.setBounds(5, 5, 280, 50); // to get height, set large font
        textField.setFont(textField.getFont().deriveFont(50f));
        panel.add(textField);

        pack(); // make the GUI the minimum size needed to display the content
        setVisible(true);
    }

    public static void main(String[] args) {
        // GUIS should be constructed on the EDT.
        JFrame tt = new TT();
    }
}

这篇关于在JPanel上放置JTextfield?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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