Painting a JTextField while using Graphics2D

21 views Asked by At

I am trying to do a simple calculator and I've decided to use Graphics2D. I have no trouble painting some Jlabels, but when I try to paint a JTextField, everything on the JFrame disappeared and only a line that I put in the "public void paint(Graphics g)" stays painted on the JFrame. I am using a separeate classes which I've labeled MyFrame and MyPanel. I am setting up the labels and text fields in MyFrame and the Graphics2D is in the MyPanel class. Just to note that I am a beginner in Java coding

Here is the MyPanel code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class MyPanel extends JPanel{
    
    MyPanel(){
        this.setPreferredSize(new Dimension(500, 500));
        this.setLayout(null);
        
    }

    public void paint(Graphics g){

        Graphics2D g2 = (Graphics2D) g;
        g2.drawLine(20, 50, 480, 50);
        
    }
    
}

And here is the code from MyFrame:

import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.*; 
import javax.swing.*;


public class MyFrame extends JFrame implements ActionListener{
    
    public JButton buttonCalc;
    public JButton buttonExit;

    public JRadioButton cthRButton;
    public JRadioButton andRButton;
    public JRadioButton bPButton;

    public JLabel label;
    public JLabel labelBP;
    public JLabel labelETT;
    public JLabel labelCA;

    public JTextField textField;

    MyPanel panel;

    ImageIcon icon = new ImageIcon("icons.png");

    public MyFrame(){

        panel = new MyPanel();

        this.setTitle("Vacuum Tube Calculator");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(panel);
        this.pack();
        this.setIconImage(icon.getImage());
        this.setResizable(false);
        this.setVisible(true);
        this.setLayout(null);

        label = new JLabel("Tube Calculator");
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.TOP);
        label.setBounds(165, 10, 180, 70);
        label.setFont(new Font("Arial", Font.BOLD, 23));
        label.setVisible(true);

        labelETT = new JLabel("Enter Tube Type:");
        labelETT.setBounds(140, 70, 150, 20);
        labelETT.setFont(new Font("Arial", Font.BOLD, 16));
        labelETT.setVisible(true);
        
        textField = new JTextField();
        

        this.add(label);
        this.add(labelETT);
        this.add(textField);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource()==buttonCalc){
            if(textField.getText().equalsIgnoreCase("6P14P"))
            System.out.println("");
        }
        if(e.getSource()==cthRButton){
            System.out.println("");
        }
        if(e.getSource()==andRButton){
            System.out.println("");
        }
        if(e.getSource()==bPButton){
            System.out.println("");
        }
        else{
            if(e.getSource()==buttonExit){
                setVisible(false);
                dispose();
                System.exit(getDefaultCloseOperation());
            }
        }
    }
}

I checked some "tempates" in Google and YouTube but, nothing worked

0

There are 0 answers