gpt4 book ai didi

Java:某些表单元素被其他元素部分覆盖

转载 作者:行者123 更新时间:2023-12-02 06:14:29 25 4
gpt4 key购买 nike

我设计了一个表单,用于在框架内显示图表和表格。这是代码:

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() {

}
});
}
}

问题是,当我运行这个时,我的表格与图表重叠。我该如何解决这个问题?

这是屏幕截图: enter image description here

编辑:我用简单的图像替换了图表。它覆盖了复选框面板,当我尝试调整窗口大小时,它也会导致一些故障。这是我编辑的代码:

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() {

}
});
}
}

屏幕截图:

enter image description here

最佳答案

这不是答案。只是一个建议,对于评论来说太长了

另一个建议,因为我认为这个问题可能与我们昨天的讨论有关,您正在尝试将动态组件添加到框架中 - 相反,不要这样做。 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 执行此操作,因为它有点复杂。所以我现在建议第一个选择。

关于Java:某些表单元素被其他元素部分覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21623787/

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