gpt4 book ai didi

java - 刷新 jframe 并处理旧框架

转载 作者:行者123 更新时间:2023-12-01 12:31:40 26 4
gpt4 key购买 nike

我有一个java swing 程序,它在jtable 中显示xml 页面的内容。我需要显示帧每 1 分钟刷新一次。我需要处理旧框架并在刷新后显示新框架

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLTableExample {
JFrame frame = new JFrame("Frame");
public static void main(String[] args) {
new XMLTableExample();
}

public XMLTableExample() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException
| UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JTable table = new JTable();
table.setOpaque(false);

Color lav = new Color(220, 220, 250);

try {
table.setModel(new XMLTableModel(new File("Htmll.xml")));
Thread.sleep(6000);
//frame.dispose();

} catch (ParserConfigurationException | SAXException
| IOException | XPathExpressionException ex) {
ex.printStackTrace();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


JLabel label=new JLabel("label",JLabel.CENTER);
label.setText("Service Now Notification");
label.setFont(new Font("Courier New", Font.ITALIC, 30));
label.setForeground(Color.blue);

frame.add(label,BorderLayout.NORTH);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setUndecorated(true);

JButton aButton = new JButton("submit");

aButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

System.exit(0);
}
});
JScrollPane scroll = new JScrollPane(table);

scroll.getViewport().setBackground(lav);

frame.add(scroll);
frame.add(aButton, BorderLayout.SOUTH);
frame.pack();

frame.setLocationRelativeTo(null);
int frameWidth = 800;
int frameHeight = 135;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

frame.setBounds((int) screenSize.getWidth() - frameWidth, 0, frameWidth, frameHeight);

frame.setLocation((int) 220,590);
frame.setVisible(true);


}
});
}

public static class XMLTableModel extends AbstractTableModel {

/**
*
*/
private static final long serialVersionUID = 1L;
protected static final String[] COLUMN_NAMES = { "Number", "Priority",
"State", "Assigned", "Description", "Task" };
private List<MSEntity> rows;

public XMLTableModel(File file) throws ParserConfigurationException,
SAXException, IOException, XPathExpressionException {
rows = new ArrayList<>(25);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(file);

setDocument(dom);
}

protected String getMSValue(Node msNode, String name)
throws XPathExpressionException {
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xpath.compile("S[@N='" + name + "']");
Node sNode = (Node) expression
.evaluate(msNode, XPathConstants.NODE);
return sNode != null ? sNode.getTextContent() : null;
}

@Override
public int getRowCount() {
return rows.size();
}

@Override
public int getColumnCount() {
return COLUMN_NAMES.length;
}

@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}

@Override
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}

public MSEntity getEntityAtRow(int row) {
return rows.get(row);
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
MSEntity entity = getEntityAtRow(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = entity.getNumber();
break;
case 1:
value = entity.getPriority();
break;
case 2:
value = entity.getState();
break;
case 3:
value = entity.getAssigned();
break;
case 4:
value = entity.getDesc();
break;
case 5:
value = entity.getTask();
break;
}
return value;
}

public void setDocument(Document dom) throws XPathExpressionException {

XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xpath.compile("/Objs/Obj/MS");
NodeList nl = (NodeList) expression.evaluate(dom,
XPathConstants.NODESET);
for (int index = 0; index < nl.getLength(); index++) {
Node node = nl.item(index);
String number = getMSValue(node, "Number");
String priority = getMSValue(node, "Priority");
String state = getMSValue(node, "State");
String assigned = getMSValue(node, "Assigned");
String desc = getMSValue(node, "Short_desc");
String task = getMSValue(node, "Task");

MSEntity entity = new MSEntity(number, priority, state,
assigned, desc, task);
rows.add(entity);
}

fireTableDataChanged();

}

public class MSEntity {
private final String number;
private final String priority;
private final String state;
private final String assigned;
private final String desc;
private final String task;

public MSEntity(String number, String priority, String state,
String assigned, String desc, String task) {
this.number = number;
this.priority = priority;
this.state = state;
this.assigned = assigned;
this.desc = desc;
this.task = task;
}

public String getAssigned() {
return assigned;
}

public String getDesc() {
return desc;
}

public String getNumber() {
return number;
}

public String getPriority() {
return priority;
}

public String getState() {
return state;
}

public String getTask() {
return task;
}

}

}

}

我的xml文件是这样的

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>Selected.System.Management.Automation.PSCustomObject</T>
<T>System.Management.Automation.PSCustomObject</T>
<T>System.Object</T>
</TN>
<MS>
<S N="Number">INC0811168</S>
<S N="Priority">2 - High</S>
<S N="State">Assigned</S>
<S N="Assigned">New</S>
<S N="Short_desc">Review Ad-Hoc Service Request for Lavon A Gudmundson</S>
<S N="Task">Catalog Task</S>
</MS>
</Obj>
</Objs>

我试过了

frame.dispose() but the frame is not being refreshed

最佳答案

我用过

static JFrame frame = new JFrame("Frame");

问题解决了

关于java - 刷新 jframe 并处理旧框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25883555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com