我正在尝试创建gui,它会在重新加载XML文件后更改其部分元素(应用程序在开始时根据XML文件的内容创建少量GUI元素)。
启动应用程序后一切正常(如果加载了test.xml,则创建20个按钮[10个单词翻译对],加载testTwo.xml时创建4个按钮[2个单词翻译对]),但我不知道如何重新加载或随机播放GUI内容(点击按钮后)。
我试过把revalidate();在ActionListener中,但它在我的应用程序中不起作用。
如果你能指出我如何做到这一点,我将很高兴。
GUI:XmlGui.java
package xmltest;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import net.miginfocom.swing.MigLayout;
import org.xml.sax.SAXException;
public class XmlGui extends JFrame {
protected JPanel panel;
protected JFrame frame;
protected File file = new File("src/xmltest/test.xml");
protected JButton setOne, setTwo, shuffle;
protected JTextArea text;
protected XmlTest engine;
public XmlGui() throws ParserConfigurationException, SAXException, IOException {
XmlEvent xmlEvent = new XmlEvent(this);
SAXParserFactory spfac = SAXParserFactory.newInstance();
SAXParser sp = spfac.newSAXParser();
XmlTest engine = new XmlTest();
sp.parse(file, engine);
engine.readList();
engine.shuffleList(1);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
panel = new JPanel(new MigLayout("Insets 0"));
setOne = new JButton("Default (test.xml)");
setOne.addActionListener(xmlEvent);
panel.add(setOne, "split 3");
setTwo = new JButton("Next set (testTwo.xml)");
setTwo.addActionListener(xmlEvent);
panel.add(setTwo);
shuffle = new JButton("Shuffle");
shuffle.addActionListener(xmlEvent);
panel.add(shuffle, "wrap");
for (int i = 0; i < engine.cardList.size(); i++) {
JPanel xpanel = new JPanel(new MigLayout("Insets 0"));
final String test = engine.cardList.get(i).getTextOne();
JButton word = new JButton(engine.cardList.get(i).getWord()+" ("+i+")");
word.setPreferredSize(new Dimension(200, 40));
word.addActionListener(new ActionListener(){
public void ActionListener(ActionEvent event) {
}
@Override
public void actionPerformed(ActionEvent e) {
text.setText(test);
}
});
JButton translation = new JButton(engine.cardList.get(i).getTranslation()+" ("+i+") ");
translation.setName("translation"+i);
translation.setPreferredSize(new Dimension(200, 40));
xpanel.add(word);
xpanel.add(translation);
panel.add(xpanel, "wrap");
}
text = new JTextArea();
text.setLineWrap(true);
text.setPreferredSize(new Dimension(400, 45));
panel.add(text);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] arguments) throws ParserConfigurationException, SAXException, IOException {
XmlGui gui = new XmlGui();
}
}
XML方法:XmlTest.java
package xmltest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XmlTest extends DefaultHandler {
Card card;
private String temp;
ArrayList<Card> cardList = new ArrayList<Card>();
public void characters(char[] buffer, int start, int length) {
temp = new String(buffer, start, length);
}
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
temp = "";
if (qName.equalsIgnoreCase("Card")) {
card = new Card();
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("Card")) {
cardList.add(card);
} else if (qName.equalsIgnoreCase("Word")) {
card.setWord(temp);
} else if (qName.equalsIgnoreCase("Translation")) {
card.setTranslation(temp);
} else if (qName.equalsIgnoreCase("TextOne")) {
card.setTextOne(temp);
} else if (qName.equalsIgnoreCase("TextTwo")) {
card.setTextTwo(temp);
}
}
public void readList() {
System.out.println("Number of cards in the collection: " + cardList.size() + ".\n");
Iterator<Card> it = cardList.iterator();
while (it.hasNext()) {
System.out.println(it.next().toString());
}
}
public void shuffleList(int x) {
System.out.println("Shuffled cards order ("+x+"): ");
Collections.shuffle(cardList);
Iterator<Card> it = cardList.iterator();
while (it.hasNext()) {
System.out.print(it.next().shuffledList());
}
System.out.println("\n");
}
}
事件类:XmlEvent.java
package xmltest;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class XmlEvent implements ActionListener {
XmlGui xmlGui;
XmlEvent (XmlGui in) {
xmlGui = in;
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source.equals(xmlGui.setOne)) {
xmlGui.file = new File("src/xmltest/test.xml");
xmlGui.text.setText("test.xml");
}
if (source.equals(xmlGui.setTwo)) {
xmlGui.file = new File("src/xmltest/testTwo.xml");
xmlGui.text.setText("testTwo.xml");
}
if (source.equals(xmlGui.shuffle)) {
xmlGui.text.setText("Shuffling list!");
}
}
}
表示XML数据:Card.java
package xmltest;
public class Card {
private String word;
private String translation;
private String textOne;
private String textTwo;
public Card() {
}
public Card(String word, String translation, String textOne, String textTwo) {
this.word = word;
this.translation = translation;
this.textOne = textOne;
this.textTwo = textTwo;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getTranslation() {
return translation;
}
public void setTranslation(String translation) {
this.translation = translation;
}
public String getTextOne() {
return textOne;
}
public void setTextOne(String textOne) {
this.textOne = textOne;
}
public String getTextTwo() {
return textTwo;
}
public void setTextTwo(String textTwo) {
this.textTwo = textTwo;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("Card details:");
sb.append("\nWord: " + getWord());
sb.append("\nTranslation: " + getTranslation());
sb.append("\nTextOne: " + getTextOne());
if(getTextTwo().equals("blank")) {
sb.append("\n\n");
} else {
sb.append("\nTextTwo: " + getTextTwo()+"\n\n");
}
return sb.toString();
}
public String shuffledList() {
return getWord()+" ";
}
}
XML1:test.xml
<?xml version="1.0" encoding="UTF-8"?>
<cards>
<card>
<word>cloud</word>
<translation>chmura</translation>
<textOne>D: a visible collection of particles of water or ice suspended in the air.</textOne>
<textTwo>blank</textTwo>
</card>
<card>
<word>rain</word>
<translation>deszcz</translation>
<textOne>D: water that is condensed from the aqueous vapor in the atmosphere and falls to earth in drops.</textOne>
<textTwo>blank</textTwo>
</card>
<card>
<word>wind</word>
<translation>wiatr</translation>
<textOne>D: air in natural motion, as that moving horizontally at any velocity along the earth's surface.</textOne>
<textTwo>blank</textTwo>
</card>
<card>
<word>storm</word>
<translation>burza</translation>
<textOne>D: a disturbance of the normal condition of the atmosphere, manifesting itself by winds of unusual force or direction, often accompanied by rain, snow, hail, thunder, and lightning, or flying sand or dust.</textOne>
<textTwo>blank</textTwo>
</card>
<card>
<word>blizzard</word>
<translation>zamieć</translation>
<textOne>D: a storm with dry, driving snow, strong winds, and intense cold.</textOne>
<textTwo>blank</textTwo>
</card>
<card>
<word>fog</word>
<translation>mgła</translation>
<textOne>D: a cloudlike mass or layer of minute water droplets or ice crystals near the surface of the earth, appreciably reducing visibility.</textOne>
<textTwo>blank</textTwo>
</card>
<card>
<word>sunny</word>
<translation>słoneczny</translation>
<textOne>D: abounding in sunshine.</textOne>
<textTwo>test</textTwo>
</card>
<card>
<word>weather</word>
<translation>pogoda</translation>
<textOne>D: the state of the atmosphere with respect to wind, temperature, cloudiness, moisture, pressure, etc.</textOne>
<textTwo>blank</textTwo>
</card>
<card>
<word>temperature</word>
<translation>temperatura</translation>
<textOne>D: a measure of the warmth or coldness of an object or substance with reference to some standard value.</textOne>
<textTwo>blank</textTwo>
</card>
<card>
<word>hail</word>
<translation>grad</translation>
<textOne>D: showery precipitation in the form of irregular pellets or balls of ice.</textOne>
<textTwo>blank</textTwo>
</card>
</cards>
XML2:testTwo.xml
<?xml version="1.0" encoding="UTF-8"?>
<cards>
<card>
<word>cloud</word>
<translation>chmura</translation>
<textOne>D: a visible collection of particles of water or ice suspended in the air.</textOne>
<textTwo>blank</textTwo>
</card>
<card>
<word>rain</word>
<translation>deszcz</translation>
<textOne>D: water that is condensed from the aqueous vapor in the atmosphere and falls to earth in drops.</textOne>
<textTwo>blank</textTwo>
</card>
</cards>
答案 0 :(得分:0)
我通过以下方式解决了我的问题:
工作正常。