本来正常情况下,几个buuton都应出来的,可是有时出来,有时不显示出来。代码如下:
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Color;
import java.applet.Applet;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest //implements MouseListener
{
static int xPoit;
static int yPoit;
static Graphics gp;
static int num=1;
public static void main(String[] args)
{
Frame f=new Frame("实例窗口");
f.setSize(600,400);
f.setLocation(100,100);
f.setVisible(true);
gp=f.getGraphics();
f.setLayout(new FlowLayout());
Button bt1=new Button("中");
Button bt2=new Button("东");
Button bt3=new Button("西");
Button bt4=new Button("南");
Button bt5=new Button("北");
f.add(bt1,"Center");
myActionListener ml=new myActionListener();
bt1.addActionListener(ml);
f.add(bt2,"East");
f.add(bt3,"West");
f.add(bt4,"South");
f.add(bt5,"North");
bt2.addActionListener(ml);
bt3.addActionListener(ml);
bt4.addActionListener(ml);
bt5.addActionListener(ml);
//f.setBackground(Color.RED);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent emp)
{
xPoit=emp.getX();
yPoit=emp.getY();
}
public void mouseReleased(MouseEvent emd)
{
gp.drawOval(xPoit,yPoit,emd.getX()-xPoit,emd.getY()-yPoit);
System.out.println("画第"+FrameTest.num+"个圆");
FrameTest.num++;
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
class myActionListener implements ActionListener
{
Button gg;
public void actionPerformed(ActionEvent e)
{
gg=(Button)e.getSource();
//System.out.println(gg.getLabel());
//String str=e.getActionCommand();
if(gg.getLabel()=="中")
{
System.out.println("called program 1");
}
else
{
System.out.println("called program 2");
}
}
}
7 个解决方案
#1
f.setVisible(true);这句的位置不对
修改后: 见红色字体
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Color;
import java.applet.Applet;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest extends Frame //implements MouseListener
{
static int xPoit;
static int yPoit;
static Graphics gp;
static int num=1;
public static void main(String[] args)
{
Frame f=new Frame("实例窗口");
f.setSize(600,400);
f.setLocation(100,100);
gp=f.getGraphics();
f.setLayout(new FlowLayout());
Button bt1=new Button("中");
Button bt2=new Button("东");
Button bt3=new Button("西");
Button bt4=new Button("南");
Button bt5=new Button("北");
f.add(bt1,"Center");
myActionListener ml=new myActionListener();
bt1.addActionListener(ml);
f.add(bt2,"East");
f.add(bt3,"West");
f.add(bt4,"South");
f.add(bt5,"North");
f.setVisible(true);
bt2.addActionListener(ml);
bt3.addActionListener(ml);
bt4.addActionListener(ml);
bt5.addActionListener(ml);
//f.setBackground(Color.RED);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent emp)
{
xPoit=emp.getX();
yPoit=emp.getY();
}
public void mouseReleased(MouseEvent emd)
{
gp.drawOval(xPoit,yPoit,emd.getX()-xPoit,emd.getY()-yPoit);
System.out.println("画第"+FrameTest.num+"个圆");
FrameTest.num++;
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
class myActionListener implements ActionListener
{
Button gg;
public void actionPerformed(ActionEvent e)
{
gg=(Button)e.getSource();
//System.out.println(gg.getLabel());
//String str=e.getActionCommand();
if(gg.getLabel()=="中")
{
System.out.println("called program 1");
}
else
{
System.out.println("called program 2");
}
}
}
这样再试下吧
修改后: 见红色字体
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Color;
import java.applet.Applet;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest extends Frame //implements MouseListener
{
static int xPoit;
static int yPoit;
static Graphics gp;
static int num=1;
public static void main(String[] args)
{
Frame f=new Frame("实例窗口");
f.setSize(600,400);
f.setLocation(100,100);
gp=f.getGraphics();
f.setLayout(new FlowLayout());
Button bt1=new Button("中");
Button bt2=new Button("东");
Button bt3=new Button("西");
Button bt4=new Button("南");
Button bt5=new Button("北");
f.add(bt1,"Center");
myActionListener ml=new myActionListener();
bt1.addActionListener(ml);
f.add(bt2,"East");
f.add(bt3,"West");
f.add(bt4,"South");
f.add(bt5,"North");
f.setVisible(true);
bt2.addActionListener(ml);
bt3.addActionListener(ml);
bt4.addActionListener(ml);
bt5.addActionListener(ml);
//f.setBackground(Color.RED);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent emp)
{
xPoit=emp.getX();
yPoit=emp.getY();
}
public void mouseReleased(MouseEvent emd)
{
gp.drawOval(xPoit,yPoit,emd.getX()-xPoit,emd.getY()-yPoit);
System.out.println("画第"+FrameTest.num+"个圆");
FrameTest.num++;
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
class myActionListener implements ActionListener
{
Button gg;
public void actionPerformed(ActionEvent e)
{
gg=(Button)e.getSource();
//System.out.println(gg.getLabel());
//String str=e.getActionCommand();
if(gg.getLabel()=="中")
{
System.out.println("called program 1");
}
else
{
System.out.println("called program 2");
}
}
}
这样再试下吧
#2
1楼兄弟,这样也不对,Button是出来了,可是鼠标拖动画图又报错了。
#3
先解决楼主自己程序中的问题
f.setLayout(new FlowLayout());
和
f.add(bt1,"Center");
f.add(bt2,"East");
f.add(bt3,"West");
f.add(bt4,"South");
f.add(bt5,"North");
矛盾
f.add(bt1,"Center"); 这种方式是BorderLayout中添加组件的方式,既然已经设置为FlowLayout,那就不能这样添加,f.add(bt1); 这样才对,五个都改了就好了。
另外,强烈建议楼主使用swing包中的组件,还有注意编码规范,老三我改了一下楼主东东,如下
f.setLayout(new FlowLayout());
和
f.add(bt1,"Center");
f.add(bt2,"East");
f.add(bt3,"West");
f.add(bt4,"South");
f.add(bt5,"North");
矛盾
f.add(bt1,"Center"); 这种方式是BorderLayout中添加组件的方式,既然已经设置为FlowLayout,那就不能这样添加,f.add(bt1); 这样才对,五个都改了就好了。
另外,强烈建议楼主使用swing包中的组件,还有注意编码规范,老三我改了一下楼主东东,如下
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class FrameTest extends JFrame implements ActionListener
{
private static final long serialVersionUID = 3856883799347234725L;
private Container contentPane;
private JButton btnCenter;
private JButton btnEast;
private JButton btnWest;
private JButton btnSouth;
private JButton btnNorth;
private Graphics g;
private int x;
private int y;
private int num = 1;
public static void main(String[] args) throws Exception
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new FrameTest();
}
public FrameTest()
{
contentPane = this.getContentPane();
btnCenter = new JButton("中");
btnEast = new JButton("东");
btnWest = new JButton("西");
btnSouth = new JButton("南");
btnNorth = new JButton("北");
this.setTitle("实例窗口");
contentPane.setLayout(new FlowLayout());
btnCenter.addActionListener(this);
btnEast.addActionListener(this);
btnWest.addActionListener(this);
btnSouth.addActionListener(this);
btnNorth.addActionListener(this);
contentPane.add(btnCenter);
contentPane.add(btnEast);
contentPane.add(btnWest);
contentPane.add(btnSouth);
contentPane.add(btnNorth);
contentPane.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e)
{
g.drawOval(x, y, e.getX() - x, e.getY() - y);
System.out.println("画第" + num + "个圆");
num++;
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
g = contentPane.getGraphics();
}
public void actionPerformed(ActionEvent e)
{
JButton source = (JButton)e.getSource();
if(source.getText().equals("中"))
{
System.out.println("called program 1");
}
else
{
System.out.println("called program 2");
}
}
}
#4
还有,楼主若要想真正意义上实现画圆的功能,以上的程序远远不够,你现在这个在调整窗口大小或者最小化后再显示出来(应该还有其他情况),那以前画的东东都没了,我上面改的也仅仅只是用swing包中的组件实现了楼主帖的程序中的功能而已,要想彻底解决问题,老三我帖上一个类似的供楼主参考(
需要在JDK5及以上的环境中运行)
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class DrawPane extends JComponent
{
private static final long serialVersionUID = -8963598853892068851L;
private int startX = 0;
private int startY = 0;
private int endX = 0;
private int endY = 0;
private List<Draw> drawList = new ArrayList<Draw>();
private Color color = Color.BLUE;
private Font font = new Font("Courier New", Font.BOLD, 14);
private boolean drawing = false;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
final DrawPane draw = new DrawPane();
JFrame frame = new JFrame();
frame.setTitle("Draw Demo");
frame.add(draw, BorderLayout.CENTER);
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.WHITE);
frame.setVisible(true);
}
});
}
public DrawPane()
{
super();
init();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(Draw draw: drawList)
{
draw.draw(g);
}
if(drawing)
{
g.drawString(endX + "," + endY, endX, endY);
g.drawLine(startX, startY, endX, endY);
}
}
private void init()
{
addMouseListener(new MouseL());
addMouseMotionListener(new MouseMotionL());
addComponentListener(new ComponentL());
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public Color getColor()
{
return color;
}
public void setColor(Color color)
{
this.color = color;
}
public Font getFont()
{
return font;
}
public void setFont(Font font)
{
this.font = font;
}
private class MouseL extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
startX = e.getX();
startY = e.getY();
drawList.add(new Text(font, color, startX, startY));
repaint();
}
public void mouseReleased(MouseEvent e)
{
drawing = false;
endX = e.getX();
endY = e.getY();
drawList.add(new Line(font, color, startX, startY, endX, endY));
drawList.add(new Text(font, color, endX, endY));
repaint();
startX = 0;
startY = 0;
endX = 0;
endY = 0;
}
}
private class MouseMotionL extends MouseMotionAdapter
{
public void mouseDragged(final MouseEvent e)
{
drawing = true;
endX = e.getX();
endY = e.getY();
repaint();
}
}
private class ComponentL extends ComponentAdapter
{
public void componentResized(final ComponentEvent e)
{
repaint();
}
}
private class Line implements Draw
{
private int orgX = 0;
private int orgY = 0;
private int endX = 0;
private int endY = 0;
private Font font;
private Color color;
public Line(Font font, Color color, int orgX, int orgY, int endX, int endY)
{
this.orgX = orgX;
this.orgY = orgY;
this.endX = endX;
this.endY = endY;
this.color = color;
this.font = font;
}
public void draw(Graphics g)
{
g.setFont(font);
g.setColor(color);
g.drawLine(orgX, orgY, endX, endY);
}
}
private class Text implements Draw
{
private int orgX = 0;
private int orgY = 0;
private Font font;
private Color color;
public Text(Font font, Color color, int orgX, int orgY)
{
this.orgX = orgX;
this.orgY = orgY;
this.color = color;
this.font = font;
}
public void draw(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(2));
g.setFont(font);
g.setColor(color);
g.drawString(orgX + "," + orgY, orgX, orgY);
}
}
private interface Draw
{
public void draw(Graphics g);
}
}
#5
谢谢楼上的指点和建议,小弟不胜感谢!
#6
3楼,4楼兄弟,我修改代码如下,但运行结果,还是和原来一样的
实际应用GUI的时候用Swing,我是想找出我这个用AWT写的怎么会出现这样的情况???
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest implements ActionListener
{
private int xPoit;
private int yPoit;
private Graphics gp;
private static int num=1;
private Button bt1;
private Button bt2;
private Button bt3;
private Button bt4;
private Button bt5;
FrameTest()
{
Frame f=new Frame("实例窗口");
f.setSize(600,400);
f.setLocation(100,100);
f.setVisible(true);
gp=f.getGraphics();
f.setLayout(new FlowLayout());
bt1=new Button("中");
bt2=new Button("东");
bt3=new Button("西");
bt4=new Button("南");
bt5=new Button("北");
f.add(bt1);
bt1.addActionListener(this);
f.add(bt2);
f.add(bt3);
f.add(bt4);
f.add(bt5);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt5.addActionListener(this);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent emp)
{
xPoit=emp.getX();
yPoit=emp.getY();
}
public void mouseReleased(MouseEvent emd)
{
gp.drawOval(xPoit,yPoit,emd.getX()-xPoit,emd.getY()-yPoit);
System.out.println("画第"+FrameTest.num+"个圆");
FrameTest.num++;
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
FrameTest ft=new FrameTest();
}
public void actionPerformed(ActionEvent e)
{
Button gg=(Button)e.getSource();
if(gg.getLabel()=="中"){
System.out.println("called program 1");
}else{
System.out.println("called program 2");
}
}
}
实际应用GUI的时候用Swing,我是想找出我这个用AWT写的怎么会出现这样的情况???
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest implements ActionListener
{
private int xPoit;
private int yPoit;
private Graphics gp;
private static int num=1;
private Button bt1;
private Button bt2;
private Button bt3;
private Button bt4;
private Button bt5;
FrameTest()
{
Frame f=new Frame("实例窗口");
f.setSize(600,400);
f.setLocation(100,100);
f.setVisible(true);
gp=f.getGraphics();
f.setLayout(new FlowLayout());
bt1=new Button("中");
bt2=new Button("东");
bt3=new Button("西");
bt4=new Button("南");
bt5=new Button("北");
f.add(bt1);
bt1.addActionListener(this);
f.add(bt2);
f.add(bt3);
f.add(bt4);
f.add(bt5);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt5.addActionListener(this);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent emp)
{
xPoit=emp.getX();
yPoit=emp.getY();
}
public void mouseReleased(MouseEvent emd)
{
gp.drawOval(xPoit,yPoit,emd.getX()-xPoit,emd.getY()-yPoit);
System.out.println("画第"+FrameTest.num+"个圆");
FrameTest.num++;
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
FrameTest ft=new FrameTest();
}
public void actionPerformed(ActionEvent e)
{
Button gg=(Button)e.getSource();
if(gg.getLabel()=="中"){
System.out.println("called program 1");
}else{
System.out.println("called program 2");
}
}
}
#7
6楼代码,格式是排好的,发上来就变了
#1
f.setVisible(true);这句的位置不对
修改后: 见红色字体
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Color;
import java.applet.Applet;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest extends Frame //implements MouseListener
{
static int xPoit;
static int yPoit;
static Graphics gp;
static int num=1;
public static void main(String[] args)
{
Frame f=new Frame("实例窗口");
f.setSize(600,400);
f.setLocation(100,100);
gp=f.getGraphics();
f.setLayout(new FlowLayout());
Button bt1=new Button("中");
Button bt2=new Button("东");
Button bt3=new Button("西");
Button bt4=new Button("南");
Button bt5=new Button("北");
f.add(bt1,"Center");
myActionListener ml=new myActionListener();
bt1.addActionListener(ml);
f.add(bt2,"East");
f.add(bt3,"West");
f.add(bt4,"South");
f.add(bt5,"North");
f.setVisible(true);
bt2.addActionListener(ml);
bt3.addActionListener(ml);
bt4.addActionListener(ml);
bt5.addActionListener(ml);
//f.setBackground(Color.RED);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent emp)
{
xPoit=emp.getX();
yPoit=emp.getY();
}
public void mouseReleased(MouseEvent emd)
{
gp.drawOval(xPoit,yPoit,emd.getX()-xPoit,emd.getY()-yPoit);
System.out.println("画第"+FrameTest.num+"个圆");
FrameTest.num++;
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
class myActionListener implements ActionListener
{
Button gg;
public void actionPerformed(ActionEvent e)
{
gg=(Button)e.getSource();
//System.out.println(gg.getLabel());
//String str=e.getActionCommand();
if(gg.getLabel()=="中")
{
System.out.println("called program 1");
}
else
{
System.out.println("called program 2");
}
}
}
这样再试下吧
修改后: 见红色字体
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Color;
import java.applet.Applet;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest extends Frame //implements MouseListener
{
static int xPoit;
static int yPoit;
static Graphics gp;
static int num=1;
public static void main(String[] args)
{
Frame f=new Frame("实例窗口");
f.setSize(600,400);
f.setLocation(100,100);
gp=f.getGraphics();
f.setLayout(new FlowLayout());
Button bt1=new Button("中");
Button bt2=new Button("东");
Button bt3=new Button("西");
Button bt4=new Button("南");
Button bt5=new Button("北");
f.add(bt1,"Center");
myActionListener ml=new myActionListener();
bt1.addActionListener(ml);
f.add(bt2,"East");
f.add(bt3,"West");
f.add(bt4,"South");
f.add(bt5,"North");
f.setVisible(true);
bt2.addActionListener(ml);
bt3.addActionListener(ml);
bt4.addActionListener(ml);
bt5.addActionListener(ml);
//f.setBackground(Color.RED);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent emp)
{
xPoit=emp.getX();
yPoit=emp.getY();
}
public void mouseReleased(MouseEvent emd)
{
gp.drawOval(xPoit,yPoit,emd.getX()-xPoit,emd.getY()-yPoit);
System.out.println("画第"+FrameTest.num+"个圆");
FrameTest.num++;
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
class myActionListener implements ActionListener
{
Button gg;
public void actionPerformed(ActionEvent e)
{
gg=(Button)e.getSource();
//System.out.println(gg.getLabel());
//String str=e.getActionCommand();
if(gg.getLabel()=="中")
{
System.out.println("called program 1");
}
else
{
System.out.println("called program 2");
}
}
}
这样再试下吧
#2
1楼兄弟,这样也不对,Button是出来了,可是鼠标拖动画图又报错了。
#3
先解决楼主自己程序中的问题
f.setLayout(new FlowLayout());
和
f.add(bt1,"Center");
f.add(bt2,"East");
f.add(bt3,"West");
f.add(bt4,"South");
f.add(bt5,"North");
矛盾
f.add(bt1,"Center"); 这种方式是BorderLayout中添加组件的方式,既然已经设置为FlowLayout,那就不能这样添加,f.add(bt1); 这样才对,五个都改了就好了。
另外,强烈建议楼主使用swing包中的组件,还有注意编码规范,老三我改了一下楼主东东,如下
f.setLayout(new FlowLayout());
和
f.add(bt1,"Center");
f.add(bt2,"East");
f.add(bt3,"West");
f.add(bt4,"South");
f.add(bt5,"North");
矛盾
f.add(bt1,"Center"); 这种方式是BorderLayout中添加组件的方式,既然已经设置为FlowLayout,那就不能这样添加,f.add(bt1); 这样才对,五个都改了就好了。
另外,强烈建议楼主使用swing包中的组件,还有注意编码规范,老三我改了一下楼主东东,如下
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
public class FrameTest extends JFrame implements ActionListener
{
private static final long serialVersionUID = 3856883799347234725L;
private Container contentPane;
private JButton btnCenter;
private JButton btnEast;
private JButton btnWest;
private JButton btnSouth;
private JButton btnNorth;
private Graphics g;
private int x;
private int y;
private int num = 1;
public static void main(String[] args) throws Exception
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new FrameTest();
}
public FrameTest()
{
contentPane = this.getContentPane();
btnCenter = new JButton("中");
btnEast = new JButton("东");
btnWest = new JButton("西");
btnSouth = new JButton("南");
btnNorth = new JButton("北");
this.setTitle("实例窗口");
contentPane.setLayout(new FlowLayout());
btnCenter.addActionListener(this);
btnEast.addActionListener(this);
btnWest.addActionListener(this);
btnSouth.addActionListener(this);
btnNorth.addActionListener(this);
contentPane.add(btnCenter);
contentPane.add(btnEast);
contentPane.add(btnWest);
contentPane.add(btnSouth);
contentPane.add(btnNorth);
contentPane.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e)
{
g.drawOval(x, y, e.getX() - x, e.getY() - y);
System.out.println("画第" + num + "个圆");
num++;
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
g = contentPane.getGraphics();
}
public void actionPerformed(ActionEvent e)
{
JButton source = (JButton)e.getSource();
if(source.getText().equals("中"))
{
System.out.println("called program 1");
}
else
{
System.out.println("called program 2");
}
}
}
#4
还有,楼主若要想真正意义上实现画圆的功能,以上的程序远远不够,你现在这个在调整窗口大小或者最小化后再显示出来(应该还有其他情况),那以前画的东东都没了,我上面改的也仅仅只是用swing包中的组件实现了楼主帖的程序中的功能而已,要想彻底解决问题,老三我帖上一个类似的供楼主参考(
需要在JDK5及以上的环境中运行)
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class DrawPane extends JComponent
{
private static final long serialVersionUID = -8963598853892068851L;
private int startX = 0;
private int startY = 0;
private int endX = 0;
private int endY = 0;
private List<Draw> drawList = new ArrayList<Draw>();
private Color color = Color.BLUE;
private Font font = new Font("Courier New", Font.BOLD, 14);
private boolean drawing = false;
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
final DrawPane draw = new DrawPane();
JFrame frame = new JFrame();
frame.setTitle("Draw Demo");
frame.add(draw, BorderLayout.CENTER);
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.WHITE);
frame.setVisible(true);
}
});
}
public DrawPane()
{
super();
init();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(Draw draw: drawList)
{
draw.draw(g);
}
if(drawing)
{
g.drawString(endX + "," + endY, endX, endY);
g.drawLine(startX, startY, endX, endY);
}
}
private void init()
{
addMouseListener(new MouseL());
addMouseMotionListener(new MouseMotionL());
addComponentListener(new ComponentL());
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public Color getColor()
{
return color;
}
public void setColor(Color color)
{
this.color = color;
}
public Font getFont()
{
return font;
}
public void setFont(Font font)
{
this.font = font;
}
private class MouseL extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
startX = e.getX();
startY = e.getY();
drawList.add(new Text(font, color, startX, startY));
repaint();
}
public void mouseReleased(MouseEvent e)
{
drawing = false;
endX = e.getX();
endY = e.getY();
drawList.add(new Line(font, color, startX, startY, endX, endY));
drawList.add(new Text(font, color, endX, endY));
repaint();
startX = 0;
startY = 0;
endX = 0;
endY = 0;
}
}
private class MouseMotionL extends MouseMotionAdapter
{
public void mouseDragged(final MouseEvent e)
{
drawing = true;
endX = e.getX();
endY = e.getY();
repaint();
}
}
private class ComponentL extends ComponentAdapter
{
public void componentResized(final ComponentEvent e)
{
repaint();
}
}
private class Line implements Draw
{
private int orgX = 0;
private int orgY = 0;
private int endX = 0;
private int endY = 0;
private Font font;
private Color color;
public Line(Font font, Color color, int orgX, int orgY, int endX, int endY)
{
this.orgX = orgX;
this.orgY = orgY;
this.endX = endX;
this.endY = endY;
this.color = color;
this.font = font;
}
public void draw(Graphics g)
{
g.setFont(font);
g.setColor(color);
g.drawLine(orgX, orgY, endX, endY);
}
}
private class Text implements Draw
{
private int orgX = 0;
private int orgY = 0;
private Font font;
private Color color;
public Text(Font font, Color color, int orgX, int orgY)
{
this.orgX = orgX;
this.orgY = orgY;
this.color = color;
this.font = font;
}
public void draw(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setStroke(new BasicStroke(2));
g.setFont(font);
g.setColor(color);
g.drawString(orgX + "," + orgY, orgX, orgY);
}
}
private interface Draw
{
public void draw(Graphics g);
}
}
#5
谢谢楼上的指点和建议,小弟不胜感谢!
#6
3楼,4楼兄弟,我修改代码如下,但运行结果,还是和原来一样的
实际应用GUI的时候用Swing,我是想找出我这个用AWT写的怎么会出现这样的情况???
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest implements ActionListener
{
private int xPoit;
private int yPoit;
private Graphics gp;
private static int num=1;
private Button bt1;
private Button bt2;
private Button bt3;
private Button bt4;
private Button bt5;
FrameTest()
{
Frame f=new Frame("实例窗口");
f.setSize(600,400);
f.setLocation(100,100);
f.setVisible(true);
gp=f.getGraphics();
f.setLayout(new FlowLayout());
bt1=new Button("中");
bt2=new Button("东");
bt3=new Button("西");
bt4=new Button("南");
bt5=new Button("北");
f.add(bt1);
bt1.addActionListener(this);
f.add(bt2);
f.add(bt3);
f.add(bt4);
f.add(bt5);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt5.addActionListener(this);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent emp)
{
xPoit=emp.getX();
yPoit=emp.getY();
}
public void mouseReleased(MouseEvent emd)
{
gp.drawOval(xPoit,yPoit,emd.getX()-xPoit,emd.getY()-yPoit);
System.out.println("画第"+FrameTest.num+"个圆");
FrameTest.num++;
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
FrameTest ft=new FrameTest();
}
public void actionPerformed(ActionEvent e)
{
Button gg=(Button)e.getSource();
if(gg.getLabel()=="中"){
System.out.println("called program 1");
}else{
System.out.println("called program 2");
}
}
}
实际应用GUI的时候用Swing,我是想找出我这个用AWT写的怎么会出现这样的情况???
import java.awt.Frame;
import java.awt.Button;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FrameTest implements ActionListener
{
private int xPoit;
private int yPoit;
private Graphics gp;
private static int num=1;
private Button bt1;
private Button bt2;
private Button bt3;
private Button bt4;
private Button bt5;
FrameTest()
{
Frame f=new Frame("实例窗口");
f.setSize(600,400);
f.setLocation(100,100);
f.setVisible(true);
gp=f.getGraphics();
f.setLayout(new FlowLayout());
bt1=new Button("中");
bt2=new Button("东");
bt3=new Button("西");
bt4=new Button("南");
bt5=new Button("北");
f.add(bt1);
bt1.addActionListener(this);
f.add(bt2);
f.add(bt3);
f.add(bt4);
f.add(bt5);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt5.addActionListener(this);
f.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent emp)
{
xPoit=emp.getX();
yPoit=emp.getY();
}
public void mouseReleased(MouseEvent emd)
{
gp.drawOval(xPoit,yPoit,emd.getX()-xPoit,emd.getY()-yPoit);
System.out.println("画第"+FrameTest.num+"个圆");
FrameTest.num++;
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
FrameTest ft=new FrameTest();
}
public void actionPerformed(ActionEvent e)
{
Button gg=(Button)e.getSource();
if(gg.getLabel()=="中"){
System.out.println("called program 1");
}else{
System.out.println("called program 2");
}
}
}
#7
6楼代码,格式是排好的,发上来就变了