尝试制作这个简单的小程序 - 第一部分只是创建一个简单的gui,用户选择数字,形状和颜色......然后当他们提交它加载另一个小程序时,它接收该信息并创建许多形状那种颜色。很简单,除了它没有工作 - 第二个小程序只是空白。不是网站的一部分,只是试图让它在netbeans中工作。
创建gui的第一部分
public class NewJApplet extends javax.swing.JApplet {
/** Initializes the applet NewJApplet */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();
jComboBox3 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jLabel1.setText("Number of Figures");
jLabel2.setText("Type of Figure");
jLabel3.setText("Color of Figure");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "4", "8", "16" }));
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Circle", "Oval", "Rectangle", "Square" }));
jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Red", "Blue", "Green", "Yellow", "Pink", "Black", "Cyan", "Magenta" }));
jButton1.setText("Draw");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(184, 184, 184)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(55, 55, 55)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))))
.addContainerGap(192, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(54, 54, 54)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(54, 54, 54)
.addComponent(jButton1)
.addContainerGap(289, Short.MAX_VALUE))
);
}// </editor-fold>
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new DrawApplet().setVisible(true);
setVisible(false);
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
public static javax.swing.JComboBox jComboBox1;
public static javax.swing.JComboBox jComboBox2;
public static javax.swing.JComboBox jComboBox3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
// End of variables declaration
}
应该做绘图的第二部分......
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.io.*;
/**
*
* @author Jason
*/
public class DrawApplet extends Applet {
public void paint(Graphics g) {
int size = 200;
int i = 0;
int x;
int y;
int width;
int height;
int shapeInt;
String number = (String)NewJApplet.jComboBox1.getSelectedItem();
int numberInt = Integer.parseInt(number);
String shape = (String)NewJApplet.jComboBox2.getSelectedItem();
String color = (String)NewJApplet.jComboBox3.getSelectedItem();
for (i = 0; i < numberInt; i++) {
if (color.equals("red")) {
g.setColor(Color.red);
} else if (color.equals("blue")) {
g.setColor(Color.blue);
} else if (color.equals("green")) {
g.setColor(Color.green);
} else if (color.equals("yello")) {
g.setColor(Color.yellow);
} else if (color.equals("pink")) {
g.setColor(Color.pink);
} else if (color.equals("black")) {
g.setColor(Color.black);
} else if (color.equals("cyan")) {
g.setColor(Color.cyan);
} else {
g.setColor(Color.magenta);
}
x = (int)(Math.random() * size);
y = (int)(Math.random() * size);
width = (int)(Math.random() * size);
height = (int)(Math.random() * size);
if (shape.equals("circle")) {
shapeInt = 1;
} else if (shape.equals("oval")) {
shapeInt = 2;
} else if (shape.equals("rectangle")) {
shapeInt = 3;
} else {
shapeInt = 4;
}
switch (shapeInt) {
case 1:
g.fillOval(x, y, width, height);
break;
case 2:
g.fillOval(x, y, width, height);
break;
case 3:
g.fillRect(x, y, width, height);
break;
case 4:
g.fillRect(x, y, width, height);
break;
}
}
}
}