사용자 입력을 사용하려는 일부 텍스트 필드를 만들었습니다. 나는 documentListener를 사용해야한다고 읽었지 만, 나는 생각하기에 그것을 구현하는 데 어려움이있다.documentlistener 구현 방법
코드에서 텍스트 필드 tf1에 구현하려고합니다. 입력 된 메신저가 두 번 파싱되어야하므로 수학 계산을 할 수 있습니다.
여기 내 코드를 구현하려고합니다.
다음 가능한 방법 (블록 실행) 번호 포매터의 JFormattedTextField를 사용하는 직접하는 DocumentFilter를 구현거나 당신이 번호를
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Display {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
JLabel label;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
...
button = new JButton("Value Bet");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
JFrame frame = new JFrame("Value Bet");
frame.setVisible(true);
frame.setSize(500,300);
GridBagLayout layout = new GridBagLayout();
frame.setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
JLabel label;
JTextField tf;
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
if (shouldWeightX) {
c.weightx = 0.5;
}
...
final JTextField tf1 = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
frame.add(tf1, c);
tf1.getDocument().addDocumentListener(new DocHandler(){
public class DocHandler implements DocumentListener{
@Override
public void changedUpdate(DocumentEvent arg0) {
tfHasChanged();
}
@Override
public void insertUpdate(DocumentEvent arg0) {
tfHasChanged();
}
@Override
public void removeUpdate(DocumentEvent arg0) {
tfHasChanged();
}
}
public void tfHasChanged(){
double chance1 = Double.parseDouble(tf1.getText());
}
});
... div components
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Betting Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
나는이 모든 방법을 드롭하고 내가 권장하는 모달 대화 상자를 사용하는 것이 좋습니다 마지막 질문에 [내 대답] (http://stackoverflow.com/a/10846008/418556)을 입력하십시오. 또는 최선의 도움을 얻으려면 최소한 왜 그런 접근법을 사용하지 않으려는지 설명하십시오. –
더 나은 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. –