基本上,我有一个用于在其内部框架上绘制Graphs的类,它扩展了JPanel。我想到了一种方法,在不打扰当前图表的情况下向该面板添加按钮。我使用Java Window Builder b y th
方式
package motor_2_package;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GraphingData extends JPanel {
private static final Stroke GRAPH_STROKE = new BasicStroke(3f);
JFrame frame = new JFrame("DrawGraph");
private static final int GRAPH_POINT_WIDTH = 12;
final int PAD = 20;
int []data1;//=new int[d.getPartList().get(i).getSaleList().size()+1]; //it
returns size of sales list supply
int []data2;//=new int[d.getPartList().get(i).getSaleList().size()+1]; //it
returns size of sales list sold
int xs,ys;
details t=new details();
public GraphingData(details d,int i,int checker)
{
frame.getContentPane().setLayout(null);
JButton btnBack = new JButton("Back");
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MotorPartFrame mf=new MotorPartFrame(d);
mf.setVisible(true);
frame.dispose();
}
});
btnBack.setBounds(0, getHeight()-PAD, 89, 18);
frame.getContentPane().add(btnBack);
//paintComponent(g);
t=new details(d);
xs=d.getPartList().get(i).getSaleList().size()+1;
ys=d.getPartList().get(i).getSaleList().size()+1;
data1=new int[xs]; //it returns size of sales list supply
data2=new int[ys]; //it returns size of sales list sold
data1[0]=0;
data2[0]=0;
for(int j=1;j<=xs-1;j++)
{
data1[j]=j;
if(checker==1) data2[j]=d.getPartList().get(i).getSaleList().get(j
-1).getSupply();
else data2[j]=data2[j]=d.getPartList().get(i).getSaleList().get(j-
1).getSold();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// Draw ordinate.
//now we set x- axis border and y-axis border too
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
// Draw abcissa.
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
double xInc = (double)(w - 2*PAD)/(double) (30);
double scale=( double) (h-2*PAD)/getMax(2);
//now to write spikes on points
int mah=30,muh=getMax(2);
for (int i = 0; i<muh-1; i++) {
int x0 = PAD;
int x1 = GRAPH_POINT_WIDTH + PAD;
int y0 = getHeight() - (((i + 1) * (getHeight() - PAD * 2)) /(muh-1)
+PAD);
int y1 = y0;
g2.drawLine(x0, y0, x1, y1);
}
// and for x axis
for (int i = 0; i < mah; i++) {
int x0 = (i + 1) * (getWidth() - PAD * 2) / 30 + PAD;
int x1 = x0;
int y0 = getHeight() - PAD;
int y1 = y0 - GRAPH_POINT_WIDTH;
g2.drawLine(x0, y0, x1, y1);
}
//
Stroke oldStroke=g2.getStroke();
g2.setStroke(GRAPH_STROKE);
g2.setColor(Color.RED);
for(int i=0;i<xs-1;i++)
{
int x1=(int) (PAD+(xInc*data1[i]));
int y1=(int) (getHeight()-PAD-(scale*data2[i]));
int x2=(int) (PAD+(xInc*data1[i+1]));
int y2=(int) (getHeight()-PAD-(scale*data2[i+1]));
g2.drawLine(x1, y1, x2, y2);
}
//now for thesecond plot
g2.setStroke(oldStroke);
g2.setColor(Color.BLUE);
for (int i = 0; i < xs; i++) {
int x =(int) ((int) PAD+(xInc*data1[i]) - GRAPH_POINT_WIDTH / 2);
int y =(int) ((int) getHeight()-PAD-(scale)*data2[i] -
GRAPH_POINT_WIDTH / 2);;
int ovalW = GRAPH_POINT_WIDTH;
int ovalH = GRAPH_POINT_WIDTH;
g2.fillOval(x, y, ovalW, ovalH);
}
}
private int getMax(int x) {
int max = -Integer.MAX_VALUE;
if(x==1)
{
for(int i = 0; i < data1.length; i++) {
if(data1[i] > max)
max = data1[i];
}
}
else
{
for(int i = 0; i < data2.length; i++) {
if(data2[i] > max)
max = data2[i];
}
}
return max;
}
public void createAndShowGui(details d,int i)
{
GraphingData mainPanel = new GraphingData(d,i,1);
frame = new JFrame("DrawGraph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
enter code here
答案 0 :(得分:1)
有两种可能性:
使用LayoutManager,例如BorderLayout,将原始面板放在中间,将面板放在南边的按钮上。
您也可以自己绘制按钮,例如通过将两个填充的矩形放在彼此顶部并在其中放置一些文本,然后您需要保存坐标并检测该矩形中的鼠标点击。