将按钮添加到JPanel

时间:2016-10-28 17:47:46

标签: button jframe

我有一个显示框架的JPanel代码:

http://i63.tinypic.com/vzix09.png

我的代码是:

   
public class DifficultyLVL extends JPanel implements PropertyChangeListener, ActionListener {
    private int Easy;
    private int Med;
    private int Hard;

    private JLabel EasyLabel;
    private JLabel MedLabel;
    private JLabel HardLabel;

    private static String EasyString = "Easy: ";
    private static String MedString = "Meduim: ";
    private static String HardString = "Hard: ";

    private JFormattedTextField EasyField;
    private JFormattedTextField MedField;
    private JFormattedTextField HardField;

    private NumberFormat EasyFormat;
    private NumberFormat MedFormat;
    private NumberFormat HardFormat;
 
    public DifficultyLVL() {
        super(new BorderLayout());
        setUpFormats();
        
        EasyLabel = new JLabel(EasyString);
        MedLabel = new JLabel(MedString);
        HardLabel = new JLabel(HardString);

        EasyField = new JFormattedTextField(EasyField);
        EasyField.setValue(new Integer(Easy));
        EasyField.setColumns(10);
        EasyField.addPropertyChangeListener("value", this);
 
        MedField = new JFormattedTextField(MedField);
        MedField.setValue(new Integer(Med));
        MedField.setColumns(10);
        MedField.addPropertyChangeListener("value", this);
 
        HardField = new JFormattedTextField(HardField);
        HardField.setValue(new Integer(Hard));
        HardField.setColumns(10);
        HardField.addPropertyChangeListener("value", this);
 
        EasyLabel.setLabelFor(EasyField);
        MedLabel.setLabelFor(MedField);
        HardLabel.setLabelFor(HardField);

        JPanel labelPane = new JPanel(new GridLayout(0,1));
        labelPane.add(EasyLabel);
        labelPane.add(MedLabel);
        labelPane.add(HardLabel);

        JPanel fieldPane = new JPanel(new GridLayout(0,1));
        fieldPane.add(EasyField);
        fieldPane.add(MedField);
        fieldPane.add(HardField);

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        add(labelPane, BorderLayout.CENTER);
        add(fieldPane, BorderLayout.LINE_END);
    }
    
	@Override
	public void actionPerformed(ActionEvent arg0) {
//		if (e.getSource--() == OK)
//		{
//			
//		}
	}
 
    /** Called when a field's "value" property changes. */
    public void propertyChange(PropertyChangeEvent e) {
        Object source = e.getSource();
        if (source == EasyField) {
            Easy = (int) ((Number)EasyField.getValue()).doubleValue();
        } else if (source == MedField) {
            Med = (int) ((Number)MedField.getValue()).doubleValue();
        } else if (source == HardField) {
            Hard = ((Number)HardField.getValue()).intValue();
        }
    }
 

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Choose How Many From Each Level");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Add contents to the window.
        frame.add(new DifficultyLVL());
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

}

我需要在哪里添加按钮?

我希望按钮位于中间框架的底部,正好位于HARD文本字段的下方

.......

0 个答案:

没有答案