30/09/2018, 20:15
Cho em hỏi về Swing trong Java cách tạo Date Field?
public class FormPanel extends JPanel {
private JLabel eventLabel;
private JTextField eventText;
private JLabel dateLable;
private UtilDateModel modelDateEvent;
private JDatePanelImpl datePanel;
private JDatePickerImpl dateEventPicker;
public FormPanel(){
eventLabel = new JLabel("Event name: ");
eventText = new JTextField();
dateLable = new JLabel("Event date: ");
modelDateEvent = new UtilDateModel();
datePanel = new JDatePanelImpl(modelDateEvent);
dateEventPicker = new JDatePickerImpl(datePanel);
Dimension dim = getPreferredSize();
dim.width = 250;
setPreferredSize(dim);
setBorder(BorderFactory.createTitledBorder("Add"));
layoutComponents();
}
public void layoutComponents(){
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
/////////////////// First row///////////////////
gc.gridy = 0;
gc.weightx= 1;
gc.weighty= 0.1;
gc.gridx = 0;
gc.fill = GridBagConstraints.NONE;
gc.anchor = GridBagConstraints.LINE_END;
gc.insets = new Insets(0,0,0,5);
add(eventLabel, gc);
gc.gridx = 1;
gc.gridy = 0;
gc.insets = new Insets(0,0,0,0);
gc.anchor = GridBagConstraints.LINE_START;
add(eventText, gc);
//////////////////// Second row //////////////////
gc.gridy++;
gc.weightx= 1;
gc.weighty= 0.1;
gc.gridx = 0;
gc.fill = GridBagConstraints.NONE;
gc.anchor = GridBagConstraints.LINE_END;
gc.insets = new Insets(0,0,0,5);
add(dateLable, gc);
gc.gridx = 1;
gc.gridy = 0;
gc.insets = new Insets(0,0,0,0);
gc.anchor = GridBagConstraints.LINE_START;
add(dateEventPicker);
}
}
Thì nó ra như hình.
Cho em hỏi làm cách nào để text field rộng ra, và datefiled về đúng chỗ mặc dù e đã config bố cục.
Bài liên quan