JFrame,JPanel,paintComponent如何 [英] JFrame, JPanel, paintComponent how to

查看:105
本文介绍了JFrame,JPanel,paintComponent如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我有以下课程,我想要显示内容 (paintComponent或带有paint类中此矩形的面板) 在我的JFrame中.我已经尝试找出如何通过 看这个论坛上发布的不同示例 对我没有帮助,我需要简单的示例,例如面板内框 油漆组件或类似的东西,以了解应该如何工作! ps.不要将我吊死在树上,因为我是新手,请问问题!!!

  1. Hi I have following classes, I want display content (paintComponentor that panel with this rectangle from paint class) inside my JFrame. I try already find out how to achieve this by looking at different examples posted on this forum however this doesn't help me I need simple example like panel inside frame with paint component or something similar to understand how should work! ps. don't hang me on tree because I am newbie jut ask question!!!

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

[I want something like this][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

推荐答案

首先看一下:

  • Painting in AWT and Swing
  • Performing Custom Painting
  • 2D Graphics

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

This is probably the simplest I can make it...

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面板边缘的内容.

This example uses an outer panel, which has an empty border applied to it, this pushes the content of the edges of the outer panel.

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

The inner panel (which is unchanged from the last example), as a light gray border applied to it so you can see it, the red rectangle is still been painted by the panels paintComponent method.

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

    }

}

这篇关于JFrame,JPanel,paintComponent如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆