why can't I draw any stuffs on Frame in java?

2019-03-06 23:28发布

问题:

Coding is here. I can't create any rectangle or circle inside frame. the object of this project is to create converting celcius 2 Farenheit & Farenheit 2 Celcius.

so what I want is, please teach me to how to draw rectangle or oval in side the frame.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class C2F  extends JComponent{

private double input1, output1;
private double input2, output2;
JPanel center = new JPanel();
JPanel top = new JPanel();
JPanel east = new JPanel();
JPanel south = new JPanel();
//for giving input & output

C2F(){

JFrame frame = new JFrame();
frame.setTitle("C2F");
frame.setSize(700,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());

frame.getContentPane().add(top,BorderLayout.NORTH);
frame.getContentPane().add(center,BorderLayout.CENTER);
frame.getContentPane().add(south,BorderLayout.SOUTH);
frame.getContentPane().add(east,BorderLayout.EAST);
frame.setVisible(true);
CC2F();

}

public void CC2F(){
//making frame

//give specific location
JLabel L1 = new JLabel("Please input Celcius or Fahrenheit to Convert");
top.add(L1);

JLabel l1 = new JLabel("Cel -> Fah");
south.add(l1);

JTextField T1 = new JTextField(12);
south.add(T1);

JButton B1 = new JButton("Convert");
south.add(B1);

JLabel l2 = new JLabel("Fah -> Cel");
south.add(l2);

JTextField T2 = new JTextField(12);
south.add(T2);

JButton B2 = new JButton("Convert");
south.add(B2);
//to create buttons and labels to give an answer
B1.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){

    input1 = Double.parseDouble(T1.getText());
    output1 = input1 *(9/5) + 32;
    T2.setText(""+output1);
    repaint();
}
});

B2.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){

input2 = Double.parseDouble(T2.getText());
output2 = (input2 - 32)/9*5;
T1.setText(""+output2);
}
});
//making events

//placing the buttons and labels
output1 = 0;
output2 = 0;
//initialize the value

}
public void paintComponent(Graphics g) {
//error spots. it compiles well. But this is not what I want. 
 super.paintComponent(g);   
Graphics2D gg = (Graphics2D) g;

gg.setColor(Color.BLACK);
gg.drawOval(350, 500,12,12);

gg.setColor(Color.RED);
gg.fillRect(350, 500, 10,(int) output1);
gg.fillOval(350, 500, 10, 10);

gg.setColor(Color.RED);
gg.fillRect(350, 500, 10,(int) output2);
gg.fillOval(350, 500, 10, 10);

//to draw stuffs
}

public static void main(String[] args)
{//to run the program
 new C2F();
 }
 }

回答1:

  1. You never actually add C2F to anything that would be able to paint it, therefore your paint method will never be called.
  2. You should override paintComponent instead of paint, as you've broken the paint chain for the component which could cause no end of issues with wonderful and interesting paint glitches. Convention would also suggest that you should call super.paintComponent (when overriding paintComponent) as well before you perform any custom painting

See Painting in AWT and Swing and Performing Custom Painting for more details

As a general piece of advice, I'd discourage you from creating a frame within the constructor of another component, this will make the component pretty much unusable again (if you wanted to re-use it on another container for example)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class C2F extends JComponent {

    public C2F() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                TestPane center = new TestPane();
                JPanel top = new JPanel();
                JPanel east = new JPanel();
                JPanel south = new JPanel();
                //give specific location
                JLabel L1 = new JLabel("Please input Celcius or Fahrenheit to Convert");
                top.add(L1);

                JLabel l1 = new JLabel("Cel -> Fah");
                south.add(l1);

                JTextField T1 = new JTextField(12);
                south.add(T1);

                JButton B1 = new JButton("Convert");
                south.add(B1);

                JLabel l2 = new JLabel("Fah -> Cel");
                south.add(l2);

                JTextField T2 = new JTextField(12);
                south.add(T2);

                JButton B2 = new JButton("Convert");
                south.add(B2);
                //to create buttons and labels to give an answer
                B1.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {

                        double input1 = Double.parseDouble(T1.getText());
                        double output1 = input1 * (9 / 5) + 32;
                        T2.setText("" + output1);
                        center.setOutput1(output1);
                    }
                });

                B2.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {

                        double input2 = Double.parseDouble(T2.getText());
                        double output2 = (input2 - 32) / 9 * 5;
                        T1.setText("" + output2);
                        center.setOutput2(output2);
                    }
                });
                //making events
                frame.getContentPane().add(top, BorderLayout.NORTH);
                frame.getContentPane().add(center, BorderLayout.CENTER);
                frame.getContentPane().add(south, BorderLayout.SOUTH);
                frame.getContentPane().add(east, BorderLayout.EAST);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private double output1, output2;

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 600);
        }

        public void setOutput1(double output1) {
            this.output1 = output1;
            repaint();
        }

        public void setOutput2(double output2) {
            this.output2 = output2;
            repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;

            g2d.setColor(Color.BLACK);
            g2d.drawOval(350, 500, 12, 12);

            g2d.setColor(Color.RED);
            g2d.fillRect(350, 0, 10, (int) output1);
            g2d.fillOval(350, 0, 10, 10);

            g2d.setColor(Color.BLUE);
            g2d.fillRect(350, 0, 10, (int) output2);
            g2d.fillOval(350, 0, 10, 10);
            g2d.dispose();
        }

    }

    public static void main(String[] args) {//to run the program
        new C2F();
    }
}