java:如何点击一个按钮button1之后,在一个面板panel画直线。
下面是我做了例子,只能点击按钮后直接在Frame上画直线,而不能在Panel上画直线,求助各位帮忙。importjava.awt.Color;importjava.awt...
下面是我做了例子,只能点击按钮后直接在Frame上画直线,而不能在Panel上画直线,求助各位帮忙。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class aa extends JFrame {
public static void main(String[] args){
aa f = new aa();
f.setVisible(true);
}
public aa(){
super();
getContentPane().setLayout(null);
setBounds(100, 100, 400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setLayout(null);
p.setBounds(100,100, 200, 200);
p.setBackground(Color.red);
getContentPane().add(p);
final JButton b = new JButton();
b.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
Graphics g = getGraphics();
g.drawLine(20, 20, 200, 200);
}
});
b.setText("画");
b.setBounds(73,27,86,28);
p.add(b);
}
} 展开
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class aa extends JFrame {
public static void main(String[] args){
aa f = new aa();
f.setVisible(true);
}
public aa(){
super();
getContentPane().setLayout(null);
setBounds(100, 100, 400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setLayout(null);
p.setBounds(100,100, 200, 200);
p.setBackground(Color.red);
getContentPane().add(p);
final JButton b = new JButton();
b.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
Graphics g = getGraphics();
g.drawLine(20, 20, 200, 200);
}
});
b.setText("画");
b.setBounds(73,27,86,28);
p.add(b);
}
} 展开
2个回答
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class aa extends JFrame {
public static void main(String[] args){
aa f = new aa();
f.setVisible(true);
}
public aa(){
super();
getContentPane().setLayout(null);
setBounds(100, 100, 400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel p = new JPanel();
p.setLayout(null);
p.setBounds(100,100, 200, 200);
p.setBackground(Color.red);
getContentPane().add(p);
final JButton b = new JButton();
b.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
Graphics g = p.getGraphics();
g.drawLine(20, 20, 200, 200);
}
});
b.setText("画");
b.setBounds(73,27,86,28);
p.add(b);
}
}
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class aa extends JFrame {
public static void main(String[] args){
aa f = new aa();
f.setVisible(true);
}
public aa(){
super();
getContentPane().setLayout(null);
setBounds(100, 100, 400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel p = new JPanel();
p.setLayout(null);
p.setBounds(100,100, 200, 200);
p.setBackground(Color.red);
getContentPane().add(p);
final JButton b = new JButton();
b.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
Graphics g = p.getGraphics();
g.drawLine(20, 20, 200, 200);
}
});
b.setText("画");
b.setBounds(73,27,86,28);
p.add(b);
}
}
更多追问追答
追问
非常感谢,你真厉害,能再告诉我为什么要点击两次才画直线,怎样才能让它实现单击画直线呢?
追答
不需要点击两次啊,不是点击一次就画线了么?
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class aa extends JFrame {
public static void main(String[] args){
aa f = new aa();
f.setVisible(true);
}
bb p;
public aa(){
super();
getContentPane().setLayout(null);
setBounds(100, 100, 400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new bb();
getContentPane().add(p);
}
private class bb extends JPanel{
bb(){
setLayout(null);
setBounds(100,100, 200, 200);
setBackground(Color.red);
final JButton b = new JButton();
b.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
Graphics g = getGraphics();
g.drawLine(20, 20, 200, 200);
}
});
b.setText("画");
b.setBounds(73,27,86,28);
add(b);
}
}
}
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class aa extends JFrame {
public static void main(String[] args){
aa f = new aa();
f.setVisible(true);
}
bb p;
public aa(){
super();
getContentPane().setLayout(null);
setBounds(100, 100, 400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p = new bb();
getContentPane().add(p);
}
private class bb extends JPanel{
bb(){
setLayout(null);
setBounds(100,100, 200, 200);
setBackground(Color.red);
final JButton b = new JButton();
b.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
Graphics g = getGraphics();
g.drawLine(20, 20, 200, 200);
}
});
b.setText("画");
b.setBounds(73,27,86,28);
add(b);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询