java - JFrame、JPanel、paintComponent 如何

标签 java swing jpanel paintcomponent

  1. 您好,我有以下类(class),我想要显示内容 (paintComponentor 具有来自paint 类的矩形的面板) 在我的 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]:/image/OJTrq.png

最佳答案

首先看一下:

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

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

    }

}

复合示例

此示例使用 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();
        }

    }

}

关于java - JFrame、JPanel、paintComponent 如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27179739/

相关文章:

java - 如何使用字符串 JTree 创建节点?

java - JPanel 对 MouseEvents 没有反应?

java - 如何强制(至 1.2.0)OSGi XMLNS 用于 OSGi R7 元类型注释

java - 如何获取文本并响应 JFormattedTextField 上的输入按下

java - 在 QSH 命令中导入属性文件 - iseries

java - 设置多个彼此独立的 JFrame

java - 我应该使用哪种基于 Java 的工作流引擎?

java - 如何使用单个滚动条滚动两个或多个 JTable?

java - 如何创建边框颜色样式面板?

java - 从 JFrame 关闭 JPanel 窗口 [Java]