如何使按钮不影响绘制形状的位置?

huangapple 未分类评论43阅读模式
英文:

How do you make it so that a button doesn't interfere with the location of drawn shape?

问题

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

public class Grid {

    public class homeGraphics extends JComponent {
        homeGraphics() {
            setPreferredSize(new Dimension(450, 600));
        }

        public void paint(Graphics g) {
            super.paint(g);
            g.fillRect(200, 275, 50, 50);
        }
    }

    public void homeFrame() {
        JFrame frame1 = new JFrame();
        frame1.setSize(450, 600);
        frame1.setResizable(false);
        frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);

        JButton playButton = new JButton("Play");
        playButton.setPreferredSize(new Dimension(60, 30));

        JPanel panel1 = new JPanel();
        panel1.add(playButton);
        panel1.add(new homeGraphics());

        frame1.add(panel1);
        frame1.setVisible(true);
    }

    public static void main(String args[]) {
        Grid frame = new Grid();
        frame.homeFrame();
    }
}
英文:

So basically when I add a button it essentially pushes the black rectangle drawn in this program down, putting it out of its given location. How would you fix this?

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

public class Grid {

    public class homeGraphics extends JComponent {
        homeGraphics() {
            setPreferredSize(new Dimension(450, 600));
        }

        public void paint(Graphics g) {
            super.paint(g);
            g.fillRect(200, 275, 50, 50);
        }
    }

    public void homeFrame() {
        JFrame frame1 = new JFrame();
        frame1.setSize(450, 600);
        frame1.setResizable(false);
        frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);

        JButton playButton = new JButton("Play");
        playButton.setPreferredSize(new Dimension(60, 30));

        JPanel panel1 = new JPanel();
        panel1.add(playButton);
        panel1.add(new homeGraphics());
        
        frame1.add(panel1);
        frame1.setVisible(true);
    }

    public static void main(String args[]) {
        Grid frame = new Grid();
        frame.homeFrame();
    }
}```

</details>


# 答案1
**得分**: 0

>它本质上是将程序中绘制的黑色矩形向下推,使其超出其给定位置。

什么意思超出了它的位置?绘画始终是相对于组件进行的。因此,您的绘画将始终在组件的 (200, 275) 处进行。

如果您试图相对于“frame”在 (200, 275) 处绘制,那是错误的。这不是绘画的工作方式。

您的代码中还存在其他问题:

1. 不要尝试设置框架的大小。如果自定义面板是 (450, 600),那么框架怎么可能是相同的大小?框架还包含“标题栏”和“边框”。不要使用 `setSize()`,而是在 `frame1.setVisible(….)` 之前调用 `frame.pack()`。

2. 类名以大写字母开头。通过示例学习。您是否见过 JDK 中以小写字母开头的类名?

3. 自定义绘画是通过覆盖 `paintComponent(…)` 进行的,而不是 `paint()`。

4. 默认情况下,JPanel 使用 FlowLayout。所以您看到的是按钮在一行上,然后 “HomeGraphics” 类太大无法放在同一行上,所以它换到第二行。

在进行框架布局时,您应该更加明确。因此,您的代码应该类似于:

    JPanel wrapper = new JPanel();
    wrapper.add( playButton );
    
    //JPanel panel1 = new JPanel();
    //panel1.add(playButton);
    //panel1.add(new homeGraphics());
    JPanel panel1 = new JPanel( new BorderLayout() );
    panel1.add(wrapper, BorderLayout.PAGE_START);
    panel1.add(new HomeGraphics(), BorderLayout.CENTER);

现在,代码更清楚地显示了您的布局尝试。

<details>
<summary>英文:</summary>

&gt; it essentially pushes the black rectangle drawn in this program down, putting it out of its given location.

What do you mean out of its location? Painting is always done relative to the component. So your painting will always be done at (200, 275) of the component. 

If you are attempting to paint at (200, 275) relative to the &quot;frame&quot;, then don&#39;t. That is NOT how painting works. 

Other problems with your code:

1. Don&#39;t attempt to set the size of your frame. If the custom panel is (450, 600) how can the frame possibly be the same size? The frame also contains the &quot;title bar&quot; and &quot;borders&quot;. Instead of using setSize(), you invoke `frame`.pack()` just before `frame1.setVisible(….)`.

2. Class names start with an upper case character. Learn by example. Have you ever seen a class name in the JDK that doesn&#39;t start with an upper case character?

3. Custom painting is done by overriding `paintComponent(…)`, not paint().

4. By default a JPanel uses a FlowLayout. So what you see it the button on one line and then the &quot;HomeGraphics&quot; class is too big to fit on the same line so it wraps the to the second line. 

You should be more explicit when you do frame layout. So your code should be something like:

    JPanel wrapper = new JPanel();
    wrapper.add( playButton );
    
    //JPanel panel1 = new JPanel();
    //panel1.add(playButton);
    //panel1.add(new homeGraphics());
    JPanel panel1 = new JPanel( new BorderLayout() );
    panel1.add(wrapper, BorderLayout.PAGE_START);
    panel1.add(new HomeGraphics(), BorderLayout.CENTER);

Now the code shows your layout attempt more clearly.


</details>



huangapple
  • 本文由 发表于 2020年4月4日 22:34:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/61029660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定