当前位置: 技术问答>java相关
高手哪儿去了?回答一个菜鸟的问题行不行?
来源: 互联网 发布时间:2015-01-30
本文导语: 我做了一个用JSlider滑条(滑条范围从100到200)控制半径(初始值为150)画一个圆,圆和滑条中间是JTextArea,说明半径,直径,面积和周长的值。而这些值也要随滑条移动而变化。 我的问题是: 1.当我试图在event han...
我做了一个用JSlider滑条(滑条范围从100到200)控制半径(初始值为150)画一个圆,圆和滑条中间是JTextArea,说明半径,直径,面积和周长的值。而这些值也要随滑条移动而变化。
我的问题是:
1.当我试图在event handle 中加入textArea display时,该语句在compile过程出现错误信息:
local variable display is accessed from within inner class; needs to be declared final
setText(java.lang.Swing)in javax.swing.text.JTextComponent can not be applied to (int)
2.textArea 部分用BorderLayout.CENTER,显示太长。无法调整显示高度。
3.我用鼠标关闭程序窗口后,ms-dos不能正常回复到光标显示,不能输入命令。一定要按热启动关闭java后才正常。怎么办?
我问了好多人,都没能解决以上问题。先谢谢您的指导了。
以下是程序部分:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Circle extends JFrame {
private OvalPanel myPanel;
private JTextArea display;
private JSlider radiusSlider;
public Circle()
{
super( "Circle" );
myPanel = new OvalPanel();
myPanel.setBackground(Color.pink);
JTextArea display = new JTextArea( 5, 30 );
display.setText( "The Radius is: " + myPanel.getRadius() +
"nThe Diameter is: " + myPanel.getDiameter()
+ "nThe Area is: " + myPanel.getArea() +
"nThe Circumference is: " +
myPanel.getCircumference() );
radiusSlider= new JSlider (SwingConstants.HORIZONTAL,100,200,10);
radiusSlider.setMajorTickSpacing(10);
radiusSlider.setPaintTicks(true);
radiusSlider.addChangeListener(
new ChangeListener() {
public void stateChanged (ChangeEvent e)
{
myPanel.setRadius(radiusSlider.getValue());
display.setText(radiusSlider.getValue());
repaint();
}
}
);
getContentPane().add(myPanel, BorderLayout.NORTH );
getContentPane().add(display, BorderLayout.CENTER);
getContentPane().add(radiusSlider,BorderLayout.SOUTH);
setSize( 210, 500 );
show();
}
class OvalPanel extends JPanel {
private int radius=150;
public void paintComponent( Graphics g )
{ g.drawOval( 0, 0, radius, radius ); }
public void setRadius(int r)
{
radius = ( r>=0 ? r:150);
}
public Dimension getPreferredSize()
{
return new Dimension (200,200);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public int getDiameter() { return ( 2 * radius ); }
public int getCircumference()
{ return ( int )( 2 * Math.PI * radius ); }
public int getArea()
{ return ( int )( radius * radius * Math.PI ); }
public int getRadius() { return radius; }
}
public static void main( String args[] )
{
Circle app = new Circle();
app.addWindowListener(
new WindowAdapter() {
public void WindowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
我的问题是:
1.当我试图在event handle 中加入textArea display时,该语句在compile过程出现错误信息:
local variable display is accessed from within inner class; needs to be declared final
setText(java.lang.Swing)in javax.swing.text.JTextComponent can not be applied to (int)
2.textArea 部分用BorderLayout.CENTER,显示太长。无法调整显示高度。
3.我用鼠标关闭程序窗口后,ms-dos不能正常回复到光标显示,不能输入命令。一定要按热启动关闭java后才正常。怎么办?
我问了好多人,都没能解决以上问题。先谢谢您的指导了。
以下是程序部分:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Circle extends JFrame {
private OvalPanel myPanel;
private JTextArea display;
private JSlider radiusSlider;
public Circle()
{
super( "Circle" );
myPanel = new OvalPanel();
myPanel.setBackground(Color.pink);
JTextArea display = new JTextArea( 5, 30 );
display.setText( "The Radius is: " + myPanel.getRadius() +
"nThe Diameter is: " + myPanel.getDiameter()
+ "nThe Area is: " + myPanel.getArea() +
"nThe Circumference is: " +
myPanel.getCircumference() );
radiusSlider= new JSlider (SwingConstants.HORIZONTAL,100,200,10);
radiusSlider.setMajorTickSpacing(10);
radiusSlider.setPaintTicks(true);
radiusSlider.addChangeListener(
new ChangeListener() {
public void stateChanged (ChangeEvent e)
{
myPanel.setRadius(radiusSlider.getValue());
display.setText(radiusSlider.getValue());
repaint();
}
}
);
getContentPane().add(myPanel, BorderLayout.NORTH );
getContentPane().add(display, BorderLayout.CENTER);
getContentPane().add(radiusSlider,BorderLayout.SOUTH);
setSize( 210, 500 );
show();
}
class OvalPanel extends JPanel {
private int radius=150;
public void paintComponent( Graphics g )
{ g.drawOval( 0, 0, radius, radius ); }
public void setRadius(int r)
{
radius = ( r>=0 ? r:150);
}
public Dimension getPreferredSize()
{
return new Dimension (200,200);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public int getDiameter() { return ( 2 * radius ); }
public int getCircumference()
{ return ( int )( 2 * Math.PI * radius ); }
public int getArea()
{ return ( int )( radius * radius * Math.PI ); }
public int getRadius() { return radius; }
}
public static void main( String args[] )
{
Circle app = new Circle();
app.addWindowListener(
new WindowAdapter() {
public void WindowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
|
3. ctrl + c 即可
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Circle extends JFrame {
private OvalPanel myPanel;
private JTextArea display;
private JSlider radiusSlider;
public Circle()
{
super( "Circle" );
myPanel = new OvalPanel();
myPanel.setBackground(Color.pink);
display = new JTextArea( 5, 30 );
display.setText( "The Radius is: " + myPanel.getRadius() +
"nThe Diameter is: " + myPanel.getDiameter()
+ "nThe Area is: " + myPanel.getArea() +
"nThe Circumference is: " +
myPanel.getCircumference() );
radiusSlider= new JSlider (SwingConstants.HORIZONTAL,100,200,10);
radiusSlider.setMajorTickSpacing(10);
radiusSlider.setPaintTicks(true);
radiusSlider.addChangeListener(
new ChangeListener() {
public void stateChanged (ChangeEvent e)
{
myPanel.setRadius(radiusSlider.getValue());
display.setText(Integer.toString(radiusSlider.getValue()));
repaint();
}
}
);
getContentPane().add(myPanel, BorderLayout.CENTER );
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(display, BorderLayout.NORTH);
p.add(radiusSlider,BorderLayout.SOUTH);
getContentPane().add(p,BorderLayout.SOUTH);
setSize( 210, 500 );
show();
}
class OvalPanel extends JPanel {
private int radius=150;
public void paintComponent( Graphics g )
{ g.drawOval( 0, 0, radius, radius ); }
public void setRadius(int r)
{
radius = ( r>=0 ? r:150);
}
public Dimension getPreferredSize()
{
return new Dimension (200,200);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public int getDiameter() { return ( 2 * radius ); }
public int getCircumference()
{ return ( int )( 2 * Math.PI * radius ); }
public int getArea()
{ return ( int )( radius * radius * Math.PI ); }
public int getRadius() { return radius; }
}
public static void main( String args[] )
{
Circle app = new Circle();
app.addWindowListener(
new WindowAdapter() {
public void WindowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Circle extends JFrame {
private OvalPanel myPanel;
private JTextArea display;
private JSlider radiusSlider;
public Circle()
{
super( "Circle" );
myPanel = new OvalPanel();
myPanel.setBackground(Color.pink);
display = new JTextArea( 5, 30 );
display.setText( "The Radius is: " + myPanel.getRadius() +
"nThe Diameter is: " + myPanel.getDiameter()
+ "nThe Area is: " + myPanel.getArea() +
"nThe Circumference is: " +
myPanel.getCircumference() );
radiusSlider= new JSlider (SwingConstants.HORIZONTAL,100,200,10);
radiusSlider.setMajorTickSpacing(10);
radiusSlider.setPaintTicks(true);
radiusSlider.addChangeListener(
new ChangeListener() {
public void stateChanged (ChangeEvent e)
{
myPanel.setRadius(radiusSlider.getValue());
display.setText(Integer.toString(radiusSlider.getValue()));
repaint();
}
}
);
getContentPane().add(myPanel, BorderLayout.CENTER );
Panel p = new Panel();
p.setLayout(new BorderLayout());
p.add(display, BorderLayout.NORTH);
p.add(radiusSlider,BorderLayout.SOUTH);
getContentPane().add(p,BorderLayout.SOUTH);
setSize( 210, 500 );
show();
}
class OvalPanel extends JPanel {
private int radius=150;
public void paintComponent( Graphics g )
{ g.drawOval( 0, 0, radius, radius ); }
public void setRadius(int r)
{
radius = ( r>=0 ? r:150);
}
public Dimension getPreferredSize()
{
return new Dimension (200,200);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public int getDiameter() { return ( 2 * radius ); }
public int getCircumference()
{ return ( int )( 2 * Math.PI * radius ); }
public int getArea()
{ return ( int )( radius * radius * Math.PI ); }
public int getRadius() { return radius; }
}
public static void main( String args[] )
{
Circle app = new Circle();
app.addWindowListener(
new WindowAdapter() {
public void WindowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
|
这个分好混,别忘记给分哦???
public void windowClosing(WindowEvent e) 里面的"windowClosing"不是大写的W
public void windowClosing(WindowEvent e) 里面的"windowClosing"不是大写的W