java SWING小程序
importjava.awt.*;importjavax.swing.*;publicclassJPanelDemo{publicstaticvoidmain(Strin...
import java.awt.*;
import javax.swing.*;
public class JPanelDemo {
public static void main(String args[]){
JFrameDemoa jf=new JFrameDemoa();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class JFrameDemoa extends JFrame{
public JFrameDemoa() {
this.setTitle("nihao");
this.setBounds(100,100,200,200);
Jpanelscreena jll=new Jpanelscreena();
add(jll);
}
}
class Jpanelscreena extends JPanel{
private int width=20;
private int height=20;
public void paintComponnet(Graphics g){
super.paintComponent(g);
g.drawString("Java is money", 20, 20);
}
}
这个程序怎么不能在JFrameDemoa 中显示Java is money这句话,请说明理由,以及怎样改正 展开
import javax.swing.*;
public class JPanelDemo {
public static void main(String args[]){
JFrameDemoa jf=new JFrameDemoa();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class JFrameDemoa extends JFrame{
public JFrameDemoa() {
this.setTitle("nihao");
this.setBounds(100,100,200,200);
Jpanelscreena jll=new Jpanelscreena();
add(jll);
}
}
class Jpanelscreena extends JPanel{
private int width=20;
private int height=20;
public void paintComponnet(Graphics g){
super.paintComponent(g);
g.drawString("Java is money", 20, 20);
}
}
这个程序怎么不能在JFrameDemoa 中显示Java is money这句话,请说明理由,以及怎样改正 展开
展开全部
因为你把显示语句的方法体定义在了Jpanelscreena的有参构造器中,而你Jpanelscreena jll=new Jpanelscreena();这一句调用的确实无参构造器,这样的话,有参构造器当中的方法体当然不会被执行啦。改法如下:
import java.awt.*;
import javax.swing.*;
public class JPanelDemo {
public static void main(String args[]) {
JFrameDemoa jf = new JFrameDemoa();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class JFrameDemoa extends JFrame {
public JFrameDemoa() {
this.setTitle("nihao");
this.setBounds(100, 100, 200, 200);
Jpanelscreena jll = new Jpanelscreena();
add(jll);
}
}
class Jpanelscreena extends JPanel {
private int width = 20;
private int height = 20;
public void paintComponnet() {
}
public void paint(Graphics g) {
super.paintComponent(g);
g.drawString("Java is money", 20, 20);
}
}
记得给我分哦^_^
import java.awt.*;
import javax.swing.*;
public class JPanelDemo {
public static void main(String args[]) {
JFrameDemoa jf = new JFrameDemoa();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class JFrameDemoa extends JFrame {
public JFrameDemoa() {
this.setTitle("nihao");
this.setBounds(100, 100, 200, 200);
Jpanelscreena jll = new Jpanelscreena();
add(jll);
}
}
class Jpanelscreena extends JPanel {
private int width = 20;
private int height = 20;
public void paintComponnet() {
}
public void paint(Graphics g) {
super.paintComponent(g);
g.drawString("Java is money", 20, 20);
}
}
记得给我分哦^_^
展开全部
错误有2:
1.往JFrame添加东西的时候要先getContentPane().add(jll),否者某些时候会显示不出来。
2.你的方法名字写错了paintComponent(Graphics g)而不是paintComponnet(Graphics g),所以就会不显示字符串了,你这个方法没有重载,所以是不会有作用的。
import javax.swing.*;
public class JPanelDemo {
public static void main(String args[]){
JFrameDemoa jf=new JFrameDemoa();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class JFrameDemoa extends JFrame{
public JFrameDemoa() {
this.setTitle("nihao");
this.setBounds(100,100,200,200);
Jpanelscreena jll=new Jpanelscreena();
getContentPane().add(jll);
}
}
class Jpanelscreena extends JPanel{
private int width=20;
private int height=20;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("Java is money", 20, 20);
}
}
1.往JFrame添加东西的时候要先getContentPane().add(jll),否者某些时候会显示不出来。
2.你的方法名字写错了paintComponent(Graphics g)而不是paintComponnet(Graphics g),所以就会不显示字符串了,你这个方法没有重载,所以是不会有作用的。
import javax.swing.*;
public class JPanelDemo {
public static void main(String args[]){
JFrameDemoa jf=new JFrameDemoa();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
class JFrameDemoa extends JFrame{
public JFrameDemoa() {
this.setTitle("nihao");
this.setBounds(100,100,200,200);
Jpanelscreena jll=new Jpanelscreena();
getContentPane().add(jll);
}
}
class Jpanelscreena extends JPanel{
private int width=20;
private int height=20;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawString("Java is money", 20, 20);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询