您的方法遵循时尚。更好的解决方案是更新图表的模型
gData,并让图表自行更新。也,
不要在
JButton
; 上使用鼠标监听器 只需处理ActionEvent
。使用Java命名约定。
阿
JOptionPane
可具有多个输入字段,如图这里。
SSCCE:
public class Graphframe extends Jframe { Graphframe() { final GraphPanel gPanel = new GraphPanel(); gPanel.create(); JButton button = new JButton(new AbstractAction("Update") { @Override public void actionPerformed(ActionEvent e) { String a = JOptionPane.showInputDialog(rootPane, "ENTER FIRST VALUE", "", JOptionPane.PLAIN_MESSAGE); String b = JOptionPane.showInputDialog(rootPane, "ENTER SECOND VALUE", "", JOptionPane.PLAIN_MESSAGE); int aa = Integer.parseInt(a); int bb = Integer.parseInt(b); gPanel.update(aa, bb); } }); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.add(gPanel, BorderLayout.CENTER); this.add(button, BorderLayout.SOUTH); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } private static class GraphPanel extends JPanel { private DefaultCategoryDataset gData = new DefaultCategoryDataset(); void create() { update(56, 20); JFreeChart chart = ChartFactory.createBarChart("", "", "", gData, PlotOrientation.VERTICAL, false, false, false); ChartPanel chartPanel = new ChartPanel(chart); this.add(chartPanel); } private void update(int value1, int value2) { gData.clear(); gData.setValue(value1, "What you saved", ""); gData.setValue(value2, "What you paid", ""); } } public static void main(String args[]) { new Graphframe(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)