调用时程序未绘制 GUI

Program is not painting the GUI when called

我是一名试图自学 GUI 基础知识的高中生。我正在尝试编写的程序将从用户那里获取两个数字,然后将这两个数字绘制成图表。如果他或她愿意,用户还可以输入更多积分。我遇到的问题是,当我按下打开应该是图形的 GUI 的按钮时,GUI 出现但它是空白的。我认为绘画方法以及设置和调用的方式存在问题,但我不确定。在此先感谢您的任何建议或帮助。

这是创建图形 GUI 的 class:

package dataGraph;
import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Graphics2D;

public class graphGUI extends JFrame {
//public Graphics2D g2;
public graphGUI() {
    setTitle("Data Graph");
    setSize(500, 500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
}

public void paint(Graphics g) {
    super.paintComponents(g);
    g.drawLine(40, 425, 450, 425);
    g.drawLine(40, 425, 40, 70);
    g.drawString("Graph", 20, 20);
    // g2.draw
    g.drawLine(50, 50, 50, 50);
     for(int i = 0; i < dataEntryGUI.x.size(); i++){
     g.drawOval(Integer.parseInt(dataEntryGUI.x.get(i)),
     Integer.parseInt(dataEntryGUI.y.get(i)),5,5);
     }
}

}

这是为数据输入创建 GUI 的 class 并具有允许用户添加更多 "points" 然后 "graph" 它们的动作侦听器:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;

public class dataEntryGUI extends JFrame implements ActionListener {

public static ArrayList<String> x;
public static ArrayList<String> y;

private Button btnAdd;
private Button btnGraph;
private Label lbl;
private Label lbl2;
private TextField xInt;
private TextField yInt;

public dataEntryGUI() {
    setTitle("Data entry");
    setSize(250, 250);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    lbl = new Label("x");

    lbl2 = new Label("y");

    // text fields
    xInt = new TextField();

    yInt = new TextField();
    x = new ArrayList<String>();
    y = new ArrayList<String>();

    // add button
    btnAdd = new Button("Add another");
    // btnAdd.setPreferredSize(new Dimension(70,30));
    btnAdd.addActionListener(this);
    btnGraph = new Button("Make Graph");
    btnGraph.addActionListener(this);
    add(lbl);
    add(xInt);
    add(lbl2);
    add(yInt);
    add(btnAdd);
    add(btnGraph);
    setLayout(new FlowLayout());
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {

    // System.out.println("boogers");

    if (e.getSource() == btnAdd) {
        //xInt.getText();

        x.add(xInt.getText());
        y.add(yInt.getText());
        xInt.setText("");
        yInt.setText("");

    } else {
        graphGUI graph = new graphGUI();
        graph.repaint();
    }
}

}

这是主要方法:

package dataGraph;
public class dataGraphMain {

public static void main(String[] args) {
    dataEntryGUI gui = new dataEntryGUI();
    //graphGUI gui2 = new graphGUI();

}

}
public void paint(Graphics g) {
    super.paintComponents(g);

一般来说,无论何时覆盖一个方法,您都应该在覆盖的方法名称上调用 "super",而不是其他方法。在这种情况下,您将调用:

super.paint(g);

但是,对于自定义绘画,您不应覆盖 JFrame 的 paint() 方法。

相反,您应该覆盖 JPanel 的 paintComponent(...) 方法,然后将面板添加到框架中。阅读有关 Custom Painting 的 Swing 教程部分,了解更多信息和示例。

I am a high school student trying to teach myself GUI basics.

将 link 到 Table 的 Swing 教程内容放在手边,以备将来参考,因为本教程涵盖了基础知识和更多内容。

正如 camickr 所提到的,应该进行一些重构。也许这会有所帮助。

首先,您正在尝试重新绘制 JFrame。不推荐这样做。您要做的是定义一个要处理的 JPanel 并将其添加到框架的窗格内容中。

我稍微重构了您的代码,使 GraphGUI 现在是 JPanel,而不是 JFrame。这样,您可以将它用作您将来可能创建的任何 JFrame 中的组件。

其次,我在 DataEntryGUI 中定义了一个函数来创建一个新框架,该框架将保存图形组件并对其进行绘制。完整代码如下:


Class GraphGUI


package dataGraph;

import javax.swing.*;
import java.awt.*;

import java.awt.Graphics;

public class graphGUI extends JPanel {

    public GraphGUI() {

        setBackground(Color.WHITE);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawLine(40, 425, 450, 425);
        g.drawLine(40, 425, 40, 70);
        g.drawString("Graph", 20, 20);
        g.drawLine(50, 50, 50, 50);

        for(int i = 0; i < dataEntryGUI.x.size(); i++){
            g.drawOval(Integer.parseInt(dataEntryGUI.x.get(i)),
                    Integer.parseInt(dataEntryGUI.y.get(i)),5,5);
        }
    }
}

DataEntryGUI


package dataGraph;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;

public class DataEntryGUI extends JFrame implements ActionListener {

    public static ArrayList<String> x;  //This should be figured out by
    public static ArrayList<String> y;  //the OP. Encapsulation and
                                        //decoupling is a different matter.

    private Button btnAdd;
    private Button btnGraph;
    private Label lbl;
    private Label lbl2;
    private TextField xInt;
    private TextField yInt;


    public DataEntryGUI() {
        setTitle("Data entry");
        setSize(250, 250);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        lbl = new Label("x");

        lbl2 = new Label("y");

        // text fields
        xInt = new TextField();

        yInt = new TextField();
        x = new ArrayList<String>();
        y = new ArrayList<String>();

        // add button
        btnAdd = new Button("Add another");
        // btnAdd.setPreferredSize(new Dimension(70,30));
        btnAdd.addActionListener(this);
        btnGraph = new Button("Make Graph");
        btnGraph.addActionListener(this);
        add(lbl);
        add(xInt);
        add(lbl2);
        add(yInt);
        add(btnAdd);
        add(btnGraph);
        setLayout(new FlowLayout());
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {

        // System.out.println("boogers");

        if (e.getSource() == btnAdd) {
            //xInt.getText();

            x.add(xInt.getText());
            y.add(yInt.getText());
            xInt.setText("");
            yInt.setText("");

        } else {
            paintGraph();
        }
    }

    private void paintGraph() {
        JFrame graphFrame = new JFrame();
        GraphGUI graph = new GraphGUI();
        graphFrame.getContentPane().add(graph);

        graphFrame.setTitle("Data Graph");
        graphFrame.setSize(500, 500);
        graphFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        graphFrame.setResizable(false);
        graphFrame.setVisible(true);

    }

}

这似乎表现出您想要的行为。我有一些关于如何传递 x,y 数据的 thoughts/comments(以及它们 public 而不是私有的事实),但这超出了您在这里需要的范围。