java在JFrame上画东西,主要是使用paint方法,代码如下:
import javaawtColor;import javaawtGraphics;
import javaxswingJFrame;
import javaxswingJPanel;
public class Draw extends JFrame{
JPanel jPanel=new JPanel();
public Draw() {
jPanelsetBackground(Colorred);
add(jPanel);
Drawation drawaction=new Drawation();//添加画图,把上面jpanel的设置给覆盖了;要是先添加画图再添加
add(drawaction); //jpanel则把画图覆盖了
}
public static void main(String[] args){
Draw draw=new Draw();
drawsetTitle("abc");
drawsetSize(300,300);
drawsetVisible(true);
drawsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
}
class Drawation extends JPanel{
public void paintComponent(Graphics g){
superpaintComponents(g);
gdrawString("agagh", 50, 45);
}
}
运行结果如下:
package bdzhidao;import javaawt;
import javaxswing;
import javaawtevent;
public class DrawingBoard extends JFrame{
int[] xPoint={200,300,500};
int[] yPoint={100,180,240};
static int m=15;
static Color color=new Color(255,0,0);
private JComboBox jcb2=new JComboBox(new Object[]{
" 20"," 28"," 36"," 44"," 52"," 72 "
});
private JComboBox jcb3=new JComboBox(new Object[]{
"红色","黑色","蓝色","绿色"
});
public DrawingBoard(){
JMenuBar Bar=new JMenuBar();
JMenu jmFile=new JMenu("文件");
JMenuItem jmi1=new JMenuItem("新建");
JMenuItem jmi2=new JMenuItem("打开");
JMenuItem jmi3=new JMenuItem("保存");
JMenuItem jmi4=new JMenuItem("退出");
jmFileadd(jmi1);
jmFileadd(jmi2);
jmFileadd(jmi3);
jmFileadd(jmi4);
JLabel jlb1=new JLabel("线条");
JLabel jlb2=new JLabel("线宽");
JLabel jlb3=new JLabel("颜色");
final JComboBox jcb1=new JComboBox(new Object[]{
"实线","虚线"
});
JButton jbt0=new JButton("直线");
JButton jbt1=new JButton("三角形");
JButton jbt2=new JButton("圆");
JButton jbt3=new JButton("矩形");
JPanel jp1=new JPanel();
jp1add(jlb1);
jp1add(jcb1);
jp1add(jlb2);
jp1add(jcb2);
jp1add(jlb3);
jp1add(jcb3);
jp1add(jbt0);
jp1add(jbt1);
jp1add(jbt2);
jp1add(jbt3);
final DrawingPanel jp2=new DrawingPanel();
jp2setBackground(Colorwhite);
jp2setPreferredSize(new Dimension(736,300));
thisadd(Bar);
thisadd(jp1,BorderLayoutCENTER);
thisadd(jp2,BorderLayoutSOUTH);
jcb1addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(jcb1getSelectedItem()=="实线"){
Graphics g=jp2getGraphics();
gdrawLine(10,10,736,10);
}
if(jcb1getSelectedItem()=="虚线"){
Graphics g=jp2getGraphics();
for(int i=1;i<100;i++){
gdrawLine(6i-6,50,6i-3,50);
}
}
}
});
jcb2addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(jcb2getSelectedItem()==" 20"){
m=20;
repaint();
}
if(jcb2getSelectedItem()==" 28"){
m=28;repaint();
}
if(jcb2getSelectedItem()==" 36"){
m=36;repaint();
}
if(jcb2getSelectedItem()==" 44"){
m=44;repaint();
}
if(jcb2getSelectedItem()==" 52"){
m=52;repaint();
}
if(jcb2getSelectedItem()==" 72 "){
m=72;repaint();
}
}
});
jcb3addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
if(jcb3getSelectedItem()=="红色"){
color=new Color(255,0,0);
repaint();
}
if(jcb3getSelectedItem()=="蓝色"){
color=new Color(0,0,255);
repaint();
}
if(jcb3getSelectedItem()=="黑色"){
color=new Color(0,0,0);
repaint();
}
if(jcb3getSelectedItem()=="绿色"){
color=new Color(0,128,0);
repaint();
}
}
});
jbt0addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics g=jp2getGraphics();
gdrawLine(10,10,736,10);
}
});
jbt1addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics grap=jp2getGraphics();//创建所画组件对象
grapdrawPolygon(xPoint,yPoint,3);
}
});
jbt2addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics gr=jp2getGraphics();
grdrawOval(100,10,200,200);
}
});
jbt3addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics G= jp2getGraphics();
GdrawRect(500,50,200,200);
}
});
}
public static void main(String[] args){
JFrame frame=new DrawingBoard();
framesetTitle("测试画图板");
framesetSize(736,379);
framesetLocation(100,100);
framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
framesetVisible(true);
}
static class DrawingPanel extends JPanel{
protected void paintComponent(Graphics g){
superpaintComponent(g);
gsetFont(new Font("宋体",FontBOLD,m));
gsetColor(color);
gdrawString("Heleoeo ",100, 100);
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)