Ⅰ 用java编程: 在JFrame窗体中绘制五角星,并用红色填充
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.Point;
importjavax.swing.JLabel;
{
=-3847465547528967067L;
privatePointpointA;
privatePointpointB;
privatePointpointC;
privatePointpointD;
privatePointpointE;
privatePointpointLeftInner;
privatePointpointRightInner;
privateintpix;
privatestaticColorbgColor=Color.YELLOW;
privatestaticColorfieldColor=Color.RED;
publicstaticvoidsetBgColor(ColorbgColor){
Pentagram.bgColor=bgColor;
}
publicstaticvoidsetFieldColor(ColorfieldColor){
Pentagram.fieldColor=fieldColor;
}
/**只有width有效,把label弄成正方的*/
publicvoidsetBounds(intx,inty,intwidth,intheight){
super.setBounds(x,y,width,width);
}
/**只有width有效,把label弄成正方的*/
publicvoidsetSize(intwidth,intheight){
super.setSize(width,width);
}
publicvoidsetSize(intsize){
setSize(size,size);
}
publicPentagram(intsize){
pix=size/12;
setSize(pix*12);
initPoints();
}
privatevoidinitPoints(){
pointA=newPoint(6*pix,0);//上边中间点
pointB=newPoint(0,4*pix);//左边上面点
pointC=newPoint(12*pix,4*pix);//右边上面点
pointD=newPoint(3*pix,12*pix);//左边下面点
pointE=newPoint(9*pix,12*pix);//右边下面点
pointLeftInner=newPoint(5*pix,4*pix);//左边里面的点
pointRightInner=newPoint(7*pix,4*pix);//右边里面的点
}
privatevoiddraw(Graphicsg){
fillPolygon(g,pointA,pointLeftInner,pointRightInner);
fillPolygon(g,pointB,pointE,pointRightInner);
fillPolygon(g,pointC,pointD,pointLeftInner);
}
/**
*已知点,填充多边形。
*@paramg画笔
*@parampoints已知的'点'们
*/
privatevoidfillPolygon(Graphicsg,Point...points){
int[]xPoints=newint[points.length];
int[]yPoints=newint[points.length];
for(inti=0;i<points.length;i++){
xPoints[i]=points[i].x;
yPoints[i]=points[i].y;
}
g.fillPolygon(xPoints,yPoints,points.length);
}
publicvoidpaint(Graphicsg){
g.setColor(bgColor);
g.fillRect(0,0,12*pix,12*pix);
g.setColor(fieldColor);
draw(g);
}
}
/**以下是测试代码*/
importjavax.swing.JFrame;
importjavax.swing.JLabel;
publicclassTest{
publicstaticvoidmain(String[]args){
JFrameframe=newJFrame();
frame.setBounds(200,200,400,400);
JLabellabel=newPentagram(frame.getSize().height/2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.setVisible(true);
}
}
Ⅱ java窗口编程中有什么 能精确定位的方法
先设置不使用布局管理器:setLayout(null)。 然后所有控件用setBounds(left, top, width, height)设置绝对定位(left、top)和尺寸(width、height)。 public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = f.getContentPane(); c.setLayout(null); JButton b = new JButton("button"); b.setBounds(100, 50, 80, 40); c.add(b); f.setSize(400, 300); f.setVisible(true); }
Ⅲ JAVA 编写一个带有窗口的应用程序
这样:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
public class MainClass extends JFrame {
public JComboBox box;
int flag = 0;
jpNewPanel jpNewPanel;
public static void main(String[] args) {
MainClass frame = new MainClass();
frame.setBounds(650,300,550,550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("信号灯");
frame.setVisible(true);
}
public MainClass() {
box = new JComboBox();
box.addItem("请选择");
box.addItem("红灯");
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = box.getSelectedIndex();
jpNewPanel.repaint();
}
});
box.addItem("黄灯");
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = box.getSelectedIndex();
jpNewPanel.repaint();
}
});
box.addItem("绿灯");
box.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
flag = box.getSelectedIndex();
jpNewPanel.repaint();
}
});
add(box, BorderLayout.NORTH);
jpNewPanel = new jpNewPanel();
add(jpNewPanel, BorderLayout.CENTER);
}
class jpNewPanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(150, 0, 120, 120);
if (flag == 1) {
g.setColor(Color.RED);
g.fillOval(150, 0, 120, 120);
} else if (flag == 2) {
g.setColor(Color.YELLOW);
g.fillOval(150, 0, 120, 120);
} else if (flag == 3) {
g.setColor(Color.GREEN);
g.fillOval(150, 0, 120, 120);
}
}
}
}
注意事项
每个Road对象都有一个name成员变量来代表方向,有一个vehicles成员变量来代表方向上的车辆集合。
在Road对象的构造方法中启动一个线程每隔一个随机的时间向vehicles集合中增加一辆车(用一个“路线名_id”形式的字符串进行表示)。
在Road对象的构造方法中启动一个定时器,每隔一秒检查该方向上的灯是否为绿,是则打印车辆集合和将集合中的第一辆车移除掉。