简体   繁体   English

我的JButton应该执行,我应该在我的窗格上看到一个矩形。 我如何让它可见?

[英]My JButton should execute and a rectangle should me seen on my Pane. How do i make it visible?

I have to code an interface with 4 Buttons. 我必须使用4个按钮编写一个接口。 The buttons should draw a rectangle. 按钮应绘制一个矩形。 I have set the layout and wanted to implement the action the button "Schwarz" (black in English) does after clicking it but it wont do anything... Are they alternatives of letting a button draw a rectangle or square? 我已经设置了布局并希望实现“Schwarz”按钮(英文为黑色)在点击后执行动作但它不会做任何事情......它们是否可以让按钮绘制矩形或正方形?

I also tried to add a jpanel to my already implementet panel but to set the visiblity on false. 我还尝试将jpanel添加到我已经实现的面板中,但是将visiblity设置为false。 After pressing the butten the boolean state should change but that didint work either 按下按钮后,布尔状态应该改变,但是这也不起作用

... ...

public class MyPanel extends javax.swing.JPanel {


    boolean E = false;

    public void paintComponement (Graphics rechtecke) {
        if(E == true) {
        super.paintComponents(rechtecke);
        rechtecke.setColor(Color.black);
        rechtecke.fillRect(200, 400, 400, 400);
        }

    }
    public MyPanel(){
        setBackground(Color.green);

... ...

... ...

public class MyFrame extends javax.swing.JFrame implements ActionListener  {    


public MyFrame()
 {
        int a = 1200;
        int b = 800;
        setSize(a, b);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        foad = new MyPanel();


        JButton Entfernen = new JButton("Entfernen");
        JButton Bild = new JButton("Bild");
        add(foad, BorderLayout.CENTER);
        //foad.setBackground(Color.GREEN);;
        JButton Rot = new JButton("rot");
        JButton Schwarz = new JButton("Schwarz");



        Entfernen.addActionListener(this);
        Bild.addActionListener(this);
        Rot.addActionListener(this);
        Schwarz.addActionListener(this);
        setVisible(true);
        }



        public void actionPerformed(ActionEvent e) { 
             if (e.getSource()==this.Schwarz) {
                 foad.E = true;

                repaint();}
        }

... ...

I do get the layout with the buttons and everything. 我确实得到了按钮和一切的布局。 It is just the functunalitiy of the button 这只是按钮的功能

I found it. 我找到了。 the super.paintcomponent has to be out side of my if satement. super.paintcomponent必须在我的if语句之外。

暂无
暂无

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

相关问题 在Eclipse中,“包”窗格中的文件夹旁边有一个感叹号。 我该如何解决? - In Eclipse there's an exclamation point next to the file folders in the packages pane. How should i fix this? 如何让我的 JButton 计数并更改我的显示? - How do I make my JButton count and change my display? 如何使播放器矩形跳变? - How should I proceed in making my player rectangle jump? 我正在努力使我的应用程序具有“保持登录状态”状态。 我该怎么办? - I'm working on having a “Keep me on Logged in” state on my app. How should i do it? 如何重置JButton? - How do I reset my JButton? 如何将Jbutton制作为矩形 - how to make a Jbutton as rectangle 如何使我的Jbutton写入文件。 我不断抛出FileNotFound异常错误 - How do I make my Jbutton write to a file. I keep getting throws FileNotFound Exception Errors 应该真的很简单,我如何让一个jbutton显示我已经通过Eclipse中的窗口构建器创建的jframe? - Should be really simple, how do I make a jbutton show a jframe i have already created via the window builder in eclipse? 我的Android应用显示的是TextViews的边界,但实际的TextViews不可见,该怎么办? - My Android app is showing bounds of TextViews but the actual TextViews aren't visible, what should I do? 当我在现有文档中进行编辑时,它会向我检索旧值而不是我的编辑值,我该怎么办? - when i edit in exist document it retrieve to me the old value not my edit value what should i do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM