单击JButton后,在某个位置弹出一个JTextField

时间:2016-12-23 08:45:20

标签: java swing jbutton jtextfield

我将三个JButton放在我想要的位置(在框架的顶部中心),当用户点击一个时,就会在BoxLayout中弹出一个JTextField。

问题是,当JTextField显示时,它位于按钮的左侧,并移动它们。

我尝试设置JTextField的对齐方式并使用各种胶水,但JTextField不会移动。

如果我想让JTextField弹出我的JButtons下方和屏幕中心,我应该使用什么?

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class Library extends JFrame implements ActionListener {

    private JFrame jf1;
    private JPanel jp1;
    private JTextField jtf1;
    private JButton jb1;
    private JButton jb2;
    private JButton jb3;

    public Library() {
        try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception q) {
        q.printStackTrace();
        }
    JFrame.setDefaultLookAndFeelDecorated(false);
    jf1 = new JFrame("Library");
    jf1.setVisible(true);
    jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf1.setSize(1080, 900);
    jf1.setResizable(true);

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);

    jp1 = (JPanel) jf1.getContentPane();
    jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));

    jb1 = new JButton("Genre");
    jb1.addActionListener(this);
    jb1.setMinimumSize(new Dimension(140, 60));
    jb1.setPreferredSize(new Dimension(150, 60));
    jb1.setMaximumSize(new Dimension(150, 60));
    jb1.setAlignmentY(-70.0f);
    jb2 = new JButton("Author");
    jb2.addActionListener(this);
    jb2.setMinimumSize(new Dimension(140, 60));
    jb2.setPreferredSize(new Dimension(150, 60));
    jb2.setMaximumSize(new Dimension(150, 60));
    jb2.setAlignmentY(-70.0f);
    jb3 = new JButton("Title");
    jb3.addActionListener(this);
    jb3.setMinimumSize(new Dimension(140, 60));
    jb3.setPreferredSize(new Dimension(150, 60));
    jb3.setMaximumSize(new Dimension(150, 60));
    jb3.setAlignmentY(-70.0f);



    jp1.add(Box.createHorizontalGlue());
    jp1.add(jb1);
    jp1.add(jb2);
    jp1.add(jb3);
    jp1.add(Box.createHorizontalGlue());

    jf1.validate();
}

@Override
public void actionPerformed(ActionEvent e) {
    Object code = e.getSource();
    if (code == jb1) {
        jtf1 = new JTextField("Enter Text");
        jtf1.setPreferredSize(new Dimension(200,20));
        jtf1.setMaximumSize(new Dimension(200,20));
        jtf1.setMinimumSize(new Dimension(10,10));
        jp1.add(jtf1);

        jp1.validate();
    }
    else if (code == jb2) {

    }
    else if (code == jb3) {

    }
}

public static void main(String[] args) {
    Library shoe = new Library();
    }
}

2 个答案:

答案 0 :(得分:3)

建议:不要动态添加/删除UI元素。只需在最初添加所有这些内容,然后只需在文本字段中调用setVisible(false)即可。

(而不是使用您的动作侦听器添加/删除字段)

答案 1 :(得分:0)

一个好的简单解决方案可能是使用另一个JPanel来保存动态创建的文本字段。这可能是你想要的:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;

public class Library extends JFrame implements ActionListener {

    private JFrame jf1;
    private JPanel jp1;
    private JPanel jp2;
    private JTextField jtf1;                   
    private JButton jb1;
    private JButton jb2;
    private JButton jb3;

    public Library() {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception q) {
            q.printStackTrace();
        }
        JFrame.setDefaultLookAndFeelDecorated(false);
        jf1 = new JFrame("Library");
        jf1.setVisible(true);
        jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf1.setSize(1080, 900);
        jf1.setResizable(true);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        jf1.setLocation(dim.width/2-jf1.getSize().width/2, dim.height/2-jf1.getSize().height/2);

        jp1 = new JPanel();
        jp1.setLayout(new BoxLayout(jp1, BoxLayout.LINE_AXIS));

        jp2 = new JPanel();
        jp2.setLayout(new BoxLayout(jp2, BoxLayout.LINE_AXIS));

        jb1 = new JButton("Genre");
        jb1.addActionListener(this);
        jb1.setMinimumSize(new Dimension(140, 60));
        jb1.setPreferredSize(new Dimension(150, 60));
        jb1.setMaximumSize(new Dimension(150, 60));
        jb1.setAlignmentY(-70.0f);

        jb2 = new JButton("Author");
        jb2.addActionListener(this);
        jb2.setMinimumSize(new Dimension(140, 60));
        jb2.setPreferredSize(new Dimension(150, 60));
        jb2.setMaximumSize(new Dimension(150, 60));
        jb2.setAlignmentY(-70.0f);

        jb3 = new JButton("Title");
        jb3.addActionListener(this);
        jb3.setMinimumSize(new Dimension(140, 60));
        jb3.setPreferredSize(new Dimension(150, 60));
        jb3.setMaximumSize(new Dimension(150, 60));
        jb3.setAlignmentY(-70.0f);

        jp1.add(Box.createHorizontalGlue());
        jp1.add(jb1);
        jp1.add(jb2);
        jp1.add(jb3);
        jp1.add(Box.createHorizontalGlue());

        jf1.getContentPane().add(jp1);
        jp1.setBounds(0, 0, 1080, 900);
        jf1.getContentPane().add(jp2);
        jp2.setBounds(0, 0, 1080, 900);

        validate();
    }

    public void actionPerformed(ActionEvent e) { 
        Object code = e.getSource();
        if (code == jb1) {
            jtf1 = new JTextField("Enter Text");
            jtf1.setPreferredSize(new Dimension(200,20));
            jtf1.setMaximumSize(new Dimension(200,20));
            jtf1.setMinimumSize(new Dimension(10,10));
            jp2.add(Box.createHorizontalGlue());
            jp2.add(jtf1);
            jp2.add(Box.createHorizontalGlue());

            jp2.validate();
        }
        else if (code == jb2) {

        }
        else if (code == jb3) {

        }
    }                                        

    public static void main(String[] args) {
        Library shoe = new Library();  
    }

}

我试着快速解决这个问题。希望能帮助到你。在代码中进行更多调整以完美匹配布局。