gpt4 book ai didi

java - Gui Swing 绘图对象未显示

转载 作者:行者123 更新时间:2023-11-30 06:13:13 24 4
gpt4 key购买 nike

我需要帮助在某个面板上绘制/绘制对象。我想在“drawPanel”JPanel 中绘制东西。有人可以看一下这个并告诉我我错过了什么吗?我将不胜感激,如果我遗漏了一些明显的东西,我很抱歉。

当我运行该程序时,没有绘制任何内容。

package redball;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

public class drawTest extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
drawTest frame = new drawTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public drawTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);


JPanel leftPanel = new JPanel();
contentPane.add(leftPanel, BorderLayout.WEST);

JLabel lblNewLabel = new JLabel("some stuff");
leftPanel.add(lblNewLabel);

JPanel drawPanel = new JPanel(); //this is where I want to draw stuff
contentPane.add(drawPanel, BorderLayout.CENTER);


}

protected void paintComponent(Graphics g) {
super.paintComponents(g);

g.setColor(Color.black);
g.fillRect(25, 500, 100, 20);
}

}

最佳答案

一般提示(前两个有助于解决此问题):

  1. 在任何更改的方法上使用@Override 表示法! (该代码试图重写不存在的方法。即使它是在面板中实现的,它也会调用错误的 super 方法。)
  2. 自定义绘制的组件应建议首选尺寸。
  3. 请学习常见的 Java 命名法(命名约定 - 例如 EachWordUpperCaseClassfirstWordLowerCaseMethod()firstWordLowerCaseAttribute,除非它是 UPPER_CASE_CONSTANT)并一致地使用它。

这是实现上述建议的结果,并在代码中的注释中提到了更多提示。

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class DrawTest extends JFrame {

private JPanel contentPane;

public DrawTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// instead pack() once components are added
// setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.CYAN);
contentPane.add(leftPanel, BorderLayout.LINE_START);

JLabel lblNewLabel = new JLabel("some stuff");
leftPanel.add(lblNewLabel);

JPanel drawPanel = new JPanel() {

Dimension preferredSize = new Dimension(300, 100);

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

g.setColor(Color.black);
// 500? That's much deeper than the panel shows on-screen!
//g.fillRect(25, 500, 100, 20);
g.fillRect(25, 50, 100, 20);
}

@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
if (d.width<50 || d.height<50) {
return preferredSize;
} else {
System.out.println("d: " + d);
return d;
}
}
};
drawPanel.setBackground(Color.YELLOW);
contentPane.add(drawPanel, BorderLayout.CENTER);

pack();
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DrawTest frame = new DrawTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

关于java - Gui Swing 绘图对象未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49808281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com