为什么我不能在Java中的Frame上绘制任何东西? [英] why can't I draw any stuffs on Frame in java?

查看:134
本文介绍了为什么我不能在Java中的Frame上绘制任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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中绘画执行自定义绘画了解更多详情

作为一般建议,我不鼓励你在另一个组件的构造函数中创建一个框架,这将使组件相当多再次无法使用(例如,如果你想在另一个容器上重复使用它)

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

这篇关于为什么我不能在Java中的Frame上绘制任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆