简体   繁体   English

JFrame,JPanel,paintComponent如何

[英]JFrame, JPanel, paintComponent how to

  1. Hi I have following classes, I want display content (paintComponentor that panel with this rectangle from paint class) inside my JFrame. 嗨,我有以下类,我想在JFrame中显示内容(paintComponent或带有paint类中带有此矩形的面板)。 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. PS。 don't hang me on tree because I am newbie jut ask question!!! 不要将我吊死在树上,因为我是新手,请问问题!!!

    [I want something like this][1] [我想要这样的东西] [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]: http//i.stack.imgur.com/OJTrq.png

Start by taking a look at: 首先看一下:

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

    }

}

Compound Example 复合示例

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. 本示例使用一个outer面板,该面板上应用了一个空边框,这将推动outer面板边缘的内容。

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. 内部面板(与上一个示例保持不变),因为对其应用了浅灰色边框,因此您可以看到它,但是红色矩形仍由面板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();
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM