java按钮动作
编一个程序实现点击按钮“a",面板上显示aaaaaaa,点击b,面板上显示bbbbbbb,下面是我编的代码,但是点击按钮后什么都显示不出来,请各位看一看,指点一下,并说明...
编一个程序实现点击按钮“a",面板上显示aaaaaaa,点击b,面板上显示bbbbbbb,下面是我编的代码,但是点击按钮后什么都显示不出来,请各位看一看,指点一下,并说明为什么我编的程序错在哪里?
package testbutton;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import javax.swing.*;
public class button {
public static void main(String[] args)
{
bframe frame=new bframe();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class bframe extends JFrame
{
public bframe()
{
setTitle("buttontest");
setSize(300,200);
bpanel=new JPanel();
Action a=new word("a","aaaaaaa");
Action b=new word("b","bbbbbbb");
bpanel.add(new JButton(a));
bpanel.add(new JButton(b));
add(bpanel);
ActionMap amap=bpanel.getActionMap();
amap.put("panel.a", a);
amap.put("panel.b", b);
}
public void addc(String n)
{
pr p=new pr(n);
add(p);
}
public class word extends AbstractAction
{
public word(String s,String n)
{
putValue(Action.NAME,s);
putValue("string",n);
}
public void actionPerformed(ActionEvent event)
{
String n=(String) getValue("string");
addc(n);
}
}
private JPanel bpanel;
}
class pr extends JComponent
{
public pr(String s)
{
string=s;
}
public void paintComponent(Graphics g)
{
g.drawString(string, 100, 100);
}
private String string;
} 展开
package testbutton;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import javax.swing.*;
public class button {
public static void main(String[] args)
{
bframe frame=new bframe();
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class bframe extends JFrame
{
public bframe()
{
setTitle("buttontest");
setSize(300,200);
bpanel=new JPanel();
Action a=new word("a","aaaaaaa");
Action b=new word("b","bbbbbbb");
bpanel.add(new JButton(a));
bpanel.add(new JButton(b));
add(bpanel);
ActionMap amap=bpanel.getActionMap();
amap.put("panel.a", a);
amap.put("panel.b", b);
}
public void addc(String n)
{
pr p=new pr(n);
add(p);
}
public class word extends AbstractAction
{
public word(String s,String n)
{
putValue(Action.NAME,s);
putValue("string",n);
}
public void actionPerformed(ActionEvent event)
{
String n=(String) getValue("string");
addc(n);
}
}
private JPanel bpanel;
}
class pr extends JComponent
{
public pr(String s)
{
string=s;
}
public void paintComponent(Graphics g)
{
g.drawString(string, 100, 100);
}
private String string;
} 展开
3个回答
展开全部
class pr extends JComponent
{
public pr(String s)
{
string=s;
}
这就是你的JComponent,运行后单击按钮没有显示,你不觉得这个类太简单了吗!
{
public pr(String s)
{
string=s;
}
这就是你的JComponent,运行后单击按钮没有显示,你不觉得这个类太简单了吗!
追问
这个类是
class pr extends JComponent
{
public pr(String s)
{
string=s;
}
public void paintComponent(Graphics g)
{
g.drawString(string, 100, 100);
}
private String string;
}
呀
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
学习了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询