import javax.swing.JPanel
import java.awt.*
import java.awt.event.WindowAdapter
import javax.swing.JFrame
class PaintovalPane extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g)
g.setColor(Color.pink)
g.fillOval(300, 300, 100, 100)
}
}
class PaintovalFrame extends JFrame {
public PaintovalFrame() {
setTitle("圆")
setSize(1000, 1000)
addWindowListener(new WindowAdapter() {
})
Container contentPane = getContentPane()
contentPane.add(new PaintovalPane())
}
}
public class Paintoval {
public static void main(String[] args) {
JFrame f = new PaintovalFrame()
f.show()
}
}
运行没问题,采纳吧,兄弟~~
单人版五子棋,自己写的。------------------------------------------
import java.awt.*
import java.awt.event.*
import javax.swing.*
class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11]
boolean Is_Black_True
mypanel()
{
Is_Black_True = true
for(int i = 0i <11i++)
{
for(int j = 0j <11j++)
{
chess[i][j] = 0
}
}
addMouseListener(this)
setBackground(Color.BLUE)
setBounds(0, 0, 360, 360)
setVisible(true)
}
public void mousePressed(MouseEvent e)
{
int x = e.getX()
int y = e.getY()
if(x <25 || x >330 + 25 ||y <25 || y >330+25)
{
return
}
if(chess[x/30-1][y/30-1] != 0)
{
return
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1
Is_Black_True = false
repaint()
Justisewiner()
return
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2
Is_Black_True = true
repaint()
Justisewiner()
return
}
}
void Drawline(Graphics g)
{
for(int i = 30i <= 330i += 30)
{
for(int j = 30j <= 330j+= 30)
{
g.setColor(Color.WHITE)
g.drawLine(i, j, i, 330)
}
}
for(int j = 30j <= 330j += 30)
{
g.setColor(Color.WHITE)
g.drawLine(30, j, 330, j)
}
}
void Drawchess(Graphics g)
{
for(int i = 0i <11i++)
{
for(int j = 0j <11j++)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK)
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16)
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE)
g.fillOval((i + 1) * 30 - 8, (j + 1) * 30 - 8, 16, 16)
}
}
}
}
void Justisewiner()
{
int black_count = 0
int white_count = 0
int i = 0
for(i = 0i <11i++)//横向判断
{
for(int j = 0j <11j++)
{
if(chess[i][j] == 1)
{
black_count++
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利")
Clear_Chess()
return
}
}
else
{
black_count = 0
}
if(chess[i][j] == 2)
{
white_count++
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利")
Clear_Chess()
return
}
}
else
{
white_count = 0
}
}
}
for(i = 0i <11i++)//竖向判断
{
for(int j = 0j <11j++)
{
if(chess[j][i] == 1)
{
black_count++
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利")
Clear_Chess()
return
}
}
else
{
black_count = 0
}
if(chess[j][i] == 2)
{
white_count++
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利")
Clear_Chess()
return
}
}
else
{
white_count = 0
}
}
}
for(i = 0i <7i++)//左向右斜判断
{
for(int j = 0j <7j++)
{
for(int k = 0k <5k++)
{
if(chess[i + k][j + k] == 1)
{
black_count++
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利")
Clear_Chess()
return
}
}
else
{
black_count = 0
}
if(chess[i + k][j + k] == 2)
{
white_count++
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利")
Clear_Chess()
return
}
}
else
{
white_count = 0
}
}
}
}
for(i = 4i <11i++)//右向左斜判断
{
for(int j = 6j >= 0j--)
{
for(int k = 0k <5k++)
{
if(chess[i - k][j + k] == 1)
{
black_count++
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利")
Clear_Chess()
return
}
}
else
{
black_count = 0
}
if(chess[i - k][j + k] == 2)
{
white_count++
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利")
Clear_Chess()
return
}
}
else
{
white_count = 0
}
}
}
}
}
void Clear_Chess()
{
for(int i=0i<11i++)
{
for(int j=0j<11j++)
{
chess[i][j]=0
}
}
repaint()
}
public void paint(Graphics g)
{
Drawline(g)
Drawchess(g)
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
class myframe extends Frame implements WindowListener
{
mypanel panel
myframe()
{
setLayout(null)
panel = new mypanel()
add(panel)
panel.setBounds(0,23, 360, 360)
setTitle("单人版五子棋")
setBounds(200, 200, 360, 383)
setVisible(true)
addWindowListener(this)
}
public void windowClosing(WindowEvent e)
{
System.exit(0)
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe()
}
}
你把text1.addActionListener(this)换成text1.addActionListener(
new Actionlistener(){
public void actionPerformed(ActionEvent e) {
if(e.getSource()==text1){
s=text1.getText()
if(s=="red"){
text2.setText("红色")
}
else if(s=="红色"){
text2.setText("red")
}
}
}
})
用内部匿名类或者重新自定义一个集成ActionLister的类
.addActionListener(ActionListener)方法内参数必须类型是ActionListener类,这里你直接用了this,this指这个Question5_8这个类本身,而这个类继承的是applet,使用的接口时ActionListener,所以会不能正常出结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)