Can I have a textfield inside a label?

别来无恙 提交于 2019-11-26 13:53:39

Use a 'composite component' by adding the required parts to a JPanel. E.G.

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

class TimeBeforeClass {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanel gui = new JPanel(new FlowLayout(FlowLayout.LEFT, 3,3));
                gui.add(new JLabel("Open"));
                gui.add(new JSpinner(new SpinnerNumberModel(15,0,20,1)));
                gui.add(new JLabel("minutes before class"));
                JOptionPane.showMessageDialog(null, gui);
            }
        });
    }
}

Note that I swapped the 'textfield' for a JSpinner - a more suitable component for selecting 'time in minutes'.

mKorbel
Can I have a textfield inside a label?
  • answer is, yes you can, this is basic property of Java AWT / Swing Objects

  • JComboBox, JTable, JList, JSpinner, JFile(Color)Chooser.... are compound JComponents, you can extract all JComponent and put that together again.

  • you can put any of JComponents to the another

  • only JFrame/JDialog/JWindow and JPanel have got implemented LayoutManager by default in the API, for rest of then you have to implements proper LayoutManager

I think I have not understood. But, I'll try:

You can get the text from a TextField:

label.setText("Open " + textField.getText()+ " minutes before class");

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!