java绘图问题
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
public class Draw1{
public static final int YES_NO_OPTION = 0;
public static void main(String[] args){
DrawFrame drawFrame=new DrawFrame();
drawFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
int result=JOptionPane.showConfirmDialog(null, "确认退出",null,YES_NO_OPTION);
if(result==JOptionPane.YES_OPTION)
System.exit(0);
else
return;
}
});
drawFrame.setVisible(true);
drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class DrawFrame extends JFrame{
JPanel jp1=new DrawPanel();
public DrawFrame(){
setLayout(new BorderLayout());
setSize(200,300);
setTitle("123");
add(jp1);
}
}
class DrawPanel extends JPanel{
enum Flag{STRING,RECT,CIRCLE};
Flag flag;
private JPanel jp=new JPanel();
private JPanel controlPane=new JPanel();
private ButtonGroup buttonGroup=new ButtonGroup();
private JButton yellow=new JButton("yellow");
private JButton red=new JButton("red");
private JRadioButton a= new JRadioButton("String",false);
private JRadioButton b= new JRadioButton("Rect",false);
private JRadioButton c= new JRadioButton("circle",false);
public DrawPanel(){
//事件监听
ActionListener actionListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if((JRadioButton)e.getSource()==a)
flag=Flag.STRING;
if((JRadioButton)e.getSource()==b)
flag=Flag.RECT;
if((JRadioButton)e.getSource()==c)
flag=Flag.CIRCLE;
if((JButton)e.getSource()==yellow)
jp.setBackground(Color.YELLOW);
if((JButton)e.getSource()==red)
jp.setBackground(Color.RED);
}
};
controlPane.setLayout(new GridLayout(2,1));
controlPane.add(yellow);
yellow.addActionListener(actionListener);
controlPane.add(red);
red.addActionListener(actionListener);
controlPane.add(a);
controlPane.add(b);
controlPane.add(c);
buttonGroup.add(a);
a.addActionListener(actionListener);
buttonGroup.add(b);
b.addActionListener(actionListener);
buttonGroup.add(c);
c.addActionListener(actionListener);
add(jp);
add(controlPane,BorderLayout.NORTH);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(flag==Flag.STRING)
g.drawString("abc",100,50);
if(flag==Flag.RECT)
g.fillRect(20,20,130,140);
if(flag==Flag.CIRCLE)
g.fillOval(20,20,150,210);
}
}
编译通过,运行时候事件监听出现问题
求大牛能调试一下 展开
你这个主要是布局管理器太过混乱,窗口大小定义不当
class DrawPanel extends JPanel{
你的public void paintComponent(Graphics g){
是在DrawPanel区域内执行的
结果你直接
add(jp);
add(controlPane,BorderLayout.NORTH);
一、没有对DrawPanel进行布局管理器管理
二、直接调用add()方法,覆盖了你的作图区域
三、建议你引用ActionListener接口,继承只能继承一个,接口则无此限制
四、修改窗口大小,以配合程序
五、附赠你一段使窗口居中显示的代码
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.event.*;
public class Draw1{
public static final int YES_NO_OPTION = 0;
public static void main(String[] args){
DrawFrame drawFrame=new DrawFrame();
drawFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
int result=JOptionPane.showConfirmDialog(null, "确认退出",null,YES_NO_OPTION);
if(result==JOptionPane.YES_OPTION)
System.exit(0);
else
return;
}
});
drawFrame.setVisible(true);
drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class DrawFrame extends JFrame{
Dimension scrnsize;
Toolkit toolkit = Toolkit.getDefaultToolkit();
JPanel jp1=new DrawPanel();
public DrawFrame(){
setLayout(new BorderLayout());
setSize(300,400);
scrnsize = toolkit.getScreenSize();
setLocation(scrnsize.width / 2 - getWidth() / 2,
scrnsize.height / 2 - getHeight() / 2);
setTitle("123");
add(jp1);
}
}
class DrawPanel extends JPanel implements ActionListener{
enum Flag{STRING,RECT,CIRCLE};
Flag flag;
private JPanel total=new JPanel();
private JPanel jp=new JPanel();
private JPanel controlPane=new JPanel();
private ButtonGroup buttonGroup=new ButtonGroup();
private JButton yellow=new JButton("yellow");
private JButton red=new JButton("red");
private JRadioButton a= new JRadioButton("String",false);
private JRadioButton b= new JRadioButton("Rect",false);
private JRadioButton c= new JRadioButton("circle",false);
public DrawPanel(){
jp.setLayout(new GridLayout(1,2));
jp.add(yellow);
yellow.addActionListener(this);
jp.add(red);
red.addActionListener(this);
controlPane.add(a);
controlPane.add(b);
controlPane.add(c);
buttonGroup.add(a);
a.addActionListener(this);
buttonGroup.add(b);
b.addActionListener(this);
buttonGroup.add(c);
c.addActionListener(this);
this.setLayout( new BorderLayout());
total.setLayout(new BorderLayout());
total.add(jp,BorderLayout.NORTH);
total.add(controlPane,BorderLayout.SOUTH);
//add(jp,BorderLayout.SOUTH);
//add(controlPane,BorderLayout.SOUTH);
this.add(total,BorderLayout.SOUTH);
}
//事件监听
public void actionPerformed(ActionEvent e){
if(e.getSource()==a)
flag=Flag.STRING;
if(e.getSource()==b)
flag=Flag.RECT;
if(e.getSource()==c)
flag=Flag.CIRCLE;
if(e.getSource()==yellow)
this.setBackground(Color.YELLOW);
if(e.getSource()==red)
this.setBackground(Color.RED);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(flag==Flag.STRING)
g.drawString("abc",100,50);
if(flag==Flag.RECT)
g.fillRect(20,20,130,140);
if(flag==Flag.CIRCLE)
g.fillOval(20,20,150,210);
}
}
ActionListener actionListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==a)
flag=Flag.STRING;
if(e.getSource()==b)
flag=Flag.RECT;
if(e.getSource()==c)
flag=Flag.CIRCLE;
if(e.getSource()==yellow)
jp.setBackground(Color.YELLOW);
if(e.getSource()==red)
jp.setBackground(Color.RED);
}
};