简体   繁体   English

Java GUI PaintComponent(新手)

[英]Java GUI paintcomponent (newbie)

Thanks for the help. 谢谢您的帮助。 The objects are all showing now. 这些对象现在都已显示。

But I ran into a new problem. 但是我遇到了一个新问题。 Im trying to use a For loop to draw 10 copys of the same box with a little space in between so they don't just stack in the same position. 我试图使用一个For循环绘制同一个盒子的10个副本,并且在它们之间留有一点间距,因此它们不只是堆叠在同一位置。

But for some reason they keep getting painted on top of eachother and in the center instead of starting at x = 20... 但是由于某些原因,它们一直在彼此顶部和中心位置上绘画,而不是从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);
    }

}

// //

I'm trying to add graphics in my CarWashPanel class to I want to add to my GUI. 我试图在我的CarWashPanel类中添加要添加到GUI的图形。 I've read some tutorials and other questions but I can't figure out what i'm doing wrong. 我已经阅读了一些教程和其他问题,但无法弄清楚自己在做什么。

The buttons and label that i've added to the GUI show up just fine but when I add something to my CarWashPanel it doesn't show up in the GUI. 我添加到GUI的按钮和标签显示得很好,但是当我向CarWashPanel添加某些内容时,它并没有显示在GUI中。

I feel like I need to tell my GUI to add all elements from the CarWashPanel but I'm not sure how. 我感觉我需要告诉我的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);
           }

            }

If you don't need to add things dynamically the best thing to do is call setVisible(true) after you've added all your components. 如果不需要动态添加内容,则最好的做法是在添加所有组件调用setVisible(true)

However, if you want to add things after the frame is visible you can do so and then call the frame's revalidate() method to cause it to redraw. 但是,如果要在框架可见后添加内容,可以这样做,然后调用框架的revalidate()方法以使其重绘。

Secondly I'd recommend you set the layout before you add any components. 其次,我建议您添加任何组件之前先设置布局。

It's very often issue. 这是经常发生的问题。 You are calling setVisible before you are adding your components. 在添加组件之前,您正在调用setVisible Add your components on CarWashPanel , add CarWashPanel on JFrame and then call setVisible . CarWashPanel上添加组件,在JFrame上添加CarWashPanel ,然后调用setVisible Also, remove this line: setLayout(new FlowLayout()); 另外,删除此行: setLayout(new FlowLayout()); - FlowLayout is default layout for JPanel (CarWashPanel in your case) and this makes it sufficient. -FlowLayout是JPanel默认布局(在您的情况下为CarWashPanel),这就足够了。

Your code should look something like this: 您的代码应如下所示:

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

Other sidenotes: 其他旁注:

Don't call setSize for JFrame , call pack . 不要为JFrame调用setSize ,而要调用pack Rather override getPreferredSize for JPanel and return some dimensions. 而是为JPanel覆盖getPreferredSize并返回一些尺寸。

Avoid extending your class with JFrame unless you want to define new methods or override existing ones. 除非要定义新方法或覆盖现有方法,否则请避免使用JFrame扩展类。

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

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