Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
6年前关闭。
好吧,让我解释一下我的问题:
码:
输出:
备用代码:
输出:
一些事实:
focusOwner是扩展Answer的类Answer,它扩展了JPanel。 toString()方法永远不会被覆盖。证明:
元件:
focusOwner.getPage()返回其私有变量之一,该私有变量为null或不为null(在这种情况下不是)。
focusOwner.getText()调用类中包含的JTextArea的getText()方法并返回其返回值(在这种情况下,它实际上是一个空字符串)。
那里。这是我可以给您的最接近SSCCE的东西。
编辑:
您如何解释这一点:
返回此:
我们正在谈论的是在两个连续的代码行中打印出SAME变量的值,并且它没有相同的值。
打印
或两个
你想要的是
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
6年前关闭。
好吧,让我解释一下我的问题:
码:
System.out.println("Message is:" + focusOwner + focusOwner.getPage() == null + focusOwner.getText());
输出:
false
备用代码:
System.out.println("Message is:" + focusOwner);
输出:
Message is:pscript.gui.elements.Answer[,0,98,639x22,invalid,layout=net.miginfocom.swing.MigLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@677b15f3,flags=9,maximumSize=,minimumSize=,preferredSize=]
一些事实:
focusOwner是扩展Answer的类Answer,它扩展了JPanel。 toString()方法永远不会被覆盖。证明:
public class Answer extends Element
{
private static final long serialVersionUID = 1L;
private JLabel letter;
private JCheckBox check;
private Question question;
//getter and setters
void setQuestion(Question question)
{
this.question = question;
}
public Question getQuestion()
{
return question;
}
JLabel getLetter()
{
return letter;
}
public boolean isCorrect()
{
return check.isSelected();
}
public String getLetterText()
{
return letter.getText();
}
//navigational methods
@Override
public Element getPreviousElement()
{
Element previousElement = getQuestion().getPreviousAnswer((Answer) getElement());
if (previousElement != null)
{
return previousElement;
}
else
{
return getQuestion();
}
}
@Override
public Element getNextElement()
{
Element nextElement = getQuestion().getNextAnswer((Answer) getElement());
if (nextElement != null)
{
return nextElement;
}
else
{
nextElement = getPage().getNextElement(getQuestion());
if (nextElement != null)
{
return nextElement;
}
else
{
Page nextPage = getChapter().getNextPage(getPage());
if (nextPage != null)
{
return nextPage.getLastElement().lastElement();
}
else
{
Chapter nextChapter = getDocument().getNextChapter(getChapter());
if (nextChapter != null)
{
return nextChapter.getTitle();
}
else
{
return getDocument().getChapter(1).getTitle();
}
}
}
}
}
//other methods
private Element addNextElement()
{
if (getQuestion().getAnswerCount() < 5)
{
return addNextAnswer();
}
else
{
return addNextQuestion();
}
}
private Answer addNextAnswer()
{
Answer nextAnswer = new Answer();
getQuestion().addAnswer(nextAnswer, getQuestion().getAnswerIndex((Answer) getElement())+1);
getQuestion().rewriteLetters();
return nextAnswer;
}
private Question addNextQuestion()
{
Question nextQuestion = new Question();
getPage().addElement(nextQuestion, getPage().getElementIndex(getQuestion())+1);
getChapter().rewriteNumbers();
return nextQuestion;
}
private Element removeAnswer()
{
Element previousElement = getPreviousElement();
Question question = getQuestion();
question.removeAnswer((Answer)getElement());
question.rewriteLetters();
return previousElement;
}
private void checkLoop()
{
Question question = getQuestion();
if (question != null)
{
getDocument().setSaved(false);
if ((check.isSelected()) && (question.checkFlag == false))
{
question.checkFlag = true;
int answerCount = question.getAnswerCount();
for (int i = 1; i <= answerCount; i++)
{
question.getAnswer(i).check.setSelected(false);
}
check.setSelected(true);
question.checkFlag = false;
}
}
}
//actions
@Override
void primaryAction()
{
addNextElement().takeFocus();
}
@Override
void secondaryAction()
{
addNextQuestion().takeFocus();
}
@Override
void deleteAction()
{
removeAnswer().takeFocus();
}
//constructors
Answer()
{
super();
setBackground(Color.WHITE);
//TODO
check = new JCheckBox();
check.setBackground(Color.WHITE);
check.addItemListener(new ItemListener()
{
@Override
public void itemStateChanged(ItemEvent arg0)
{
checkLoop();
}
});
add(check, "top, gapleft " + LayoutConstants.CHECK_SPACE_LEFT + ", gaptop "+ LayoutConstants.CHECK_ABOVE + ", w " + LayoutConstants.CHECK_WIDTH);
letter = new JLabel("a)", SwingConstants.LEFT);
letter.setFont(Main.otherFont);
letter.setBackground(Color.WHITE);
add(letter, "top, gaptop " + LayoutConstants.LETTER_ABOVE + ", w " + LayoutConstants.LETTER_WIDTH);
getTextBox().setMargin(LayoutConstants.ANSWER_INSETS);
add(getTextBox(), "span, grow, push, w " + LayoutConstants.ANSWERBOX_WIDTH);
}
public Answer(String text, boolean correct)
{
this();
getTextBox().setText(text);
check.setSelected(correct);
}
}
元件:
public class Element extends JPanel
{
private static final long serialVersionUID = 1L;
private JTextArea textBox = new JTextArea();
private Page page = null;
private InputMap inputM = getTextBox().getInputMap();
private ActionMap actionM = getTextBox().getActionMap();
//getter and setters
Element getElement()
{
return this;
}
void setPage(Page page)
{
this.page = page;
}
public Page getPage()
{
return page;
}
Chapter getChapter()
{
return getPage().getChapter();
}
Document getDocument()
{
return getChapter().getDocument();
}
public JTextArea getTextBox()
{
return textBox;
}
public String getText()
{
return getTextBox().getText();
}
public void setText(String text)
{
getTextBox().setText(text);
}
public boolean isEmpty()
{
return getTextBox().getText().equals("");
}
//other methods
public void takeFocus()
{
getDocument().setFocusOwner(getElement());
scrollRectToVisible(getTextBox().getBounds());
getTextBox().grabFocus();
}
void addListener()
{
AbstractDocument document = (AbstractDocument) getTextBox().getDocument();
document.addDocumentListener(new DocumentListener()
{
@Override
public void changedUpdate(DocumentEvent e)
{
getDocument().setSaved(false);
}
@Override
public void insertUpdate(DocumentEvent e)
{
getDocument().setSaved(false);
}
@Override
public void removeUpdate(DocumentEvent e)
{
getDocument().setSaved(false);
}
});
}
Element lastElement()
{
if (getElement() instanceof Question)
{
if (((Question) getElement()).getAnswerCount() != 0)
{
return ((Question) getElement()).getLastAnswer();
}
}
return getElement();
}
//navigational methods
public Element getPreviousElement()
{
return null;
}
public Element getNextElement()
{
return null;
}
//actions
void primaryAction() {}
void secondaryAction() {}
private void tertiaryAction()
{
Chapter chapter = getChapter();
int chapterIndex = getDocument().getChapterIndex(chapter);
new CreateChapterDialog(getDocument(), chapterIndex+1); //TOD quaternary maybe??
getDocument().setKeyPressed(false);
}
void deleteAction() {}
private void upAction()
{
getPreviousElement().takeFocus();
}
private void downAction()
{
getNextElement().takeFocus();
}
//constructors
Element()
{
setLayout(new MigLayout("insets 0, gap 0!"));
getTextBox().setLineWrap(true);
getTextBox().setWrapStyleWord(true);
getTextBox().addKeyListener(new KeyAdapter()
{
@Override
public void keyReleased(KeyEvent e)
{
getDocument().setKeyPressed(false);
}
});
CustomDocumentFilter filter;
if (this instanceof Title)
{
getTextBox().setFont(Main.titleFont);
filter = new CustomDocumentFilter(250);
}
else if (this instanceof Question)
{
getTextBox().setFont(Main.questionFont);
filter = new CustomDocumentFilter(500);
}
else
{
getTextBox().setFont(Main.otherFont);
filter = new CustomDocumentFilter(500);
}
AbstractDocument document = (AbstractDocument) getTextBox().getDocument();
document.setDocumentFilter(filter);
//primary action
inputM.put(KeyStroke.getKeyStroke("ENTER"), "enter");
actionM.put("enter", new AbstractAction()
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent arg0)
{
if (!getDocument().isKeyPressed())
{
getDocument().setKeyPressed(true);
getDocument().setSaved(false);
primaryAction();
}
}
});
//secondary action
inputM.put(KeyStroke.getKeyStroke("shift ENTER"), "pressedShiftEnter");
actionM.put("pressedShiftEnter", new AbstractAction()
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
if (!getDocument().isKeyPressed())
{
getDocument().setKeyPressed(true);
getDocument().setSaved(false);
secondaryAction();
}
}
});
//tertiary action
inputM.put(KeyStroke.getKeyStroke("control ENTER"), "pressedCtrlEnter");
actionM.put("pressedCtrlEnter", new AbstractAction()
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
if (!getDocument().isKeyPressed())
{
getDocument().setKeyPressed(true);
getDocument().setSaved(false);
tertiaryAction();
}
}
});
//up action
inputM.put(KeyStroke.getKeyStroke("shift TAB"), "shiftTab");
actionM.put("shiftTab", new AbstractAction()
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
upAction();
}
});
//down action
inputM.put(KeyStroke.getKeyStroke("TAB"), "Tab");
actionM.put("Tab", new AbstractAction()
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
downAction();
}
});
//delete action
inputM.put(KeyStroke.getKeyStroke("shift DELETE"), "shiftDelete");
actionM.put("shiftDelete", new AbstractAction()
{
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e)
{
if (!getDocument().isKeyPressed())
{
getDocument().setKeyPressed(true);
getDocument().setSaved(false);
deleteAction();
}
}
});
getTextBox().addFocusListener(new FocusAdapter()
{
@Override
public void focusGained(FocusEvent e)
{
getDocument().setFocusOwner(getElement());
}
});
}
}
focusOwner.getPage()返回其私有变量之一,该私有变量为null或不为null(在这种情况下不是)。
focusOwner.getText()调用类中包含的JTextArea的getText()方法并返回其返回值(在这种情况下,它实际上是一个空字符串)。
那里。这是我可以给您的最接近SSCCE的东西。
编辑:
您如何解释这一点:
System.out.println("Message is:" + focusOwner + focusOwner.getPage() == null + focusOwner.getText());
System.out.println("Message is:" + focusOwner);
返回此:
false
Message is:pscript.gui.elements.Answer[,0,98,639x22,invalid,layout=net.miginfocom.swing.MigLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@1b2a8c70,flags=9,maximumSize=,minimumSize=,preferredSize=]
我们正在谈论的是在两个连续的代码行中打印出SAME变量的值,并且它没有相同的值。
最佳答案
假设问题是为什么
System.out.println("Message is:" + focusOwner + focusOwner.getPage() == null + focusOwner.getText());
打印
false
而不是在另一种情况下得到的长字符串:+
运算符的优先级高于==
运算符。因此,您要打印的是:("Message is:" + focusOwner + focusOwner.getPage()) == (null + focusOwner.getText())
或两个
String
对象之间相等性比较的结果。显然,双方并不平等。你想要的是
System.out.println("Message is:" + focusOwner + (focusOwner.getPage() == null) + focusOwner.getText());
08-16 19:04