我在APPLET中要画个饼图,用SWING做了三个文本框和按扭。可是在载入的时候,只显示出用PAINT画出来的图形,而文本框和按扭不显示,请问需要怎么写才能同时显示呢?
以下是代码:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class TestApp extends Applet {
private int jd = 90;
boolean flage = false;
private int Angle1;
private int Angle2;
private int Angle3; private boolean isStandalone = false;
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JTextField jTextField3 = new JTextField();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JButton jButton1 = new JButton();
private JLabel jLabel4 = new JLabel();
private XYLayout xYLayout1 = new XYLayout(); //Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public TestApp() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setSize(600,400);
this.setLayout(xYLayout1);
jLabel1.setText("P1:");
jLabel2.setText("P2:");
jLabel3.setText("P3:");
jButton1.setText("生成饼图");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.add(jTextField1, new XYConstraints(58, 18, 114, 26));
this.add(jTextField3, new XYConstraints(424, 13, 126, 28));
this.add(jTextField2, new XYConstraints(236, 16, 127, 26));
this.add(jLabel3, new XYConstraints(374, 15, 45, 26));
this.add(jLabel1, new XYConstraints(13, 18, 42, 25));
this.add(jLabel2, new XYConstraints(183, 18, 51, 24));
this.add(jButton1, new XYConstraints(58, 52, 100, -1));
this.add(jLabel4, new XYConstraints(199, 55, 359, 38));
this.start();
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
} void jButton1_actionPerformed(ActionEvent e) { int p1 = Integer.parseInt(jTextField1.getText());
int p2 = Integer.parseInt(jTextField2.getText());
int p3 = Integer.parseInt(jTextField3.getText()); jLabel4.setText(Integer.toString(p1+p2+p3));
if(!Integer.toString(p1+p2+p3).equals("100"))
{
JOptionPane.showMessageDialog(null,"p1,p2,p3之和要求为100!","信息提示!",JOptionPane.ERROR_MESSAGE);
}
else
{
this.Angle1 = 360 * p1/100;
this.Angle2 = 360 * p2/100;
this.Angle3 = 360 * p3/100;
this.flage = true;
//JOptionPane.showMessageDialog(null,"100!","信息提示!",JOptionPane.ERROR_MESSAGE);
//jLabel4.setText(Integer.toString(this.Angle1)+"***"+Integer.toString(this.Angle2)+"***"+Integer.toString(this.Angle3));
repaint(); }
} public void paint(Graphics g) {
if(flage)
{
g.setColor(Color.red);
g.fillArc(200, 200, 200, 200, 0,this.Angle1);
g.setColor(Color.blue);
g.fillArc(200, 200, 200, 200, this.Angle1,this.Angle2);
g.setColor(Color.green);
g.fillArc(200, 200, 200, 200, this.Angle2,this.Angle3);
}
else
{
g.setColor(Color.red);
g.fillArc(200, 200, 200, 200, 0,this.jd);
} }
}
以下是代码:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class TestApp extends Applet {
private int jd = 90;
boolean flage = false;
private int Angle1;
private int Angle2;
private int Angle3; private boolean isStandalone = false;
private JTextField jTextField1 = new JTextField();
private JTextField jTextField2 = new JTextField();
private JTextField jTextField3 = new JTextField();
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JButton jButton1 = new JButton();
private JLabel jLabel4 = new JLabel();
private XYLayout xYLayout1 = new XYLayout(); //Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public TestApp() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
this.setSize(600,400);
this.setLayout(xYLayout1);
jLabel1.setText("P1:");
jLabel2.setText("P2:");
jLabel3.setText("P3:");
jButton1.setText("生成饼图");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.add(jTextField1, new XYConstraints(58, 18, 114, 26));
this.add(jTextField3, new XYConstraints(424, 13, 126, 28));
this.add(jTextField2, new XYConstraints(236, 16, 127, 26));
this.add(jLabel3, new XYConstraints(374, 15, 45, 26));
this.add(jLabel1, new XYConstraints(13, 18, 42, 25));
this.add(jLabel2, new XYConstraints(183, 18, 51, 24));
this.add(jButton1, new XYConstraints(58, 52, 100, -1));
this.add(jLabel4, new XYConstraints(199, 55, 359, 38));
this.start();
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
} void jButton1_actionPerformed(ActionEvent e) { int p1 = Integer.parseInt(jTextField1.getText());
int p2 = Integer.parseInt(jTextField2.getText());
int p3 = Integer.parseInt(jTextField3.getText()); jLabel4.setText(Integer.toString(p1+p2+p3));
if(!Integer.toString(p1+p2+p3).equals("100"))
{
JOptionPane.showMessageDialog(null,"p1,p2,p3之和要求为100!","信息提示!",JOptionPane.ERROR_MESSAGE);
}
else
{
this.Angle1 = 360 * p1/100;
this.Angle2 = 360 * p2/100;
this.Angle3 = 360 * p3/100;
this.flage = true;
//JOptionPane.showMessageDialog(null,"100!","信息提示!",JOptionPane.ERROR_MESSAGE);
//jLabel4.setText(Integer.toString(this.Angle1)+"***"+Integer.toString(this.Angle2)+"***"+Integer.toString(this.Angle3));
repaint(); }
} public void paint(Graphics g) {
if(flage)
{
g.setColor(Color.red);
g.fillArc(200, 200, 200, 200, 0,this.Angle1);
g.setColor(Color.blue);
g.fillArc(200, 200, 200, 200, this.Angle1,this.Angle2);
g.setColor(Color.green);
g.fillArc(200, 200, 200, 200, this.Angle2,this.Angle3);
}
else
{
g.setColor(Color.red);
g.fillArc(200, 200, 200, 200, 0,this.jd);
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货