Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的编码有什么问题?使用java-swing_Java_Swing_Button - Fatal编程技术网

我的编码有什么问题?使用java-swing

我的编码有什么问题?使用java-swing,java,swing,button,Java,Swing,Button,我正在努力制作这个节目。点击按钮。创建图形。并单击另一个按钮使其移动 但我的开头有点不对劲 第一。尽管我使用super&actionlistener,但它编译得很好,但根本不起作用 第二。我应该如何使用线程编写代码? ->(这并不意味着要教我完整的代码。在我把事情搞砸之前,我需要一些建议。) 这是我的代码 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; impo

我正在努力制作这个节目。点击按钮。创建图形。并单击另一个按钮使其移动

但我的开头有点不对劲

第一。尽管我使用super&actionlistener,但它编译得很好,但根本不起作用

第二。我应该如何使用线程编写代码? ->(这并不意味着要教我完整的代码。在我把事情搞砸之前,我需要一些建议。)

这是我的代码

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Hello extends JComponent{
//This is main code to create GUI
public Hello(){

EventQueue.invokeLater(new Runnable() {

public void run(){
JFrame frame = new JFrame();

frame.setLayout(new BorderLayout());
//set Frame & layout
JPanel south = new JPanel();
testpane center = new testpane();
JButton b1 = new JButton("Create");
b1.setBackground(Color.WHITE);
b1.setFont(new Font("Arial",Font.ITALIC,20));
south.add(b1);
//setting the first button to create Things. i'll create another for move

b1.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        //repaint(); -> this was my trial for not working
        center.setoutput();
    }

    //create action listener and **Here is the place I have a trouble**
});
frame.getContentPane().add(south, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Robot");
frame.setSize(500,500);
frame.setVisible(true);
}
});
}
public class testpane extends JPanel{
testpane(){

}
public void setoutput(){
repaint();
}// this is the fuction to call repaint at Hello class
    protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.BLACK);
g2.fillRect(100,100,30,30);
g2.drawRect(100, 100,30, 30);
g2.dispose();
}//this is the Thing that I want to draw
}
public static void main(String[] args){
new Hello();
}
//main class.
}

我对您的代码做了一些修改,使其更具可读性。 您只是从未将中心JPanel添加到帧中

现在,当您打开窗口时,画图立即出现,您可以通过if else或其他方式修改它,或者在单击按钮时在JFrame处添加JPanel中心

这是您的代码:

@SuppressWarnings("serial")
public class Hello extends JFrame {

// This is main code to create GUI
public Hello() {

    EventQueue.invokeLater(new Runnable() {

        public void run() {

            setLayout(new BorderLayout());

            // set Frame & layout
            JPanel south = new JPanel();
            testpane center = new testpane();
            JButton b1 = new JButton("Create");
            b1.setBackground(Color.WHITE);
            b1.setFont(new Font("Arial", Font.ITALIC, 20));
            south.add(b1);
            // setting the first button to create Things. i'll create
            // another for move

            b1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // repaint(); -> this was my trial for not working
                    center.setoutput();
                }

                // create action listener and **Here is the place I have a
                // trouble**
            });


            getContentPane().add(south, BorderLayout.SOUTH);
            getContentPane().add(center,BorderLayout.CENTER); //Here you never have added the panel to the frame
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setTitle("Robot");
            setSize(500, 500);
            setVisible(true);
        }
    });
}


///JPanel Class
public class testpane extends JPanel {  

    public void setoutput() {
        System.out.println("Entered setoutput");
        repaint();
    }

    //Paint Method
    protected void paintComponent(Graphics g) {
        System.out.println("Entered paint");
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.fillRect(100, 100, 30, 30);
        g2.drawRect(100, 100, 30, 30);
        g2.dispose();
    }
}




//Main Class
public static void main(String[] args) {
    new Hello();
}



}//End of center class

首先,像这样将JPanel添加到JFrame中

frame.add(south);
在main方法中调用另一个类时,请使用此方法

public static void main(String[] args) {
Hello h = new Hello();
}
我已经对您的代码进行了更改,现在它正在工作

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Hello extends JFrame {

public Hello() {

    EventQueue.invokeLater(new Runnable() {

        public void run() {

            setLayout(new BorderLayout());

            JPanel south = new JPanel();
            testpane center = new testpane();
            JButton b1 = new JButton("Create");
            b1.setBackground(Color.WHITE);
            b1.setFont(new Font("Arial", Font.ITALIC, 20));
            south.add(b1);

            b1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    center.setoutput();
                }

            });

            getContentPane().add(south, BorderLayout.SOUTH);
            getContentPane().add(center, BorderLayout.CENTER);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setTitle("Robot");
            setSize(500, 500);
            setVisible(true);
        }
    });
}

public class testpane extends JPanel {

    public void setoutput() {
        System.out.println("Entered setoutput");
        repaint();
    }

    protected void paintComponent(Graphics g) {
        System.out.println("Entered paint");
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.fillRect(100, 100, 30, 30);
        g2.drawRect(100, 100, 30, 30);
        g2.dispose();
    }
}

public static void main(String[] args) {
    Hello h = new Hello();
 }

}

你能更清楚地知道到底是什么不起作用/你需要知道吗?ActionListener没有在听。。我想知道如何使创建的对象移动,比如我单击按钮时。这个长方形在画面上并不吓人,我想你把事情复杂了一点。尝试制作
Hello
extend
JFrame
,不要使用
frame.getContentPane()。添加
而使用
frame.add
。我也不明白为什么要使用
Runnable
。请阅读上Swing教程中的部分,以获得有关如何构造程序的更好示例。
LabelDemo
源代码是创建Swing程序基础知识的良好起点。类名也以大写字符开头@moarCoffee,
试着让他扩展JFrame,
-不要扩展JFrame。只有在向类添加新功能时,才能扩展类。添加组件并不意味着添加功能。我非常感谢您的帮助!嗯,从这个密码。这只是已经创建了rect。但是我想要的是在我点击按钮时创建rect。现在我用按钮得到了一些东西,然后开始移动。但是我怎样才能让它停止呢?