public class Demo2
{
public static void main(String[]args)
{
int i,j
for(i=2i<=50i++)
{
for(j=2j<=i/2j++)
if(j%i==0)break
if(j>i/2)
System.out.println(i+"是质数")
}
}
}
3,排*问题[注意别把for后面加了号]。
import javax.swing.*
public class Demo15
{
public static void main(String[]args)
{
int n,j,k,space
String result=(String)JOptionPane.showInputDialog(null,"请输入一个整数","输入对话框",JOptionPane.PLAIN_MESSAGE,null,null,null)
n=Integer.parseInt(result)
space=40
for(j=0j<=nj++,space-=2)
{
for(int i=0i<spacei++)
System.out.print(" ")
for(k=1k<=2*j+1k++)
System.out.print(" *")
System.out.print("\n")
}
space+=4
for(j=n-1j>=0j--,space+=2)
{
for(int i=0i<spacei++)
System.out.print(" ")
for(k=1k<=2*j+1k++)
System.out.print(" *")
System.out.print("\n")
}
}
}
5,红绿按钮题:
import javax.swing.*
import java.awt.*
import java.awt.event.*
public class Demo
{
public static void main(String[]args)
{
ButtonDemo myButtonGUI=new ButtonDemo()
myButtonGUI.setVisible(true)
}
}
class ButtonDemo extends JFrame implements ActionListener
{
public static final int Width=200
public static final int Height=250
ButtonDemo()
{
setSize(Width,Height)
setTitle("按钮事件样例")
Container conPane=getContentPane()
conPane.setBackground(Color.YELLOW)
conPane.setLayout(new FlowLayout())
JButton redBut=new JButton("Red")
redBut.addActionListener(this)
conPane.add(redBut)
JButton greenBut=new JButton("Green")
greenBut.addActionListener(this)
conPane.add(greenBut)
}
public void acionPerformed(ActionEvent e)
{
Container conPane=getContentPane()
if(e.getActionCommand().equals("Red"))
conPane.setBackground(Color.RED)
else if(e.getActionCommand().equals("Green"))
conPane.setBackground(Color.GREEN)
else {}
}
}
7,按钮激活文本,显示文本[注意方法是小写,类是大写,所有字母都不能错]
import java.applet.*
import java.awt.*
import java.awt.event.*
public class Demo7 extends Applet implements KeyListener
{
//setSize(400,500)
int count=0
Button button=new Button()
TextArea text=new TextArea(5,20)
public void init()
{
button.addKeyListener(this)
add(button)
add(text)
}
public void keyPressed(KeyEvent e)
{
int t=e.getKeyCode()
if(t>=KeyEvent.VK_A&&t<=KeyEvent.VK_Z)
{
text.append((char)t+" ")count++
if(count%10==0) text.append("\n")
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
8,单击按钮显示单击的次数,
import javax.swing.*
import java.awt.event.*
import java.applet.*
public class Demo8 extends Applet implements ActionListener
{
int n=0
JButton button=new JButton("单击按钮可以显示点击的次数")
public void init()
{
setSize(800,600)
button.addActionListener(this)
button.setSize(34,3)
add(button)
}
public void actionPerformed(ActionEvent e)
{
n++
button.setText(n+" ")
}
}
9,画五角星。
import java.awt.*
import javax.swing.*
class MyPanel extends JPanel
{
public void paintComponent(Graphics g)
{
int r=100
int x1=100
int x2=(int)(r*(1-Math.cos((18*Math.PI)/180)))
int x3=(int)(r*(1+Math.cos((18*Math.PI)/180)))
int x4=(int)(r*(1-Math.cos((54*Math.PI)/180)))
int x5=(int)(r*(1+Math.cos((54*Math.PI)/180)))
int y1=0
int y2=(int)(r*(1-Math.sin((18*Math.PI)/180)))
int y3=(int)(r*(1-Math.sin((18*Math.PI)/180)))
int y4=(int)(r*(1+Math.sin((54*Math.PI)/180)))
int y5=(int)(r*(1+Math.sin((54*Math.PI)/180)))
g.setColor(Color.RED)
g.drawOval(0,0,200,200)
g.drawLine(x1,y1,x4,y4)
g.drawLine(x1,y1,x5,y5)
g.drawLine(x2,y2,x3,y3)
g.drawLine(x2,y2,x5,y5)
g.drawLine(x3,y3,x4,y4)
}
}
public class Demo9 extends JFrame
{
Demo9()
{
super("Demo9")
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
getContentPane().add(new MyPanel())
setSize(800,600)
setVisible(true)
}
public static void main(String[]args)
{
new Demo9()
}
}
按照你的要求编写的红绿灯程序,你看看吧,比较简单。完整的程序如下:
import java.awt.*
import java.awt.event.*
import javax.swing.*
import java.awt.Graphics
public class TrafficLight extends JFrame{
JRadioButton jrbYellow,jrbGreen,jrbRed
int flag=0
jpNewPanel jpNewPanel
public static void main(String[] args){
TrafficLight frame=new TrafficLight()
frame.setSize(500,200)
frame.setLocationRelativeTo(null)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setTitle("TrafficLight")
frame.setVisible(true)
}
public TrafficLight(){
jpNewPanel=new jpNewPanel()
add(jpNewPanel,BorderLayout.CENTER)
JPanel jpRadioButtons=new JPanel()
jpRadioButtons.setLayout(new GridLayout(1,3))
jpRadioButtons.add(jrbYellow=new JRadioButton("Yellow"))
jpRadioButtons.add(jrbGreen=new JRadioButton("Green"))
jpRadioButtons.add(jrbRed=new JRadioButton("Red"))
add(jpRadioButtons,BorderLayout.SOUTH)
ButtonGroup group=new ButtonGroup()
group.add(jrbYellow)
group.add(jrbGreen)
group.add(jrbRed)
jrbYellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=2
jpNewPanel.repaint()
}
})
jrbGreen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=1
jpNewPanel.repaint()
}
})
jrbRed.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
flag=3
jpNewPanel.repaint()
}
})
}
class jpNewPanel extends JPanel{
protected void paintComponent(Graphics g){
super.paintComponent(g)
g.drawRect(0,0,40,100)
g.drawOval(10,10,20,20)
g.drawOval(10,40,20,20)
g.drawOval(10,70,20,20)
if(flag==1){
g.setColor(Color.GREEN)
g.fillOval(10, 70, 20, 20)
}
else if(flag==2){
g.setColor(Color.YELLOW)
g.fillOval(10, 40, 20, 20)
}
else if(flag==3){
g.setColor(Color.RED)
g.fillOval(10, 10, 20, 20)
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)