?? inputpane.java
字號:
package gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import model.Datenmodell;
public class InputPane extends JPanel{
/**
*input the values and add it into list
*/
private static final long serialVersionUID = 1073196683753167491L;
private JTextField textField=new JTextField();
private JScrollPane scrollPane;
private JList list;
private JButton add=new JButton(" Add ");
private JButton remove=new JButton("Remove");
private JToolBar toolBar=new JToolBar();
private Datenmodell model;
public InputPane(Datenmodell model){
super();
this.model=model;
setName("input Data");
// setSize(400,300);
setBorder(BorderFactory.createEtchedBorder());
init();
}
private void init(){
list=new JList(new DatenListModel(model));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.insets = new Insets(15, 155, 20, 55);
gridBagConstraints2.gridy = 2;
gridBagConstraints2.gridx = 0;
gridBagConstraints2.anchor = GridBagConstraints.SOUTH;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.BOTH;
gridBagConstraints1.gridwidth = 2;
gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 1;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.weighty = 1.0;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridwidth = 2;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
setLayout(new GridBagLayout());
scrollPane=new JScrollPane(list);
toolBar.add(add);
toolBar.add(remove);
add(textField, gridBagConstraints);
add(scrollPane, gridBagConstraints1);
add(toolBar, gridBagConstraints2);
textField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addValue_actionPerformed(e);
}
});
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addButton_actionPerformed(e);
}
});
remove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
removeButton_actionPerformed(e);
}
});
}
private void addButton_actionPerformed(ActionEvent e){
int index = list.getSelectedIndex();
if (index >= 0 && index < model.getSize()) {
model.addValue(model.getValue(index));
}
}
private void removeButton_actionPerformed(ActionEvent e){
int index = list.getSelectedIndex();
if (index >= 0 && index < model.getSize()) {
model.deletValue(index);
}
}
private void addValue_actionPerformed(ActionEvent e){
try {
double v=Double.parseDouble(textField.getText());
if(v>=100)
JOptionPane.showMessageDialog(this, "new value must smaller than 100");
else
model.addValue(v);
} catch (NumberFormatException excpt) {
JOptionPane.showMessageDialog(this, "Wrong format for new value");
}finally{
textField.setText("");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -