0

我设置了线条、矩形和椭圆形,我有一个问题。

如何使我的线条、椭圆和矩形在 main 方法中可见?由于某种原因,当我编译和运行文件时它们没有被输出......谢谢

 import java.awt.BorderLayout;
 import java.awt.Color;

 import java.awt.Dimension;
 import java.awt.Graphics;


 import javax.swing.JPanel;

 public class LinesRectsOvalsJPanel extends JPanel {


public LinesRectsOvalsJPanel() {


setLayout(new BorderLayout());
setPreferredSize(new Dimension(10, 23));

JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setOpaque(true);

add(rightPanel, BorderLayout.EAST);
   }

  public void paintComponent( Graphics g ) {

   super.paintComponent( g ); 

     this.setBackground( Color.WHITE );
     //   x y width height
     g.setColor(Color.BLACK);
     g.drawLine(5,10,5,30);
     g.setColor(Color.BLUE);
     g.drawLine(18,70,127,24);
     g.setColor(Color.RED);
     g.drawLine(25,45,100,38);

     g.setColor(Color.YELLOW);
     g.drawOval(23,25,23,55);
     g.setColor(Color.BLACK);
     g.drawOval(15,14,40,78);
     g.setColor(Color.CYAN);
     g.drawOval(180,102,5,90);
     g.setColor(Color.RED);
     g.drawOval(21,20,89,11);
     g.setColor(Color.BLUE);
     g.drawOval(35,87,39,27);
     g.setColor(Color.YELLOW);
     g.fillRect(87,5,5,60);
     g.setColor(Color.GREEN);
     g.fillRect(105,15,15,85);
     g.setColor(Color.CYAN);
     g.fillRect(14,45,76,86);
     g.setColor(Color.RED);
     g.fillRect(70,79,65,86);
     g.setColor(Color.BLUE);
     g.fillRect(90,108,5,8); 
 }
}

代码 2

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.Graphics;

 import javax.swing.BoxLayout;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.SwingConstants;
 import javax.swing.border.BevelBorder;

  public class Main {
    public static void main(String[] args) {
  JFrame frame = new JFrame("");
 frame.setLayout(new BorderLayout());
frame.setSize(300, 300);

JPanel statusPanel = new JPanel();
statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
frame.add(statusPanel, BorderLayout.SOUTH);
statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 16));
statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
JLabel statusLabel = new JLabel("status");
statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
statusPanel.add(statusLabel);

frame.setVisible(true);
}
}
4

2 回答 2

2

您尚未添加LinesRectsOvalsJPanel组件:

frame.add(new LinesRectsOvalsJPanel());
于 2012-09-05T21:10:35.440 回答
0

尝试这样的事情:

这是主类(框架):

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Frame extends JFrame{

    Panel p = new Panel();

    public Frame(){
        p.setPreferredSize(new Dimension(640,480));
        add(p);
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                Frame f = new Frame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setVisible(true);
            }   
        });
    }
}

您的面板类:

import javax.swing.*;

public class Panel extends JPanel{
    public void paintComponent(Graphics g){
        g.setColor(Color.BLUE);
        g.fillRect(20, 20, 50, 50);
    }
}
于 2012-09-05T21:11:38.020 回答