简体   繁体   English

在JFrame表单上按下Jbutton时,不会出现导出

[英]Export doesn't appear when Jbutton is pressed on JFrame Form

I have a JButton on a JFrame form which I need to open a Scatter Diagram with, the coding for the scatter Diagram is on a normal JPanel but errors appear when I run it. 我在JFrame表单上有一个JButton,需要用它打开一个散点图,该散点图的代码在普通的JPanel上,但是运行它时会出现错误。 It worked when I did the same for a JFrame to JFrame. 当我对JFrame到JFrame执行相同操作时,它起作用了。

Here's the code for the scatter diagram: 这是散点图的代码:

public class X1 extends JFrame implements ActionListener {
double[] x = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.5, 2.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.5, 1.5, 1.5, 1.0, 1.0, 1.5, 1.5, 1.5, 1.5, 3, 1.5, 1.5};
double[] y = {4.9176, 5.0208, 4.5429, 4.5573, 5.0597, 3.8910, 5.8980, 5.6039, 16.4202, 14.4598, 5.8282, 5.3003, 6.2712, 5.9592, 5.0500, 5.6039, 8.2464, 6.6969, 7.7841, 9.0384, 5.9894, 7.5422, 8.7951, 6.0931, 8.3607, 8.1400, 9.1416, 12.0000};
SimpleRegression sr = new SimpleRegression();
Plot2DPanel plot = new Plot2DPanel();
JTextArea resultados = new JTextArea();
JTextArea result = new JTextArea();

public X1() {
    for (int i = 0; i < x.length; i++) {
        sr.addData(x[i], y[i]);
    }
    double[] yc = new double[y.length];
    for (int i = 0; i < x.length; i++) {
        yc[i] = sr.predict(x[i]);
    }
    plot.addLegend("South");
    plot.addScatterPlot("Data", x, y);
    plot.addLinePlot("Regression", x, yc);
    plot.setFixedBounds(0, 0, 4);
    BaseLabel Title = new BaseLabel("Regression Line", Color.BLUE, 0.5, 1.1);
    plot.addPlotable(Title);
    result.append("/ny-Intercept:" + sr.getIntercept());

    JFrame frame = new JFrame("Regression Line");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(900, 600);
    frame.add(plot, BorderLayout.CENTER);
    frame.setVisible(true);

}

public void actionPerformed(ActionEvent e) {
}

public static void main(String[] args) {
    new X1();
}
}

And here's the code source when I double click the JButton on JFrame 这是当我双击JFrame上的JButton时的代码源

package testinggraph;

/**
 *
 * @author as776
 */
public class Graph1 extends javax.swing.JFrame {

/**
 * Creates new form Graph1
 */
public Graph1() {
    initComponents();
}   

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    new X1 ();
    X1 G1 = new X1 ();
    X1.setVisible(true);
}                                                 

These are the main errors that occur when running the file: 这些是运行文件时发生的主要错误:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous ctor sym type: testinggraph.X1.<init>
      at testinggraph.Graph1.jButton1ActionPerformed(Graph1.java:191)
      at testinggraph.Graph1.access$000(Graph1.java:13)
    at testinggraph.Graph1.jButton1ActionPerformed(Graph1.java:191)
    at testinggraph.Graph1.access$000(Graph1.java:13)       

The code in jButton1ActionPerformed is wrong: The first line should be enough, actually, as this will create a new X1 object, which sets itself visible. jButton1ActionPerformed的代码是错误的:实际上,第一行应该足够,因为这将创建一个新的X1对象,该对象将自身设置为可见。 The second line creates another instance (what for??) and assigns it to G1 (please note: in Java, it is advised to use lower-case characters for starting variables). 第二行创建另一个实例(代表??)并将其分配给G1 (请注意:在Java中,建议使用小写字母字符作为起始变量)。 The third line then is invalid: It calls setVisible on the class X1 , likely you wanted to call that on the instance G1 instead. 第三行无效:它在类X1上调用setVisible ,可能是您想在实例G1上调用它。

But as I said, the first line in that method should be enough if the rest works correctly. 但是正如我所说,如果其余方法都能正常工作,则该方法的第一行就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM