我设计了一个表单,用于在框架内显示图形和表格。这是代码:
package ExaData;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.NumberFormat;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RefineryUtilities;
public class ShowGraph extends javax.swing.JFrame {
String[][] data;
String[] columns={"Tanım","Değer"};
Sessionn obj;
String SGA="SGA";
String PGA="PGA";
String JAVAPOOL="JAVAPOOL";
JCheckBox SGA_CHECK;
JCheckBox PGA_CHECK;
JCheckBox JAVA_POOL_CHECK;
JTable table;
JPanel btnPanel,tblPanel;
ChartPanel CP;
JFreeChart chart;
DefaultCategoryDataset dataset;
boolean sgaflag=true;
boolean pgaflag=true;
boolean javapoolflag=true;
JScrollPane scroller;
JTableHeader header ;
JCheckBox[] checkboxes;
boolean[] chkboxflag;
public ShowGraph()
{
// super();
SGA_CHECK=new JCheckBox("SGA");
PGA_CHECK=new JCheckBox("PGA");
JAVA_POOL_CHECK=new JCheckBox("JAVA POOL");
SGA_CHECK.setSelected(true);
PGA_CHECK.setSelected(true);
JAVA_POOL_CHECK.setSelected(true);
btnPanel= new JPanel(new FlowLayout());
tblPanel=new JPanel(new FlowLayout());
btnPanel.add(SGA_CHECK);
btnPanel.add(PGA_CHECK);
btnPanel.add(JAVA_POOL_CHECK);
this.add(btnPanel, BorderLayout.CENTER);
this.add(tblPanel,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
PGA_CHECK.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
pgaflag=e.getStateChange() == ItemEvent.SELECTED;
update();
}
});
SGA_CHECK.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
sgaflag=e.getStateChange() == ItemEvent.SELECTED;
update();
}
});
JAVA_POOL_CHECK.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
javapoolflag=e.getStateChange() == ItemEvent.SELECTED;
update();
}
});
}
private CategoryDataset createDataset() {
dataset = new DefaultCategoryDataset();
if(sgaflag){
dataset.addValue(32.4, SGA, "Node 1");
dataset.addValue(43.2, SGA, "Node 2");
dataset.addValue(23.0, SGA, "Node 3");
dataset.addValue(13.0, SGA, "Node 4");
}
if(pgaflag){
dataset.addValue(17.8, PGA, "Node 1");
dataset.addValue(15.6, PGA, "Node 2");
dataset.addValue(111.3, PGA, "Node 3");
dataset.addValue(11.8, PGA, "Node 4");
}
if(javapoolflag){
dataset.addValue(27.7, JAVAPOOL, "Node 1");
dataset.addValue(18.3, JAVAPOOL, "Node 2");
dataset.addValue(25.5, JAVAPOOL, "Node 3");
dataset.addValue(29.5, JAVAPOOL, "Node 4");
}
return dataset;
}
private JFreeChart createChart(CategoryDataset dataset) {
chart = ChartFactory.createStackedBarChart(
"EXADATA Görünümü", // chart title
"Node'lar", // domain axis label
"Yüzde Dağılım", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // the plot orientation
true, // legend
true, // tooltips
false // urls
);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
renderer.setRenderAsPercentages(true);
renderer.setDrawBarOutline(false);
renderer.setBaseItemLabelsVisible(true);
renderer.setSeriesPaint(0,Color.LIGHT_GRAY);
renderer.setSeriesPaint(2, Color.LIGHT_GRAY);
renderer.setBaseItemLabelGenerator(
new StandardCategoryItemLabelGenerator());
return chart;
}
public void veriCek(String[][] data)
{
this.data=data;
}
public void run()
{
chart = createChart(createDataset());
table =new JTable(data,columns);
table.getColumnModel().getColumn(0).setPreferredWidth(200);
table.getColumnModel().getColumn(1).setPreferredWidth(200);
table.setEnabled(false);
CP=new ChartPanel(chart);
scroller = new JScrollPane(table);
this.add(CP, BorderLayout.NORTH);
tblPanel.add(scroller,BorderLayout.SOUTH);
this.setSize(600,600);
RefineryUtilities.centerFrameOnScreen(this);
this.setVisible(true);
}
public void update()
{
this.remove(CP);
chart = createChart(createDataset());
CP=new ChartPanel(chart);
this.add(CP, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
}
});
}
}
问题是,当我运行它时,我的表格与图表重叠。我该如何解决?
以下是截图:
编辑:我用简单的图片替换了图表。它覆盖了复选框面板,当我尝试调整窗口大小时,它也会导致一些故障。这是我编辑的代码:
package ExaData;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.text.NumberFormat;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RefineryUtilities;
public class ShowGraph extends javax.swing.JFrame {
String[][] data;
String[] columns={"Tanım","Değer"};
Sessionn obj;
String SGA="SGA";
String PGA="PGA";
String JAVAPOOL="JAVAPOOL";
JCheckBox SGA_CHECK;
JCheckBox PGA_CHECK;
JCheckBox JAVA_POOL_CHECK;
JTable table;
JPanel btnPanel,tblPanel,cPanel;
ChartPanel CP;
JFreeChart chart;
DefaultCategoryDataset dataset;
boolean sgaflag=true;
boolean pgaflag=true;
boolean javapoolflag=true;
JScrollPane scroller;
JTableHeader header ;
JCheckBox[] checkboxes;
boolean[] chkboxflag;
public ShowGraph()
{
// super();
SGA_CHECK=new JCheckBox("SGA");
PGA_CHECK=new JCheckBox("PGA");
JAVA_POOL_CHECK=new JCheckBox("JAVA POOL");
SGA_CHECK.setSelected(true);
PGA_CHECK.setSelected(true);
JAVA_POOL_CHECK.setSelected(true);
btnPanel= new JPanel(new FlowLayout());
tblPanel=new JPanel(new FlowLayout());
cPanel=new JPanel(new FlowLayout());
btnPanel.add(SGA_CHECK);
btnPanel.add(PGA_CHECK);
btnPanel.add(JAVA_POOL_CHECK);
this.add(cPanel,BorderLayout.NORTH);
this.add(btnPanel, BorderLayout.CENTER);
this.add(tblPanel,BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
PGA_CHECK.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
pgaflag=e.getStateChange() == ItemEvent.SELECTED;
update();
}
});
SGA_CHECK.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
sgaflag=e.getStateChange() == ItemEvent.SELECTED;
update();
}
});
JAVA_POOL_CHECK.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
javapoolflag=e.getStateChange() == ItemEvent.SELECTED;
update();
}
});
}
private CategoryDataset createDataset() {
dataset = new DefaultCategoryDataset();
//BURADA ANA TANIMLAMA BİTTİ
if(sgaflag){
dataset.addValue(32.4, SGA, "Node 1");
dataset.addValue(43.2, SGA, "Node 2");
dataset.addValue(23.0, SGA, "Node 3");
dataset.addValue(13.0, SGA, "Node 4");
}
if(pgaflag){
dataset.addValue(17.8, PGA, "Node 1");
dataset.addValue(15.6, PGA, "Node 2");
dataset.addValue(111.3, PGA, "Node 3");
dataset.addValue(11.8, PGA, "Node 4");
}
if(javapoolflag){
dataset.addValue(27.7, JAVAPOOL, "Node 1");
dataset.addValue(18.3, JAVAPOOL, "Node 2");
dataset.addValue(25.5, JAVAPOOL, "Node 3");
dataset.addValue(29.5, JAVAPOOL, "Node 4");
}
return dataset;
}
private JFreeChart createChart(CategoryDataset dataset) {
chart = ChartFactory.createStackedBarChart(
"EXADATA Görünümü", // chart title
"Node'lar", // domain axis label
"Yüzde Dağılım", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // the plot orientation
true, // legend
true, // tooltips
false // urls
);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance());
StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
renderer.setRenderAsPercentages(true);
renderer.setDrawBarOutline(false);
renderer.setBaseItemLabelsVisible(true);
renderer.setSeriesPaint(0,Color.LIGHT_GRAY);
renderer.setSeriesPaint(2, Color.LIGHT_GRAY);
renderer.setBaseItemLabelGenerator(
new StandardCategoryItemLabelGenerator());
return chart;
}
public void veriCek(String[][] data)
{
this.data=data;
}
public void run()
{
BufferedImage image = new BufferedImage(400,300,BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.BLUE);
g.drawLine(0, 0, 300, 400);
g.dispose();
JLabel l=new JLabel(new ImageIcon(image));
cPanel.add(l);
chart = createChart(createDataset());
table =new JTable(data,columns);
table.getColumnModel().getColumn(0).setPreferredWidth(200);
table.getColumnModel().getColumn(1).setPreferredWidth(200);
table.setEnabled(false);
// CP=new ChartPanel(chart);
// this.add(CP, BorderLayout.CENTER);
scroller = new JScrollPane(table);
tblPanel.add(scroller,BorderLayout.SOUTH);
// this.setSize(600,600);
this.pack();
RefineryUtilities.centerFrameOnScreen(this);
this.setVisible(true);
}
public void update()
{
this.remove(CP);
chart = createChart(createDataset());
CP=new ChartPanel(chart);
this.add(CP, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
}
});
}
}
截图:
答案 0 :(得分:2)
这不是答案。只是一个评论太长的建议
另一个建议,因为我认为这个问题可能与我们昨天的讨论有关,你们正试图在你的框架中添加动态组件 - 而不是这样做。 GUI Builder默认提供的GroupLayout
使得动态添加组件变得困难。只需创建一个已包含所有组件的JDialog
表单,然后通过其构造函数将数据传递给它。数据应该是制定表格和图表所需的数据。像这样的东西
public class ChartTableDialog extends JDialog {
private int[][] data;
public ChartTableDialog(final JFrame frame, boolean modal, int[][] data) {
super(frame, model);
this.data = data;
}
// somewhere else in the code you will use data[][] for formulate
// your chart and your table
}
然后在您的主JFrame
表单中,说按钮会触发JDialog
弹出,收集数据,然后将其传递给JDilaog
。像这样的东西
private void jButton1ActionPerformed(ActionEvent e) {
int[][] data = getSomeData();
new ChartTableDialog(this, true, data);
}
这将是最简单的方法。
另一种方法是使用CardLayout
,但我必须通过一个完整的教程来了解如何使用GUI Builder完成此操作,因为它有点复杂。所以我现在建议第一个选择。