📄 userproperty.java
字号:
c.insets = new Insets(30, 0, 0, 0); //top padding c.gridwidth = GridBagConstraints.REMAINDER; c.fill = GridBagConstraints.BOTH; JPanel gridletPanel = new JPanel(new GridLayout(5, 4, 15, 15)); panel.add(gridletPanel, c); createGridlet(gridletPanel); ////////////// c.gridy = index++; JPanel budgetDeadlinePanel = new JPanel(new BorderLayout(5, 5)); panel.add(budgetDeadlinePanel, c); createBudgetDeadline(budgetDeadlinePanel); //////////////// // Create an OK button c.gridy = index++; c.gridx = 0; c.gridwidth = 1; c.fill = GridBagConstraints.NONE; ok_ = new JButton("OK"); panel.add(ok_, c); // Create a Cancel button c.gridwidth = GridBagConstraints.REMAINDER; cancel_ = new JButton("Cancel"); panel.add(cancel_, c); /////////////////// // Add panel to a scroll pane int vert = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int horiz = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane scroll = new JScrollPane(panel, vert, horiz); getContentPane().add(scroll); } /** * Creates a panel about budget and deadline factors * @param panel a JPanel object * @pre panel != null * @post $none */ private void createBudgetDeadline(JPanel panel) { Font font = new Font(null, Font.PLAIN, 12); // Create a border and its title TitledBorder title = BorderFactory.createTitledBorder( "Budget and Deadline"); title.setTitleFont(font.deriveFont(Font.BOLD, 16)); panel.setBorder(title); /////////////// radioFactor_ = new JRadioButton("Factor-based"); radioFactor_.setFont(font); radioFactor_.setActionCommand("factor"); JPanel northPanel = new JPanel(); northPanel.add(radioFactor_); ////////////// radioValue_ = new JRadioButton("Value-based"); radioValue_.setFont(font); radioValue_.setActionCommand("value"); northPanel.add(radioValue_); panel.add(northPanel, BorderLayout.NORTH); // Group the radio buttons into one ButtonGroup group = new ButtonGroup(); group.add(radioFactor_); group.add(radioValue_); if (curRadioFactor_ == true) { radioFactor_.setSelected(true); } else { radioValue_.setSelected(true); } //////// JPanel centerPanel = new JPanel(new GridBagLayout()); createCenterComponent(centerPanel); panel.add(centerPanel, BorderLayout.CENTER); StringBuffer str = new StringBuffer(100); str.append(" Note: For factor-based, the range for both budget"); str.append(" and deadline is [0.0, 1.0]"); JLabel label = new JLabel( str.toString() ); label.setFont(font); panel.add(label, BorderLayout.SOUTH); } /** * Creates a panel component * @param panel a JPanel object * @pre panel != null * @post $none */ private void createCenterComponent(JPanel panel) { Font font = new Font(null, Font.PLAIN, 12); GridBagConstraints c = new GridBagConstraints(); int index = 0; c.gridy = index++; c.weighty = 1.0; c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(10, 5, 10, 10); // top, left, buttom, right JLabel label = new JLabel(" Budget: "); label.setFont(font); panel.add(label, c); JTextField text = new JTextField("" + bNum_, 10); panel.add(text, c); hashText_.put("budget", text); ////////// c.gridy = index++; label = new JLabel(" Deadline: "); label.setFont(font); panel.add(label, c); text = new JTextField("" + dNum_, 10); panel.add(text, c); hashText_.put("deadline", text); } /** * Creates a panel about Gridlets * @param panel a JPanel object * @pre panel != null * @post $none */ private void createGridlet(JPanel panel) { Font font = new Font(null, Font.PLAIN, 12); GridBagConstraints c = new GridBagConstraints(); // Create a border and its title TitledBorder title = BorderFactory.createTitledBorder("Gridlet"); title.setTitleFont(font.deriveFont(Font.BOLD, 16)); panel.setBorder(title); JLabel label = new JLabel("blah"); label.setVisible(false); panel.add(label); int index = 0; c.gridy = index++; c.weighty = 1.0; c.gridwidth = 1; c.fill = GridBagConstraints.HORIZONTAL; c.insets = new Insets(10, 5, 10, 10); // top, left, buttom, right label = new JLabel(" Size"); label.setFont(font); panel.add(label); label = new JLabel("Min. Deviation (%)"); label.setFont(font); panel.add(label); label = new JLabel("Max. Devation (%)"); label.setFont(font); panel.add(label); c.gridy = index++; label = new JLabel(" Gridlet:"); label.setFont(font); panel.add(label); JTextField text = new JTextField("" + gridSize_, 5); panel.add(text); hashText_.put("grid_size", text); text = new JTextField("" + gridMin_, 5); panel.add(text); hashText_.put("grid_min", text); text = new JTextField("" + gridMax_, 5); panel.add(text); hashText_.put("grid_max", text); ///////////////// label = new JLabel(" Length:"); label.setFont(font); panel.add(label); text = new JTextField("" + lengthSize_, 5); panel.add(text); hashText_.put("length_size", text); text = new JTextField("" + lengthMin_, 5); panel.add(text); hashText_.put("length_min", text); text = new JTextField("" + lengthMax_, 5); panel.add(text); hashText_.put("length_max", text); //////////// label = new JLabel(" File:"); label.setFont(font); panel.add(label); text = new JTextField("" + fileSize_, 5); panel.add(text); hashText_.put("file_size", text); text = new JTextField("" + fileMin_, 5); panel.add(text); hashText_.put("file_min", text); text = new JTextField("" + fileMax_, 5); panel.add(text); hashText_.put("file_max", text); ///////////////// label = new JLabel(" Output:"); label.setFont(font); panel.add(label); text = new JTextField("" + outputSize_, 5); panel.add(text); hashText_.put("output_size", text); text = new JTextField("" + outputMin_, 5); panel.add(text); hashText_.put("output_min", text); text = new JTextField("" + outputMax_, 5); panel.add(text); hashText_.put("output_max", text); } /** * Removes all GUI listeners * @pre $none * @post $none */ private void removeListeners() { ok_.removeActionListener(this); cancel_.removeActionListener(this); } /** * Saves the current values * @return <tt>true</tt> if the values have been saved successfully, * <tt>false</tt> otherwise * @pre $none * @post $none */ private boolean saveValue() { boolean flag = true; comboPolicy_ = combo_.getSelectedIndex(); name_ = ( (JTextField) hashText_.get("user") ).getText(); String errorMsg = ""; // a flag to denote where the error comes from try { errorMsg = "baud rate"; JTextField text = (JTextField) hashText_.get("baud"); baudRate_ = checkValue(baudRate_, text); errorMsg = "delay"; text = (JTextField) hashText_.get("delay"); delay_ = checkValue(delay_, text); errorMsg = "hour"; text = (JTextField) hashText_.get("hour"); hour_ = checkValue(hour_, text); errorMsg = "minute"; text = (JTextField) hashText_.get("min"); min_ = checkValue(min_, text); errorMsg = "second"; text = (JTextField) hashText_.get("sec"); sec_ = checkValue(sec_, text); ///////////// errorMsg = "gridlet size"; text = (JTextField) hashText_.get("grid_size"); gridSize_ = checkValue(gridSize_, text); errorMsg = "gridlet min deviation"; text = (JTextField) hashText_.get("grid_min"); gridMin_ = checkValue(gridMin_, text, "deviation"); errorMsg = "gridlet max deviation"; text = (JTextField) hashText_.get("grid_max"); gridMax_ = checkValue(gridMax_, text, "deviation"); /////////////// errorMsg = "length size"; text = (JTextField) hashText_.get("length_size"); lengthSize_ = checkValue(lengthSize_, text); errorMsg = "length min deviation"; text = (JTextField) hashText_.get("length_min"); lengthMin_ = checkValue(lengthMin_, text, "deviation"); errorMsg = "length max deviation"; text = (JTextField) hashText_.get("length_max"); lengthMax_ = checkValue(lengthMax_, text, "deviation"); //////////////// errorMsg = "file size"; text = (JTextField) hashText_.get("file_size"); fileSize_ = checkValue(fileSize_, text); errorMsg = "file min deviation"; text = (JTextField) hashText_.get("file_min"); fileMin_ = checkValue(fileMin_, text, "deviation"); errorMsg = "file max deviation"; text = (JTextField) hashText_.get("file_max"); fileMax_ = checkValue(fileMax_, text, "deviation"); ///////////////// errorMsg = "output size"; text = (JTextField) hashText_.get("output_size"); outputSize_ = checkValue(outputSize_, text); errorMsg = "output min deviation"; text = (JTextField) hashText_.get("output_min"); outputMin_ = checkValue(outputMin_, text, "deviation"); errorMsg = "output max deviation"; text = (JTextField) hashText_.get("output_max"); outputMax_ = checkValue(outputMax_, text, "deviation"); ////////////////// errorMsg = "budget"; text = (JTextField) hashText_.get("budget"); bNum_ = checkValue(bNum_, text, "factor"); errorMsg = "deadline"; text = (JTextField) hashText_.get("deadline"); dNum_ = checkValue(dNum_, text, "factor"); if (radioFactor_.isSelected() == true) { curRadioFactor_ = true; } else { curRadioFactor_ = false; } } catch (NumberFormatException e) { toolkit_.beep(); JOptionPane.showMessageDialog(this, "Invalid value for number of " + errorMsg + ".", errorMsg + " value error", JOptionPane.ERROR_MESSAGE); flag = false; } catch (Exception e) { toolkit_.beep(); JOptionPane.showMessageDialog( this, e.getMessage(), errorMsg + " value error", JOptionPane.ERROR_MESSAGE ); flag = false; } return flag; } /** * Checks whether the given value is correct or not * @param value given number * @param text a JTextField object * @return the correct number * @throws Exception if the given number is incorrect * @pre text != null * @post $result >= 0 */ private double checkValue(double value, JTextField text) throws Exception { value = new Double( text.getText() ).doubleValue(); if (value < 0.0) { throw new Exception("Invalid for having negative value."); } return value; } /** * Another overloaded method. * Checks whether the given value is correct or not * @param value given number * @param text a JTextField object * @return the correct number
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -