简体   繁体   English

我的Java GUI代码有什么问题,Eclipse没有响应?

[英]What's wrong with my java GUI code, Eclipse have no response?

I have two class, one is MyDrawPanel , second is TwoButtons , and I use Eclipse to run my code. 我有两个类,一个是MyDrawPanel ,第二个是TwoButtons ,我使用Eclipse运行我的代码。 But I have no idea what's wrong with my code? 但是我不知道我的代码有什么问题? Eclipse have no response @@? Eclipse @@没有回应?

first class : 头等舱

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

class MyDrawPanel extends JPanel {

public MyDrawPanel() {  
    this.setForeground(Color.white);
}

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;     
    super.paintComponent(g2d);
    g.fillRect(0,0,this.getWidth(),this.getHeight());
    GradientPaint gradient = new GradientPaint(70,70,Color.LIGHT_GRAY,200,200,Color.CYAN);
    g2d.setPaint(gradient);
    g.fillOval(70, 70, 100, 100);
}

public static void main(String[] args) {
    JFrame jFrame = new JFrame();
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setSize(300, 300);
    jFrame.add(new MyDrawPanel());
    jFrame.setVisible(true);
}

}

second class : 第二类

public class TwoButtons {
JFrame frame;
JLabel label;

public static void main(String[] args) {
    TwoButtons gui = new TwoButtons();
    gui.go();

}

private void go() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

    JButton labelButton = new JButton("change Label");
    labelButton.addActionListener(new LabelListener());

    JButton colorButton = new JButton("change Circle");
    colorButton.addActionListener(new ColorListener());

    label = new JLabel("I'm a label");
    MyDrawPanel draw = new MyDrawPanel();

    frame.getContentPane().add(BorderLayout.SOUTH,colorButton);
    frame.getContentPane().add(BorderLayout.CENTER,draw);
    frame.getContentPane().add(BorderLayout.WEST,label);
    frame.getContentPane().add(BorderLayout.EAST,labelButton);



}
class LabelListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        label.setText("Ouch!");
    }
}
class ColorListener implements ActionListener{
    public void actionPerformed(ActionEvent event){
        frame.repaint();
    }
}

} }

Can anyone tell me how to change my code? 谁能告诉我如何更改我的代码?

Add these two lines at bottom of your go method - 在go方法的底部添加这两行-

frame.setVisible(true);
frame.setSize(500,500);

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

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