首页 文章

JFrame,JPanel,paintComponent如何

提问于
浏览
0
  • 您好我有以下类,我想在我的JFrame中显示内容(paintComponentor,该面板与paint类中的这个矩形) . 我试着通过查看本论坛上发布的不同示例来了解如何实现这一点但是这对我没有帮助我需要一个简单的例子,如面板内部的框架与油漆组件或类似的东西,以了解应该如何工作! PS . 不要把我挂在树上因为我是新手jut问问题!!!

[我想要这样的东西] [1]

package scp;

import java.awt.*;

import javax.swing.*;

public class Panel extends JPanel {
    public Panel() {
        //this.setPreferredSize(new Dimension(200,200));
        //panel = new Panel();
        setVisible(true);
        setLayout(new FlowLayout());
        setSize(200, 300);
        getRootPane();
    }
    @Override
    public void paintComponent(Graphics g){
        g.drawString("HEY",20,20);
        g.drawRect(200, 200, 200, 200); 
    }

}

and

package scp;

import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.EventHandler;

public class Frame extends JFrame {

    JButton redButton;
    JButton blueButton;

    public Frame()
    {
        super("EventHandling");
        setLayout(new FlowLayout());
        redButton = new JButton("Red");
        add(redButton);
        blueButton = new JButton("Blue");
        add(blueButton);

        EventHandler h = new EventHandler();

        redButton.addActionListener(h);
        blueButton.addActionListener(h);

    }

    private class EventHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource()==redButton)
                getContentPane().setBackground(Color.RED);
            else if(e.getSource()==blueButton)
                getContentPane().setBackground(Color.BLUE);
        }
    }

}

and

package scp;

import javax.swing.JFrame;

public class EventHandling {

    public static void main(String[] args) {

        Frame f = new Frame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(800,600);
        f.setVisible(true);
        f.getContentPane().add(new Panel());
    }

}

[1]:http://i.stack.imgur.com/OJTrq.png

1 回答

  • 4

    首先来看看:

    这可能是我能做到的最简单的......

    enter image description here

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            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");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                int width = getWidth() - 100;
                int height = getHeight() - 100;
                int x = (getWidth() - width) / 2;
                int y = (getHeight() - height) / 2;
                g2d.setColor(Color.RED);
                g2d.drawRect(x, y, width, height);
                g2d.dispose();
            }
    
        }
    
    }
    

    Compound Example

    此示例使用 outer 面板,该面板上应用了空边框,这会推动 outer 面板边缘的内容 .

    内部面板(与上一个示例相同),作为应用于它的浅灰色边框,以便您可以看到它,红色矩形仍然由面板 paintComponent 方法绘制 .

    Compound

    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 javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.LineBorder;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JPanel outer = new JPanel(new BorderLayout()) {
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(400, 400);
                        }
                    };
    
                    outer.setBorder(new EmptyBorder(50, 50, 50, 50));
    
                    TestPane tp = new TestPane();
                    tp.setBorder(new LineBorder(Color.LIGHT_GRAY));
    
                    outter.add(tp);
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(outer);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                int width = getWidth() - 100;
                int height = getHeight() - 100;
                int x = (getWidth() - width) / 2;
                int y = (getHeight() - height) / 2;
                g2d.setColor(Color.RED);
                g2d.drawRect(x, y, width, height);
                g2d.dispose();
            }
    
        }
    
    }
    

相关问题