简体   繁体   English

如何将按钮和画布放入jframe? (Java摇摆)

[英]How to put buttons and canvas into jframe? (Java swing)

I have a JFrame and a few buttons and text fields inside. 我有一个JFrame以及一些按钮和文本字段。 I'm using absolute positioning (null layout). 我正在使用绝对定位(空布局)。 I tried creating a canvas to draw on, but when I try to add the canvas to a container and then into the frame, the canvas overwrites all buttons. 我尝试创建一个要绘制的画布,但是当我尝试将画布添加到容器中然后再添加到框架中时,画布将覆盖所有按钮。 I want to have the canvas next to the buttons, both visible. 我希望按钮旁边的画布都可见。

This is my code: 这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Gui extends JFrame {
    private JLabel labEnterWord;
    private JTextField tfWord;
    private JButton butOk;

    public Gui(String title) {
        super(title);
        JPanel p = new JPanel();
        p.setLayout(null);

        //Create components and add them to the container
        addComponents(p);

        //Add containers to window
        getContentPane().add(p);
        getContentPane().add(new MyCanvas());

        setWindowProperties();
    }

    private void addComponents(JPanel p) {
        labEnterWord = new JLabel("Enter your word:");
        labEnterWord.setBounds(100, 60, 200, 30);                   
        labEnterWord.setFont(new Font("Arial", Font.PLAIN, 20));
        p.add(labEnterWord);

        tfWord = new JTextField();
        tfWord.setBounds(100, 100, 100, 25);
        tfWord.setFont(new Font("Arial", Font.PLAIN, 14));
        p.add(tfWord);

        butOk = new JButton("OK");
        butOk.setBounds(220, 100, 51, 25);
        butOk.addActionListener(new Event());
        p.add(butOk);
    }

    private void setWindowProperties(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setSize(1280, 720);
        setResizable(false);
        setLocationRelativeTo(null);
    }
}

MyCanvas class: MyCanvas类:

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

public class MyCanvas extends JPanel {

    public void drawing(){
        repaint();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawRect(500, 200, 200, 200);
    }
}

Main class just calls Gui(). 主类仅调用Gui()。

Here: 这里:

getContentPane().add(p);
getContentPane().add(new MyCanvas());

The first one adds your panel with your buttons to the frame. 第一个将带有按钮的面板添加到框架。 Then you add another thing to the frame! 然后在框架中添加其他内容!

Following your approach of placing everything manually, you rather add that canvas to your panel ! 按照您手动放置所有内容的方法,您宁可将该画布添加到面板中

You disabled all means that would allow the JFrame resp. 您禁用了所有允许JFrame响应的方法。 a layout manager to meaningfully place your panel and the canvas. 布局管理器,以有意义地放置面板和画布。 Instead you push two elements into the frame, without telling the frame where that canvas should go to! 取而代之的是,您将两个元素推入框架,而无需告诉框架画布应该去的位置!

So you either need absolute placement for the canvas as well - or you step back and turn to a layout manager to do that for you. 因此,您也需要画布的绝对放置-或退后一步,然后转到布局管理器为您完成此操作。

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

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