我再试一次,大家帮忙看看以下的题目上,问题解决的话自当给分,我说了分不是问题
只要我能给得起行
×××××××××××××××××××××××××××××为什么编译能通过,却没有出现文本框让我输入数据?
而把public void paint(Graphics g)部分去掉的话就可以出现文本框了。
请高手帮我找出原因,谢谢!题目和我写的程序如下:
/*题目:编写方法squareOfAsterisks,显示一组由星号组成的正方形。正方形的边长由整型参数side确定。
例如side是4,该方法显示如下:
****
****
****
****
将此方法编入applet,该applet读入用户从键盘输入的一个表示side的整数值,并利用squareOfAsterisks
方法画图。注意:此方法应该由applet的paint方法调用,并接收来自paint的Graphics对象。*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Square extends JApplet implements ActionListener{
JLabel numLab;
JTextField numTxt;
private int x;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
numLab=new JLabel("Enter integer:");
c.add(numLab);
numTxt=new JTextField(10);
c.add(numTxt);
} //接收文本框的数据
public void actionPerformed(ActionEvent e)
{
x=Integer.parseInt(e.getActionCommand());
}
//在Applet上画图
public void paint(Graphics g)
{
g.drawString(squareOfAsterisks(x),25,25);
}
//打印正方形的方法
public String squareOfAsterisks(int side)
{
String output="";
for(int i=1;i<=side;i++)
{
for(int j=1;j<=side;j++)
output+="* ";
output+="\n";
}
return output;
}
}
只要我能给得起行
×××××××××××××××××××××××××××××为什么编译能通过,却没有出现文本框让我输入数据?
而把public void paint(Graphics g)部分去掉的话就可以出现文本框了。
请高手帮我找出原因,谢谢!题目和我写的程序如下:
/*题目:编写方法squareOfAsterisks,显示一组由星号组成的正方形。正方形的边长由整型参数side确定。
例如side是4,该方法显示如下:
****
****
****
****
将此方法编入applet,该applet读入用户从键盘输入的一个表示side的整数值,并利用squareOfAsterisks
方法画图。注意:此方法应该由applet的paint方法调用,并接收来自paint的Graphics对象。*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Square extends JApplet implements ActionListener{
JLabel numLab;
JTextField numTxt;
private int x;
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
numLab=new JLabel("Enter integer:");
c.add(numLab);
numTxt=new JTextField(10);
c.add(numTxt);
} //接收文本框的数据
public void actionPerformed(ActionEvent e)
{
x=Integer.parseInt(e.getActionCommand());
}
//在Applet上画图
public void paint(Graphics g)
{
g.drawString(squareOfAsterisks(x),25,25);
}
//打印正方形的方法
public String squareOfAsterisks(int side)
{
String output="";
for(int i=1;i<=side;i++)
{
for(int j=1;j<=side;j++)
output+="* ";
output+="\n";
}
return output;
}
}
只要不是太hello world的问题就可以,呵呵多多交流
有时候程序运行机制的原因并不是我们这些程序员能够解释的。我照着做一边,可以画出来,而你不行,只能说明你去抄的例子错了。解决方法就是找一个可以用的来照着做看看。当然,如果你要当研究人员,则另当别论。
我们现在在mainframe上面运行db2可遇到很多奇形怪状的问题,但是这些在linux,window都是跑得很好的。这些内核问题我们程序员怎么能很清楚?所以并不是别人不帮你,你看,我就是copy了你的,但是运行正确,我还能向你解释些什么呢?import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;/**
* Title:
* Description:
* Copyright: Copyright (c) 2003
* Company:
* @author
* @version 1.0
*/public class Square extends JApplet {
boolean isStandalone = false;
JLabel numLab;
JTextField numTxt;
private int x; /**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 Square() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception { this.setSize(new Dimension(400,300));
this.getContentPane().setLayout(null);
Container c=getContentPane();
c.setLayout(new FlowLayout()); numLab=new JLabel("Enter integer:");
c.add(numLab);
numTxt=new JTextField(10);
c.add(numTxt);
}
/**Start the applet*/
public void start() {
}
/**Stop the applet*/
public void stop() {
}
/**Destroy the applet*/
public void destroy() {
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
/**Main method*/
public static void main(String[] args) {
Square applet = new Square();
applet.isStandalone = true;
JFrame frame = new JFrame();
//EXIT_ON_CLOSE == 3
frame.setDefaultCloseOperation(3);
frame.setTitle("Applet Frame");
frame.getContentPane().add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
} //static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}
}
public void paint(Graphics g)
{
super.paint(g);
g.drawString(squareOfAsterisks(x),25,25);
}要调用父类的paint方法
1。即便没有分,如果你的问题有人会,一定有人愿意回答的!谁都是从一窍不通过来的,知 道在丈二和尚摸不着头脑时,能够得到别人指点一下该是多么的重要:)再说,真正的高手并不看重分数!
2。如果你有分,最好还是给点,哪怕是象征性的!以表示对他人劳动成果的尊重:)
3。如果有人的答案是你想要的结果,最好尽快揭贴!对你认为回答的好的给分。很多人都是只问问题不揭贴,拿到答案后就走人!如果大家都这样的话(或者别人知道问问题的是这样的人),那自然就没有人回答问题了:)所以,希望
高手忆苦思甜,体谅初学者渴望指点的心情,尽可能的给予帮助;
新手能够尊重别人的劳动,及时揭贴:)
大家共同进步!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class Square
extends JApplet
{
JLabel numLab; JTextField numTxt; private int x; public void init()
{
Container c = getContentPane();
c.setLayout(new FlowLayout()); numLab = new JLabel("Enter integer:");
c.add(numLab);
numTxt = new JTextField(10);
numTxt.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
input_keyPressed(e);
}
}); c.add(numTxt);
}
//这里才是接收文本框的数据
void input_keyPressed(KeyEvent e)
{
if (e.getKeyChar() == e.VK_ENTER)
{
x = Integer.parseInt(numTxt.getText());
repaint();
}
} public static void main(String[] args)
{
new Square();
}
public void paint(Graphics g)
{
super.paint(g);
drawSquare(g); }
//打印正方形的方法
int xPoint = 25, yPoint = 25; public void drawSquare(Graphics g)
{
String output = "";
for (int i = 1; i <= x; i++)
{
for (int j = 1; j <= x; j++)
output += "* ";
g.drawString(output, xPoint, yPoint);
yPoint += 20;
output = "";
}
}
}