2013-04-24 3 views
0

컴파일하려면이 코드를 어떻게 수정해야합니까? 나는 많은 문제가 있다는 것을 알고 있습니다 ... 고마워 ~ statechanged에 무엇을 넣어야합니까? 내가있는 JOptionPane에 JSpinner에 연결하는 방법? (내가 만든 btnEnter, 내가해야 할 다음 무엇을, ChangeListener를 추가?)Jspinner 및 JOptionPane

import javax.swing.*; 
import java.awt.Container; 
import java.util.Calendar; 
import java.util.Date; 
import javax.swing.event.*; 
import java.awt.event.*; 
import java.awt.Graphics; 
import java.lang.Comparable; 
import javax.swing.JOptionPane; 
import javax.swing.JDialog; 

public class JSpinnerr extends JApplet 
implements ActionListener, ChangeListener{ 
private static final long serialVersionUID = 2005L; 

private Container window; 
private JButton  btnEnter; 
private JOptionPane pane; 
private JLabel  favoriteJl; 
private JButton[] choiceJb; 
private ButtonGroup choiceBg; 

//spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/yyyy")); 
public void init() { 
    window = new DoubleBufferedPanel(); 
    setContentPane(window); 

    createAppearance(); 
    createGUI(); 
    createJOptionPane(); 

} 

public void createAppearance(){ 
    window.setLayout(null); 
} 

public void createGUI(){ 
    favoriteJl = new JLabel("When are you going to graduate from High School?"); 
    favoriteJl.setSize(100, 30); 
    favoriteJl.setLocation(20, 120); 
    window.add(favoriteJl); 

    btnEnter = new JButton("Enter"); 
    btnEnter.setSize(100, 30); 
    btnEnter.setLocation(220, 120); 
    btnEnter.addChangeListener(this); 
    window.add(btnEnter); 

    Date today = new Date(); 

    JSpinner dateJs = new JSpinner(new SpinnerDateModel(today, null, null, 
       Calendar.MONTH)); 
    JSpinner.DateEditor dateDe = new JSpinner.DateEditor(dateJs, "MM/yy"); 
    dateJs.setEditor(dateDe); 
    window.add(dateJs); 

    setVisible(true); 
} 

public void createJOptionPane(){ 
    JOptionPane pane = new JOptionPane("Congratulations!"); 
    JDialog dialog = pane.createDialog(null, "Way to go! Please continue to work hard."); 
    dialog.show(); 

    choiceBg = new ButtonGroup(); 
    choiceJb = new JButton[] { 
     new JButton("Laugh"), 
     new JButton("I am worry"), 
     new JButton("I don't want to work anymore"), 
     new JButton("Present Options") }; 
    for (int i = 0; i < choiceJb.length; i++) { 
     choiceBg.add(choiceJb[i]); 
     pane.add(choiceJb[i]); 
    } 
    window.add(pane); 

} 
public void actionPerformed(ActionEvent e) { 
    //if(e.getSource() ==) 
    // { 
    // } 

    if(choiceJb[0].isSelected()) 
    { 
     pane.showMessageDialog(this, "Laugh", 
      "Don't laugh", pane.WARNING_MESSAGE); 
    } 
    else if (choiceJb[1].isSelected()) { 
     Object[] options = {"Because of my grades", 
       "Because of my SAT scores"}; 
     int n = pane.showConfirmDialog(this, 
      "Why are you worry?", "I am worry", JOptionPane.YES_NO_OPTION, 
      JOptionPane.QUESTION_MESSAGE, 
      null,  
      options, 
      options[0]); 
    } 
    else if (choiceJb[2].isSelected()) { 
     Object[] reasons = {"I just want to chill out", "I have no incentive to work hard", "I am tired"}; 
     String s = (String)pane.showInputDialog(this, "What is the reason that you don't want to work anymore?", 
      "Your reason?", pane.PLAIN_MESSAGE, null, reasons, "I just want to chill out"); 
    } 
    else if (choiceJb[3].isSelected()) { 
     JOptionPane.showOptionDialog(this, "Present Options", 
      "Simple Dialog", getButtonType(), getMessageType(), 
      null, substrings(ButtonLabels), null); 
    } 

} 
public void stateChanged(ChangeEvent e){ 
} 

class DoubleBufferedPanel extends JPanel { 
    private static final long serialVersionUID = 44L; 

    public void paint(Graphics g){ 
     super.paint(g); 
    } 
} 
} 

편집 된 버전 수입하고있는 javax.swing을. ; 가져 오기 java.awt.Container; import java.util.Calendar; import java.util.Date; import javax.swing.event.; 가져 오기 java.awt.event. *; 가져 오기 java.awt.Graphics; import java.lang.Comparable; import javax.swing.JOptionPane; import javax.swing.JDialog; import java.awt.Dimension; 가져 오기 java.awt.FlowLayout;

public class JSpinnerrDemo extends JApplet 
implements ActionListener, ChangeListener{ 
private static final long serialVersionUID = 2005L; 

private Container window; 
private JButton  btnEnter; 
private JOptionPane pane; 
private JLabel  dateJl; 
private JButton[] choiceJb; 
private ButtonGroup choiceBg; 
private JDialog  dialog; 

//spinner.setEditor(new JSpinner.DateEditor(spinner, "MM/yyyy")); 
public void init() { 
    window = new DoubleBufferedPanel(); 
    setContentPane(window); 

    createAppearance(); 
    createGUI(); 
    createJOptionPane(); 

} 

public void createAppearance(){ 
    window.setLayout(null); 
} 

public void createGUI(){ 
    dateJl = new JLabel("When are you going to graduate from High School?"); 
    dateJl.setSize(400, 30); 
    dateJl.setLocation(20, 120); 
    window.add(dateJl); 

    btnEnter = new JButton("Enter"); 
    btnEnter.setSize(100, 30); 
    btnEnter.setLocation(220, 180); 
    btnEnter.addActionListener(this); 
    window.add(btnEnter); 

    Date today = new Date(); 

    JSpinner dateJs = new JSpinner(new SpinnerDateModel(today, null, null, 
       Calendar.MONTH)); 
    JSpinner.DateEditor dateDe = new JSpinner.DateEditor(dateJs, "MM/yy"); 
    dateJs.setEditor(dateDe); 
    dateJs.setLocation(20, 160); 
    dateJs.setSize(100, 30); 
    window.add(dateJs); 
    dateJs.addChangeListener(this); 
    dateJs.setVisible(true); 

} 

public void createJOptionPane(){ 
    JOptionPane pane = new JOptionPane("Way to go! Please continue to work hard."); 
    JDialog dialog = pane.createDialog(null, "Congratulations!"); 

    dialog.setSize(new Dimension(300, 150)); 
    dialog.setLocationRelativeTo(this); 
    dialog.setLayout(new FlowLayout()); 
    dialog.setVisible(false); 

    choiceBg = new ButtonGroup(); 
    choiceJb = new JButton[] { 
     new JButton("Laugh"), 
     new JButton("I am worry"), 
     new JButton("I don't want to work anymore"), 
     new JButton("What?") }; 
    for (int i = 0; i < choiceJb.length; i++) { 
     choiceBg.add(choiceJb[i]); 
     dialog.add(choiceJb[i]); 
    } 

} 

public void actionPerformed(ActionEvent e) { 
    if(e.getSource() == btnEnter) 
    { 
     dialog.setVisible(true); 
     window.add(dialog); 
     //JOptionPane.showMessageDialog(); 
    } 

    if(choiceJb[0].isSelected()) 
    { 
     JOptionPane.showMessageDialog(this, "Laugh", 
      "Don't laugh", JOptionPane.WARNING_MESSAGE); 
    } 
    else if (choiceJb[1].isSelected()) { 
     Object[] options = {"Because of my grades", 
       "Because of my SAT scores"}; 
     JOptionPane.showOptionDialog(this, 
      "Why are you worry?", 
      "I am worry", 
      JOptionPane.YES_NO_OPTION, 
      JOptionPane.QUESTION_MESSAGE, 
      null,  //do not use a custom Icon 
      options, //the titles of buttons 
      options[0]); //default button title 
     // showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon) 
    } 
    else if (choiceJb[2].isSelected()) { 
     Object[] reasons = {"I just want to chill out", "I have no incentive to work hard", "I am tired"}; 
     JOptionPane.showInputDialog(this, "What is the reason that you don't want to work anymore?", 
      "Your reason?", JOptionPane.PLAIN_MESSAGE, null, reasons, "I just want to chill out"); 

    } 
    else if (choiceJb[3].isSelected()) { 
     Object[] optionss = {"Leave me alone", 
       "Bye"}; 

     JOptionPane.showOptionDialog(this, "What what?", 
      "What?", JOptionPane.YES_NO_CANCEL_OPTION, 
      JOptionPane.QUESTION_MESSAGE, 
      null, 
      optionss, 
      optionss[0]); 

    } 
} 

public void stateChanged(ChangeEvent e){ 
} 

class DoubleBufferedPanel extends JPanel { 
    private static final long serialVersionUID = 44L; 

    public void paint(Graphics g){ 
     super.paint(g); 
    } 
} 

}

답변

0

ChangeListenerJSpinnerdateJs보다는 JButtonbtnEnter위한 것입니다. 당신은 사용할 수 있습니다

dateJs.addChangeListener(new ChangeListener() { 
    @Override 
    public void stateChanged(ChangeEvent e) { 
     SpinnerModel dateModel = dateJs.getModel(); 
     if (dateModel instanceof SpinnerDateModel) { 
     Date date = ((SpinnerDateModel)dateModel).getDate(); 
     // do stuff with date... 
     } 
    } 
}); 

사이드 노트 :

  • 방법 showJDialog 창을 표시 setVisible 찬성 사용되지 입니다.
  • layout manager을 사용하면 recommended by Sun/Oracle입니다. 구성 요소의 크기 조정 및 위치 지정을 관리합니다.
  • DoubleBufferedPanelpaint 메서드가 비어있는 경우 paintComponent을 무시하고 super.paintComponent(g)을 재정 의하여 스윙의 사용자 지정 그림을 완성해야합니다.

편집 :

당신은 dialog 변수를 미행하고 있습니다. 좋아

dialog = pane.createDialog(null, "Congratulations!"); 
+0

JDialog dialog = pane.createDialog(null, "Congratulations!"); 

교체 여기 나의 새로운 코드의 ... 내가 btnEnter /// –

+0

참조 업데이트를 누를 때 JDialog를 활성화에 대한 문제가 무엇인지 모르는 .... ... – Reimeus

+0

정말 고마워요. :) –