我的编码出了什么问题?用java - swing

时间:2015-06-05 13:58:45

标签: java swing button

我打算制作这个节目。单击按钮。创建图纸。并单击另一个按钮使其移动。

但是我的开头有点不对劲。

第一。虽然我使用super& amp; actionlistener它编译得很好,但它根本不起作用。

第二。之后如何使用Thread进行编码? - >(这并不意味着教我整个代码。在搞砸了所有内容之前我想要一些建议。)

这是我的编码。

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.
}

2 个答案:

答案 0 :(得分:0)

我更改了一些代码以使其更具可读性。 您根本没有将中心 JPanel添加到框架

现在,当您打开窗口时,画面会立即显示,您可以通过if或其他内容修改它,或者在单击按钮时在JFrame中添加JPanel center

这是你的代码:

@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

答案 1 :(得分:0)

首先将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();
 }

}