x給你代碼,附件是運行截圖。
創(chuàng)新互聯(lián)建站自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目做網(wǎng)站、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元蔡甸做網(wǎng)站,已為上家服務(wù),為蔡甸各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220
import java.awt.BorderLayout;
public class App extends JFrame implements ActionListener {
private JComboBox combox = null;
private Random random = new Random();
private MyPanel center = new MyPanel();
public App() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("point numbers :");
Integer[] items = { 2, 3, 4, 5, 6 };
combox = new JComboBox(items);
JButton button = new JButton("OK");
button.addActionListener(this);
panel.add(label, BorderLayout.WEST);
panel.add(combox, BorderLayout.CENTER);
panel.add(button, BorderLayout.EAST);
add(panel, BorderLayout.NORTH);
add(center, BorderLayout.CENTER);
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
@Override
public void actionPerformed(ActionEvent e) {
int number = (Integer) combox.getSelectedItem();
Listlt;Pointgt; points = new ArrayListlt;Pointgt;();
for (int i = 0; i lt; number; i++) {
Point point = new Point();
point.x = random.nextInt(400) + 80;
point.y = random.nextInt(260) + 50;
points.add(point);
}
center.points = points;
// center.repaint();
repaint();
}
}
class MyPanel extends JPanel {
public Listlt;Pointgt; points = null;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (points != null) {
paintPoints(g, points);
}
}
private void paintPoints(Graphics g, Listlt;Pointgt; points) {
if (points.size() == 1) {
return;
}
Point point = points.remove(0);
for (int i = 0; i lt; points.size(); i++) {
Point tmp = points.get(i);
paintCenterPoint(g, point, tmp);
}
paintPoints(g, points);
}
private void paintCenterPoint(Graphics g, Point src, Point dest) {
// 在兩個點上畫一個直徑20的圓點
g.fillOval(src.x - 10, src.y - 10, 20, 20);
g.fillOval(dest.x - 10, dest.y - 10, 20, 20);
// 畫點連接
g.drawLine(src.x, src.y, dest.x, dest.y);
// 計算中間點位置
int x = (int) Math.floor(src.x + (dest.x - src.x) / 2);
int y = (int) Math.floor(src.y + (dest.y - src.y) / 2);
Color color = g.getColor();
g.setColor(Color.GREEN);
// 畫兩點中間的點
g.fillOval(x - 10, y - 10, 20, 20);
g.setColor(color);
}
}
java是由類組成的
一個程序中public類可有可無 ,但要加的話,public只能修飾與
你文件名相同的那一個類,搭培也就是說,一個源文件中最高只能有一個public類
類名命名規(guī)則:首字母大寫,通常由多個單詞合成一知察唯個類名,要求每個單詞的沒叢首字母也要大寫,例如class HelloWorld
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class test {
static String[] arr;
static boolean flag=true;
static ListString a;
static JTextField jtf;
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame=new JFrame();
frame.setLayout(new GridLayout(1,2));
JButton button = new JButton("毀遲抽獎");
jtf = new JTextField();
frame.add(jtf);
frame.add(button);
a = new ArrayListString();
arr = new String[]{"纖皮李張三","李四","握檔王五"};
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int i=0;
fond:
while(true){
String str = arr[(int)(Math.random()*arr.length)];
for(String b:a){
if(b.equals(str)){
if(i=arr.length){
jtf.setText("沒有人了");
break fond;
}
continue fond;
}
i++;
}
a.add(str);
jtf.setText(str);
break;
}
}
});
frame.setSize(500, 500);
frame.setLocation(500, 500);
frame.setVisible(true);
}
}
直接老蔽定義兩個接口學(xué)生接口里面定義一段大個學(xué)費的變量,老師接口里面定義一個工資變量,
eclipse里面可以自動幫你添加侍燃州相關(guān)變量的getterhe
setter方法的。