当前位置: 技术问答>java相关
再问一个基础问题,谢谢大家!!!!
来源: 互联网 发布时间:2015-05-26
本文导语: import javax.swing.*; import java.awt.Graphics.*; import java.awt.event.*; import java.awt.*; import java.awt.event.*; public class NewDemo extends JFrame implements ActionListener { JButton jb1=new JButton(); public NewDemo() { this.getContentPane().setLayout(nul...
import javax.swing.*;
import java.awt.Graphics.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
public class NewDemo extends JFrame implements ActionListener
{
JButton jb1=new JButton();
public NewDemo()
{
this.getContentPane().setLayout(null);
jb1.addActionListener(this);
jb1.setText("创建进程");
this.getContentPane().add(jb1);
}
public static void main(String agrs[])
{
NewDemo nDemo=new NewDemo();
nDemo.setSize(new Dimension(400,400));
nDemo.show();
}
public void actionPerformed(ActionEvent e)
{
Object objCommand=e.getSource();
if(objCommand==jb1)
JOptionPane.showMessageDialog (this,"按下了!");
}
我想在这个按牛按下事件里,再添加一个事件,当按牛按下就显示一个图形( g.fillRect(200,20,200,100);),该怎么做?
import java.awt.Graphics.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
public class NewDemo extends JFrame implements ActionListener
{
JButton jb1=new JButton();
public NewDemo()
{
this.getContentPane().setLayout(null);
jb1.addActionListener(this);
jb1.setText("创建进程");
this.getContentPane().add(jb1);
}
public static void main(String agrs[])
{
NewDemo nDemo=new NewDemo();
nDemo.setSize(new Dimension(400,400));
nDemo.show();
}
public void actionPerformed(ActionEvent e)
{
Object objCommand=e.getSource();
if(objCommand==jb1)
JOptionPane.showMessageDialog (this,"按下了!");
}
我想在这个按牛按下事件里,再添加一个事件,当按牛按下就显示一个图形( g.fillRect(200,20,200,100);),该怎么做?
|
public class NewDemo extends JFrame implements ActionListener
{
JButton jb1=new JButton();
private boolean bButton = false;
public NewDemo()
{
this.getContentPane().setLayout(null);
jb1.addActionListener(this);
jb1.setText("创建进程");
this.getContentPane().add(jb1);
}
public static void main(String agrs[])
{
NewDemo nDemo=new NewDemo();
nDemo.setSize(new Dimension(400,400));
nDemo.show();
}
public void actionPerformed(ActionEvent e)
{
Object objCommand=e.getSource();
if(objCommand==jb1)
JOptionPane.showMessageDialog (this,"按下了!");
bButton = true;
repaint();
}
public void paint(Graphics g)
{
if (bButton)
g.fillRect(200,20,200,100);
}
}
{
JButton jb1=new JButton();
private boolean bButton = false;
public NewDemo()
{
this.getContentPane().setLayout(null);
jb1.addActionListener(this);
jb1.setText("创建进程");
this.getContentPane().add(jb1);
}
public static void main(String agrs[])
{
NewDemo nDemo=new NewDemo();
nDemo.setSize(new Dimension(400,400));
nDemo.show();
}
public void actionPerformed(ActionEvent e)
{
Object objCommand=e.getSource();
if(objCommand==jb1)
JOptionPane.showMessageDialog (this,"按下了!");
bButton = true;
repaint();
}
public void paint(Graphics g)
{
if (bButton)
g.fillRect(200,20,200,100);
}
}
|
设置字体颜色:
g.setColor(Color.orange);
g.setColor(Color.blue);
等等
也可以用Color的构造器自己构造颜色,比如
Color c=new Color(255,255,0);
g.setColor(c);
g.fillRect(100,100,100,100);
g.setColor(Color.orange);
g.setColor(Color.blue);
等等
也可以用Color的构造器自己构造颜色,比如
Color c=new Color(255,255,0);
g.setColor(c);
g.fillRect(100,100,100,100);
|
可以把图形放在JLabel上,这样操作会方便一点。