What I would like to do is display the following in a form:
Open [15] minutes before class
Where [15] is a text-field. Is this possible?
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'.
Can I have a textfield inside a label?
answer is, yes you can, this is basic property of Java
AWT / SwingObjectsJComboBox,JTable,JList,JSpinner,JFile(Color)Chooser.... are compoundJComponents, you can extract allJComponentand put that together again.you can put any of
JComponentsto the anotheronly
JFrame/JDialog/JWindowandJPanelhave got implementedLayoutManagerby 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");
来源:https://stackoverflow.com/questions/10021579/can-i-have-a-textfield-inside-a-label