为什么我不能在 java 中的 Frame 上绘制任何东西?

2022-01-24 00:00:00 java swing paint jframe

编码在这里.我无法在框架内创建任何矩形或圆形.该项目的目标是创建转换 celcius 2 Farenheit &华氏 2 摄氏度.

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. 你从来没有真正将 C2F 添加到任何能够绘制它的东西上,因此你的 paint 方法永远不会被调用.
  2. 您应该覆盖 paintComponent 而不是 paint,因为您已经破坏了组件的绘制链,这可能会导致无穷无尽的问题以及精彩有趣的绘制故障.约定还建议您在执行任何自定义绘画之前也应调用 super.paintComponent(覆盖 paintComponent 时)
  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

请参阅 AWT 和 Swing 中的绘画 和 执行自定义绘画了解更多详情

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();
    }
}

相关文章