简单的Java按钮来显示一个圆圈 [英] Simply Java Button to Display a Circle

查看:141
本文介绍了简单的Java按钮来显示一个圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习java,我理解除Graphics之外的其他概念,作为程序员对我来说是全新的。坦率地说,它正在逼近我的弯道。我的例子理论上应该在按下按钮时出现一个圆圈。



使用print方法进行调试我一直发现Button正确调用了所有方法并创建了一个新的循环c对象,但是在newNode()中,drawCircle()重绘()不会被调用,因此不会绘制新的对象。为什么是这个,有人可以帮助我得到这个补丁圈出现!有些人可能会注意到我使用这个例子试图帮助解决这个问题 http ://leepoint.net/notes-java/examples/graphics/circles/circles.html

这是一个网络图形程序的开始,我预计它很容易...除了在创建时显示节点...即圆圈!



这段代码现在可以工作,所以我希望它可以帮助有类似问题的人,因为我知道这是一个常见的Java任务:)

  import java.awt。*; 
import java.awt.geom。*;
import javax.swing。*;
import java.awt.event。*;
import java.util。*;
////////////////////////////////////////////// /////////////////////////
public class NetGrapher
{

public static void main( String [] args){
final JFrame frame = new JFrame(NetGrapher);
frame.getContentPane()。add(new NewNode()); /////删除行
final NewNode newNode = new NewNode();
/////回答后添加,添加,frame.getContentPane()。add(newNode); (擦除上面的frame.getContent)


JPanel buttonPanel = new JPanel();
JButton button = new JButton(New Node);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println(Button Pressed);
newNode.drawCircle() ;
}
});

buttonPanel.add(button);
frame.add(buttonPanel,BorderLayout.SOUTH);

frame.setSize(600,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}
//////////////////////////////// //////////////////////////////////////
类NewNode扩展JComponent
{

public ArrayList< Circle> _circles = new ArrayList< Circle>();

public void paintComponent(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0,0,600,600);

System.out.println(RePaint);
for(Circle c:_circles){
System.out.println(Each C);
g.setColor(Color.BLACK);
c.draw(g);



public void drawCircle(){

System.out.println(drawCircle Implemented);
Circle c =新Circle(100,100,100,100);
_circles.add(c);
repaint();
}

}

//////////////////////////// /////////////////////////////////////////
class Circle
{
int x,y,z,a;

Circle(int _x,int _y,int _z,int _a){
this.x = _x;
this.y = _y;
this.z = _z;
this.a = _a;


public void draw(Graphics g){

System.out.println(调用绘图方法);
g.setColor(Color.BLACK);
g.fillOval(x,y,z,a);
}

}


解决方案

您正在使用 NewNode

 框架的两个不同实例。 getContentPane()。add(new NewNode()); 
final NewNode newNode = new NewNode();

在您的动作监听器中,您调用 newNode.drawCircle()

顺便说一句,你注意到你有两个 Circle code>类,其中第一个做了一些奇怪的事情(比如向它无法访问的_circles中添加一个新的圆圈)?


I am currently learning java, I understand the concepts except Graphics which as a programmer is totally new to me. Quite frankly it is driving my around the bend. My example should in theory make a circle appear at the press of a Button.

Using print methods to debug I keep finding that the Button correctly called all the methods and creates a new circle c object but in the newNode().drawCircle() repaint() is never called and hence the new object not drawn. WHY IS THIS and can someone help me get this darn circle to appear!! Some may notice I resorted to using this example to try and help resolve the problem http://leepoint.net/notes-java/examples/graphics/circles/circles.html .

This was meant to be the start of a Network graphing program which i PRESUMED would be easy...except for displaying the nodes when created...i.e the circle!

This code now works so i hope it can help people with a similar problem, as i know this is a common java assignment :)

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
///////////////////////////////////////////////////////////////////////
public class NetGrapher
{

public static void main(String[] args){
final JFrame frame = new JFrame ("NetGrapher");
frame.getContentPane().add(new NewNode()); /////delete line
final NewNode newNode = new NewNode();
///// Revision after answer, add, frame.getContentPane().add(newNode); (erase the above    frame.getContent)


JPanel buttonPanel = new JPanel();
JButton button = new JButton ("New Node");
button.addActionListener(new ActionListener( ){
    public void actionPerformed( ActionEvent e) {
    System.out.println( "Button Pressed");
    newNode.drawCircle();
    }
});

buttonPanel.add(button);
frame.add(buttonPanel, BorderLayout.SOUTH);

frame.setSize(600,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}
//////////////////////////////////////////////////////////////////////
class NewNode extends JComponent
{

public ArrayList<Circle> _circles = new ArrayList<Circle>();

public void paintComponent(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, 600, 600);

System.out.println( "RePaint");
    for ( Circle c : _circles){
System.out.println( "Each C");
    g.setColor(Color.BLACK);
    c.draw(g);
    }
}

public void drawCircle(){

System.out.println( "drawCircle Implemented");
Circle c = new Circle(100, 100, 100, 100);
_circles.add(c);
repaint();
}

}

/////////////////////////////////////////////////////////////////////
class Circle
{
int x, y, z, a;

Circle (int _x, int _y, int _z, int _a){
this.x = _x;  
this.y = _y;
this.z = _z;
this.a = _a;
}

public void draw(Graphics g){

System.out.println( "Called In Draw Method");
g.setColor(Color.BLACK);
g.fillOval(x, y, z, a);
}

}

解决方案

You are using two different instances of NewNode

frame.getContentPane().add(new NewNode());
final NewNode newNode = new NewNode();

In your action listener you're calling newNode.drawCircle() on newNode which was not added to a content pane.

BTW have you noticed that you've got two Circle classes, where the first does some strange things (like adding a new circle into _circles which it cannot access)?

这篇关于简单的Java按钮来显示一个圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆