[英]Java Drawing to a JPanel (debugging)
I'm trying to draw a basic object to a JPanel although it doesn't seem to be working. 我正在尝试向JPanel绘制基本对象,尽管它似乎不起作用。
I'm certain I am doing something wrong with where the paint method is being called 我敢肯定我在调用paint方法的地方做错了
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class testGui {
static colors gc_colors;
static gui gc_gui;
public static void main(String[] args) {
gc_colors = new colors();
gc_gui = new gui();
gc_gui.cv_frame.setVisible(true);
}
public static class colors {
Color cv_ltGrey;
Color cv_mdGrey;
Color cv_dkGrey;
public colors() {
cv_ltGrey = Color.decode("#DDDDDD");
cv_mdGrey = Color.decode("#CCCCCC");
cv_dkGrey = Color.decode("#111111");
}
}
public static class gui {
JFrame cv_frame;
JPanel cv_panel;
JPanel cv_content;
public gui() {
cv_frame = new JFrame();
cv_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cv_frame.setTitle("Test GUI");
cv_frame.setSize(600, 400);
cv_frame.setLayout(new FlowLayout());
cv_panel = new JPanel();
cv_panel.setBackground(gc_colors.cv_ltGrey);
cv_panel.setPreferredSize(new Dimension(500, 300));
cv_frame.add(cv_panel);
cv_content = new content();
cv_panel.add(cv_content);
}
}
public static class content extends JPanel {
public void paint(Graphics graphic) {
super.paint(graphic);
draw(graphic);
}
public void update() {
repaint();
}
public void draw(Graphics graphic) {
Graphics2D graphic2D = (Graphics2D) graphic;
graphic2D.setPaint(gc_colors.cv_ltGrey);
graphic2D.fillRect(10, 10, 100, 100);
}
}
}
I have a class for my gui which I am adding a JPanel to (a light grey one). 我有一个针对gui的类,我要为其添加一个JPanel(浅灰色的)。 Which I am then trying to add my drawing to using a JPanel extended class called content.
然后,我尝试使用一个名为content的JPanel扩展类将其添加到图形中。
When I run it though it seems to create the grey JPanel which I want but the drawing is just a tiny white square and I'm not sure why. 当我运行它时,虽然它似乎创建了我想要的灰色JPanel,但是图形只是一个很小的白色正方形,我不确定为什么。
So, you content
panel has a default preferred size of 0x0
, FlowLayout
honours the preferredSize
of its components (with a little margin), hence the reason why you have a nice little small white rectangle. 因此,
content
面板的默认首选大小为0x0
, FlowLayout
接受其组件的preferredSize
(略有空白),因此这是您拥有一个漂亮的小白色矩形的原因。
What you need to do is override the getPreferredSize
method of the content
panel and return a suitable size, for example 您需要做的是重写
content
面板的getPreferredSize
方法并返回合适的大小,例如
public static class content extends JPanel {
@Override
public Dimension getPreferredSize() {
return new Dimension(120, 120);
}
public void paint(Graphics graphic) {
super.paint(graphic);
draw(graphic);
}
public void update() {
repaint();
}
public void draw(Graphics graphic) {
Graphics2D graphic2D = (Graphics2D) graphic;
graphic2D.setPaint(gc_colors.cv_ltGrey);
graphic2D.fillRect(10, 10, 100, 100);
}
}
I've decided to just leave out the second JPanel altogether. 我决定完全不使用第二个JPanel。 It was too much of a hassle to put the JPanel inside of another JPanel so instead I am only going to use a single JPanel
将JPanel放入另一个JPanel太麻烦了,所以我只打算使用单个JPanel
public static class gui {
JFrame cv_frame;
JPanel cv_panel;
JPanel cv_content;
public gui() {
cv_frame = new JFrame();
cv_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cv_frame.setTitle("Test GUI");
cv_frame.setSize(600, 400);
cv_frame.setLayout(new FlowLayout());
cv_content = new content();
cv_content.setBackground(gc_colors.cv_ltGrey);
cv_content.setPreferredSize(new Dimension(500, 300));
cv_frame.add(cv_content);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.