为什么可以';我不能用java在框架上画东西吗?

为什么可以';我不能用java在框架上画东西吗?,java,swing,jframe,paint,Java,Swing,Jframe,Paint,编码在这里。 我无法在框架内创建任何矩形或圆形。 本项目的目标是创建转换celcius 2 Farenheit和Farenheit 2 celcius 所以我想要的是,请教我如何在框架的侧面画矩形或椭圆形 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Frame; import java.awt.Graphics; import java.awt.Gr

编码在这里。 我无法在框架内创建任何矩形或圆形。 本项目的目标是创建转换celcius 2 Farenheit和Farenheit 2 celcius

所以我想要的是,请教我如何在框架的侧面画矩形或椭圆形

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();
 }
 }
  • 实际上,您从未向任何能够绘制它的对象添加
    C2F
    ,因此您的
    paint
    方法将永远不会被调用
  • 您应该覆盖
    paintComponent
    ,而不是
    paint
    ,因为您已经破坏了组件的绘制链,这可能会导致无休止的问题,出现奇妙而有趣的绘制故障。约定还建议您在执行任何自定义绘制之前调用
    super.paintComponent
    (当重写
    paintComponent
    时)
  • 有关详细信息,请参阅和

    作为一条一般性建议,我不鼓励您在另一个组件的构造函数中创建框架,这将使该组件再次几乎不可用(例如,如果您想在另一个容器上重复使用它)


    谢谢你回答我的问题,但我不明白。调用super.paintComponent是什么意思?你能给我举个例子吗?而且还是不行。即使我用超级油漆组件。。我想我没有。那么你的意思是我需要创建另一个方法来创建框架吗?我用另一种方法来代替构造器。那我该怎么办?我应该把super.paintComponent放在哪里?
    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();
        }
    }