【问题标题】:JFrame, JPanel, paintComponent how toJFrame、JPanel、paintComponent如何
【发布时间】:2014-11-27 23:00:33
【问题描述】:
  1. 您好,我有以下课程,我想要显示内容 (paintComponentor 具有来自绘画类的此矩形的面板) 在我的 JFrame 里面。我已经尝试找出如何通过以下方式实现这一目标 看看这个论坛上发布的不同例子但是这个 对我没有帮助我需要简单的例子,比如框架内的面板 油漆组件或类似的东西,以了解应该如何工作! 附言。不要把我挂在树上,因为我是新手,问问题!!!

    [我想要这样的东西][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

【问题讨论】:

    标签: java swing jpanel paintcomponent


    【解决方案1】:

    先看看:

    这可能是我能做到的最简单的了……

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

    复合示例

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

    内部面板(与上一个例子没有变化),作为一个浅灰色的边框应用到它上面,所以你可以看到它,红色矩形仍然是由面板paintComponent方法绘制的。

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

    【讨论】:

    • 这个红色方块其实是paint组件打印出来的吧?嗯,如果这个红色矩形将是保存 anchart 或图像的面板呢??
    • 红色方块被绘制在面板的边界内,它位于框架内。在此示例中,红色方块始终比环绕面板小 100 像素。
    • 所以如果我添加另一个面板来保存文本,那么红色矩形将在面板内的相同位置与该面板在布局上的位置无关?
    • 基于上面的例子,红色矩形将在组件内居中,并且总是比组件本身小 100 像素,所以,不,红色矩形会根据组件的大小改变位置组件...
    • 好的,这说明了这一点。我将通过链接您给我的内容来检查这两个示例。谢谢疯狂程序员
    猜你喜欢
    • 2015-02-04
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 2015-11-30
    • 1970-01-01
    • 2012-03-19
    相关资源
    最近更新 更多