Ⅰ 用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對象的構造方法中啟動一個定時器,每隔一秒檢查該方向上的燈是否為綠,是則列印車輛集合和將集合中的第一輛車移除掉。