重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
x给你代码,附件是运行截图。
创新互联建站自2013年创立以来,是专业互联网技术服务公司,拥有项目做网站、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元蔡甸做网站,已为上家服务,为蔡甸各地企业和个人服务,联系电话: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类
类名命名规则:首字母大写,通常由多个单词合成一知察唯个类名,要求每个单词的没丛首字母也要大写,例如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);
}
}
直接老蔽定义两个接口学生接口里面定义一段大个学费的变量,老师接口里面定义一个工资变量,
eclipse里面可以自动帮你添加侍燃州相关变量的getterhe
setter方法的。