简体   繁体   English

我可以在 label 中有一个文本字段吗?

[英]Can I have a textfield inside a label?

What I would like to do is display the following in a form:我想做的是以表格形式显示以下内容:

Open [15] minutes before class

Where [15] is a text-field.其中[15]文本字段。 Is this possible?这可能吗?

Use a 'composite component' by adding the required parts to a JPanel .通过将所需部分添加到JPanel来使用“复合组件”。 EG例如

课前时间

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'.请注意,我将“文本字段”换成了JSpinner - 一个更适合选择“以分钟为单位的时间”的组件。

Can I have a textfield inside a label?
  • answer is, yes you can, this is basic property of Java AWT / Swing Objects答案是,可以,这是 Java AWT / Swing对象的基本属性

  • JComboBox , JTable , JList , JSpinner , JFile(Color)Chooser .... are compound JComponents , you can extract all JComponent and put that together again. JComboBoxJTableJListJSpinnerJFile(Color)Chooser .... 是复合JComponents ,您可以提取所有JComponent并将其再次组合在一起。

  • you can put any of JComponents to the another你可以把任何一个JComponents放到另一个

  • 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只有JFrame/JDialog/JWindowJPanel在 API 中默认实现了LayoutManager对于 rest 那么你必须实现适当的 LayoutManager

I think I have not understood.我想我还没有明白。 But, I'll try:但是,我会尝试:

You can get the text from a TextField:您可以从 TextField 中获取文本:

label.setText("Open " + textField.getText()+ " minutes before class"); label.setText("打开" + textField.getText()+ "课前分钟");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM