如何从Java中的另一个类访问paintComponent()?

时间:2013-07-23 00:08:09

标签: java swing jframe jpanel extends

所以,我的主要课程扩展了JFrame,我有第二个课程延伸JPanel以允许paintComponentJPanel上绘图。但是如何访问新类以绘制到JPanel

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class MTGSAMPServerReference extends JFrame implements ActionListener {
    public static Toolkit tk = Toolkit.getDefaultToolkit(); 
    static int ScrnWidth = ((int) tk.getScreenSize().getWidth());
    static int ScrnHeight = ((int) tk.getScreenSize().getHeight());
    private static final long serialVersionUID = 1L;
    private static JList list1;
    private static JButton next;
    private static JPanel bg = new JPanel();

    public MTGSAMPServerReference() {
        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout(FlowLayout.LEADING));
        Object[]mainData = {"Vehicles", "Bikes/Bicycles", "Boats", "Houses", "Businesses", "Objects", "Jobs", "Ranks", "Licenses", "VIP"};
        JPanel controls = new JPanel(new BorderLayout(5,5));
        list1 = new JList<Object>(mainData);
        list1.setVisibleRowCount(10);
        next = new JButton("Next");
        next.addActionListener(this);
        controls.add(new JScrollPane(list1));
        controls.add(next, BorderLayout.PAGE_END);
        controls.setBorder(new EmptyBorder(25,25,0,0));
        add(controls);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Next")) {
            int index = list1.getSelectedIndex();
            System.out.println("Index Selected: " + index);
            String s = (String) list1.getSelectedValue();
            System.out.println("Value Selected: " + s);
        }
    }

    public void createAndShowGUI() {
        //Create and set up the window.
        JFrame f = new MTGSAMPServerReference();
        //Display the window.
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        f.setSize(1200, 800);
        f.setLocationRelativeTo(null);
        list1.setSize(250, 250);
        list1.setLocation(0, 0);
        next.setSize(75, 25);
        next.setLocation(251, 276);
        MTGSAMPServerReference.this.repaint();
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            MTGSAMPServerReference gui = new MTGSAMPServerReference();
            gui.createAndShowGUI();
            }
        });
    }
}

class drawOnPanel extends JPanel {
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawRect(0, 0, 50, 50);
    }
}

有什么想法吗?

提前致谢!

2 个答案:

答案 0 :(得分:3)

您只需像添加任何其他组件一样添加面板。在需要重新绘制时调用paintComponent方法。

答案 1 :(得分:3)

您没有将drawOnPanel类添加到框架的容器中。添加以下行:

add(new drawOnPanel());

附加说明

  1. createAndShowGUI()内,您的类已从JFrame扩展,无需创建另一个JFrame对象,请将其更改为:

       public void createAndShowGUI() {
        //Create and set up the window.
        //JFrame f = new MTGSAMPServerReference();
        //Display the window.
    
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1200, 800);
        setLocationRelativeTo(null);
        list1.setSize(250, 250);
        list1.setLocation(0, 0);
        next.setSize(75, 25);
        next.setLocation(251, 276);
        repaint();
        pack();
    }
    
  2. 重新绘制之后选择pack()方法以适应组件的大小,如上所述。

  3. @ knorberg的评论:在哪里以及如何绘制我的矩形?

    您的绘图很好,只需覆盖getPreferredSize()类中的drawOnPanel方法:

    class drawOnPanel extends JPanel {
      @Override
      protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.drawRect(0, 0, 50, 50);
        }
      @Override
      public Dimension getPreferredSize(){
        return new Dimension(200,200);
      }
    }