感谢您的帮助。现在所有的物体都显示出来了。
但是我遇到了一个新问题。我尝试使用 For 循环绘制同一个盒子的 10 个副本,中间有一点空间,这样它们就不会只是堆叠在同一位置。
但由于某种原因,它们不断地被绘制在彼此的顶部和中心,而不是从 x = 20 开始...
import java.awt.*;
import javax.swing.*;
public class CarWashPanel extends JPanel {
public int i;
public int x = 20;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
for (i=0; i < 10; i++){
g.fillRoundRect(x, 10, 50, 100, 55, 25);
x = x + 10;
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 150);
}
}
//
我正在尝试在我的 CarWashPanel 类中添加图形,以将其添加到我的 GUI 中。我已经阅读了一些教程和其他问题,但我不知道我做错了什么。
我添加到 GUI 的按钮和标签显示得很好,但是当我向 CarWashPanel 添加某些内容时,它不会显示在 GUI 中。
我觉得我需要告诉我的 GUI 添加 CarWashPanel 中的所有元素,但我不确定如何操作。
public class Main {
public static void main(String[] args) {
GUI g = new GUI();
}
}
import javax.swing.*;
import java.awt.*;
public class GUI extends JFrame {
private JTextField t1 = new JTextField(2);
private JLabel l1 = new JLabel("enter position");
private JButton b1 = new JButton("new customer");
private JButton b2 = new JButton("wash car");
public GUI() {
setDefaultCloseOperation (
JFrame.EXIT_ON_CLOSE );
add(l1);
add(t1);
add(b1);
add(b2);
setTitle("Carwash");
setSize(500, 200);
setVisible(true);
setLayout(new FlowLayout());
add(new CarWashPanel());
}
}
public class Carwash {
private boolean[] positions = new boolean[10];
private int washing = 10;
public void addCar(int p) {
positions[p] = true;
}
public void removeCar(int p) {
positions[p] = false;
}
public boolean[] getPositions() {
return positions;
}
public int getWashing() {
return washing;
}
}
import java.awt.*;
import javax.swing.*;
public class CarWashPanel extends JPanel {
public CarWashPanel(){
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRoundRect(150, 50, 100, 100, 50, 25);
}
}
最佳答案
这是一个很常见的问题。您在添加组件之前调用 setVisible
。在 CarWashPanel
上添加组件,在 JFrame
上添加 CarWashPanel
,然后调用 setVisible
。另外,删除此行:setLayout(new FlowLayout());
- FlowLayout 是 JPanel
的默认布局(在您的情况下是 CarWashPanel),这就足够了。
您的代码应如下所示:
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(GUI::new);
}
public static class GUI extends JFrame {
private JTextField t1 = new JTextField(2);
private JLabel l1 = new JLabel("enter position");
private JButton b1 = new JButton("new customer");
private JButton b2 = new JButton("wash car");
CarWashPanel carWashPanel = new CarWashPanel();
public GUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
carWashPanel.add(l1);
carWashPanel.add(t1);
carWashPanel.add(b1);
carWashPanel.add(b2);
add(carWashPanel,BorderLayout.CENTER);
setTitle("Carwash");
pack();
setVisible(true);
}
}
public class Carwash {
private boolean[] positions = new boolean[10];
private int washing = 10;
public void addCar(int p) {
positions[p] = true;
}
public void removeCar(int p) {
positions[p] = false;
}
public boolean[] getPositions() {
return positions;
}
public int getWashing() {
return washing;
}
}
public static class CarWashPanel extends JPanel {
public CarWashPanel() {
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.fillRoundRect(150, 50, 100, 100, 50, 25);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(500, 200);
}
}
}
其他旁注:
不要为JFrame
调用setSize
,而是调用pack
。而是覆盖 JPanel
的 getPreferredSize
并返回一些尺寸。
避免使用 JFrame
扩展您的类,除非您想定义新方法或覆盖现有方法。
关于Java GUI 绘画组件(新手),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537885/