【问题标题】:why can't I draw any stuffs on Frame in java?为什么我不能在 java 中的 Frame 上绘制任何东西?
【发布时间】:2015-05-18 04:49:24
【问题描述】:

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

所以我想要的是,请教我如何在框架内绘制矩形或椭圆形。

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

【问题讨论】:

    标签: java swing jframe paint


    【解决方案1】:
    1. 您永远不会真正将C2F 添加到任何能够绘制它的东西上,因此您的paint 方法将永远不会被调用。
    2. 您应该覆盖paintComponent 而不是paint,因为您已经破坏了组件的油漆链,这可能会导致无数有趣的油漆故障问题。约定还建议您在执行任何自定义绘制之前也应调用super.paintComponent(覆盖paintComponent

    请参阅Painting in AWT and SwingPerforming Custom Painting 了解更多详情

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

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

    【讨论】:

    • 感谢您回答我的问题,但我无法理解。调用 super.paintComponent 是什么意思?可以举个例子吗?
    • 而且它仍然不起作用。即使当我用 super.paintComponent 覆盖时
    • 嗯..我想我没有。
    • 所以你的意思是我需要创建另一种方法来创建框架?而不是构造函数?
    • 我创建了另一种方法。那我该怎么办?我应该把 super.paintComponent 放在哪里?
    猜你喜欢
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2022-11-15
    相关资源
    最近更新 更多