问答题

本题主窗口中包括一个文本框和一个文本域,在上面的文本框中输入一个整数并按回车键,就会在下面的文本域中显示该整数的平方值;如果在文本框中输入的不是一个整数,将弹出一个警告窗口。
import java.awt.event.*;
import java.awt.*;
import javax.swing.JOptionPane;
class Dwindow extends Frame implements ActionListener
TextField inputNumber;
TextArea show;
Dwindow(String s)
super(s);
inputNumber=new TextField(22);
inputNumber.addActionListener(this);
show=new TextAtea();
add(inputNumber,BorderLayout.NORTH);
add(show,BorderLayout.CENTER);
setBounds(60,60,300,300);setVisible(true);
validate();
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System.exit(0);

);

public void aetionPerformed(ActionEvent e)
boolean boo=false;
if(e.getSource()==inputNumber)
String s=______;
char a[]=s.toCharArray();
for(int i=0;i<a.length;i++)
if(!(Character.isDigit(a[i])))
boo=true;

if(boo==true)
JOptionPane.showMessageDialog(this,"您输入了非法字符","警告对话框",
______);
inputNumber.setText(null);

else if(boo==false)
int number=Integer.parseInt(s);
show.append("\n"+number+"平方:"+(number*number));




public class java2
public static void main(String args[])
new Dwindow("java2");

【参考答案】

第1处:inputNumber.getText()
第2处:JOptionPane.WARNING_MESSAGE
热门 试题

问答题
本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数。 import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class java3 extends JFrame public static JTextPane textPane; public static JScrollPane scrollPane; JPanel panel; public java3() super( java3() ); panel=new JPanel(); panel.setLayout(new BorderLayout()); panel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); textPane=new JTextPane(); textPane.setFont(new Font( monospaced ,Font.PLAIN,12)); scrollPane=new JScrollPane(textPane); panel.add(scrollPane); scrollPane.setPreferredsize(new Dimension(300,250)); setContentPane(panel); setCloseOperation(JFrame.EXIT_ON_CLOSE); LineNumber lineNumber=new LineNumber(); scrollPane.setRowHeaderView(lineNumber); public static void main(String[]args) java3 ttp=new java3(); ttp.pack(); ttp.setVisible(true); class LineNumber extends JTextPane private final static Color DEFAULT_BACKGROUND=Color.gray; private final static Color DEFAULT_FOREGROUND=Color.black; private final static Font DEFAULT_FONT=new Font( monospaced ,Font.PLAIN,12); private final static int HEIGHT=Integer.MAX_VALUE-1000000; private final static int MARGIN=5; private FontMetrics fontMetrics; private int lineHeight; private int currentRowWidth; private JComponent component; private int componentFontHeight; private int componentFontAscent; public LineNumber(JComponent component) if(component==null) setBackground(DEFAULT_BACKGROUND); setForeground(DEFAULT_FOREGROUND); setFont(DEFAULT_FONT); this.component=this; else setBackground(DEFAULT_BACKGROUND); setForeground(component.getForeground()); setFont(component.getFont()); this.component=component; componentFontHeight=component.getFontMetrics(component.getFont()).getHeight(); componentFontAscent=component.getFontMetrics(component.getFont()).getAscent(); setPreferredWidth(9999); public void setPreferredWidth(int row) int width=fontMetrics.stringWidth(String.valueOf(row)); if(currentRowWidth<width) currentRowWidth=width; setPreferredSize(new Dimension(2*MARGIN+width,HEIGHT)); public void setFont(Font font) super.setFont(font); fontMetrics=getFontMetrics(getFont()); public int getLineHeight() if(lineHeight==0) return componentFontHeight; else return lineHeight; public void setLineHeight(int lineHeight) if(lineHeight>0) this.lineHeight=lineHeight; public int getStartOffset() return component.getInsets().top+componentFontAscent; public void paintComponent(Graphics g) int lineHeight=getLinerteight(); int startOffset=getStartOffset(); Rectangle drawHere=g.getClipBounds(); g.setColor(getBackground()); g.fillRect(drawHere.x,drawHere.y,drawHere.width.drawHere.height); g.setColor(getForeground()); int startLineNumber=(drawHere.y lineHeight)+1; int endLineNumber=startLineNumber+(drawHere.height lineHeight); int start=(drawHere.y lineHeight)*lineHeight+startOffset; for(int i=startLineNumber;i<=endLineNumber;i++) String lineNumber=String.valueOf(i); int width=fontMetrics.stringWidth(lineNumber); g.drawstring(lineNumber,MARGIN+currentRowWidth-width,start); start+=lineHeight; setPreferredWidth(endLineNumber);