
请各位懂Java的帮我看看这个小程序,挺简单的!
题目要求如图所示,窗体的程序我也设计好了:importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publ...
题目要求如图所示,窗体的程序我也设计好了:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUIProgram extends JFrame
{
public static void main(String[] args)
{
JFrame f=new JFrame("testWindow");
f.setSize(200,100);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setSize(200,100);
p.setBackground(Color.green);
f.setContentPane(p);
JButton b=new JButton("red");
b.addActionListener(new ButtonHandler());
p.add(b);
f.setVisible(true);
}
}
class ButtonHandler extends GUIProgram implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
只是不知道如何加上事件处理的部分?有谁懂的,请把改程序的全部代码帮忙写下。
有谁懂的,请把改程序的全部代码帮忙写下。 展开
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUIProgram extends JFrame
{
public static void main(String[] args)
{
JFrame f=new JFrame("testWindow");
f.setSize(200,100);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setSize(200,100);
p.setBackground(Color.green);
f.setContentPane(p);
JButton b=new JButton("red");
b.addActionListener(new ButtonHandler());
p.add(b);
f.setVisible(true);
}
}
class ButtonHandler extends GUIProgram implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
只是不知道如何加上事件处理的部分?有谁懂的,请把改程序的全部代码帮忙写下。
有谁懂的,请把改程序的全部代码帮忙写下。 展开
4个回答
展开全部
哥帮你修改下,你的部分代码删除掉了
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUIProgram {
public static void main(String[] args) {
JFrame f = new JFrame("testWindow");
f.setSize(200, 100);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setSize(200, 100);
p.setBackground(Color.green);
f.setContentPane(p);
JButton b = new JButton("red");
b.addActionListener(new ButtonHandler(p));
p.add(b);
f.setVisible(true);
}
}
class ButtonHandler implements ActionListener {
private JPanel p = null;
public ButtonHandler(JPanel p) {
this.p = p;
}
public void actionPerformed(ActionEvent e) {
p.setBackground(Color.red);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GUIProgram {
public static void main(String[] args) {
JFrame f = new JFrame("testWindow");
f.setSize(200, 100);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.setSize(200, 100);
p.setBackground(Color.green);
f.setContentPane(p);
JButton b = new JButton("red");
b.addActionListener(new ButtonHandler(p));
p.add(b);
f.setVisible(true);
}
}
class ButtonHandler implements ActionListener {
private JPanel p = null;
public ButtonHandler(JPanel p) {
this.p = p;
}
public void actionPerformed(ActionEvent e) {
p.setBackground(Color.red);
}
}
追问
private JPanel p = null;
public ButtonHandler(JPanel p) {
this.p = p;
}
大哥,我是Java新手,你能注释一下这几行吗?
追答
这个是把你那个GUI对象里面的参数传递到listern里面来,好让你在actionPerformed里面处理
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class GUIProgram extends JFrame
{
public static void main(String[] args)
{
JFrame f=new JFrame("testWindow");
f.setSize(200,100);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel p=new JPanel();
p.setSize(200,100);
p.setBackground(Color.green);
f.setContentPane(p);
JButton b=new JButton("red");
b.addActionListener(new ActionListener(){
//这里用了匿名内部类 这是添加监听事件最好的办法~~
public void actionPerformed(ActionEvent e)
{
p.setBackground(Color.red);
}
});
p.add(b);
f.setVisible(true);
}
}
import javax.swing.*;
import java.awt.event.*;
class GUIProgram extends JFrame
{
public static void main(String[] args)
{
JFrame f=new JFrame("testWindow");
f.setSize(200,100);
f.setLayout(new FlowLayout(FlowLayout.CENTER));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel p=new JPanel();
p.setSize(200,100);
p.setBackground(Color.green);
f.setContentPane(p);
JButton b=new JButton("red");
b.addActionListener(new ActionListener(){
//这里用了匿名内部类 这是添加监听事件最好的办法~~
public void actionPerformed(ActionEvent e)
{
p.setBackground(Color.red);
}
});
p.add(b);
f.setVisible(true);
}
}
追问
b.addActionListener(new ActionListener(){
//这里用了匿名内部类 这是添加监听事件最好的办法~~
public void actionPerformed(ActionEvent e)
{
p.setBackground(Color.red);
}
});
大哥,我是Java新手,你能注释一下这几行吗?
追答
.不是写在上边了 这是匿名内部类
具体意思你的去看内部类的部分.这不是几句话能说的清楚的
但是 如果想学gui的话 这个使用的必须会的.....
p.setBackground(Color.red);
这一行的话对应了你上边的
p.setBackground(Color.green);
这句就是设定背景颜色
你可以随便设Color.xxx
xxx就是颜色的英文.你随便想该什么
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if( e.getSource() instanceof JButton ){
if( "red".equals( ((JButton)e.getSource()).getText() ) ){
//按钮点击要做的事情
}
}
写得有点啰嗦,但有杀错、冇放过。
if( "red".equals( ((JButton)e.getSource()).getText() ) ){
//按钮点击要做的事情
}
}
写得有点啰嗦,但有杀错、冇放过。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
//import java.awt.event.*;
public class GUIProgram extends JFrame implements ActionListener{
JPanel p;
JButton b;
public GUIProgram(){
setTitle("testWindow");//设置窗体标题
// JFrame f=new JFrame("testWindow");//设置窗体标题
this.setLayout(new FlowLayout(FlowLayout.CENTER));//设置布局
p=new JPanel();//实例化JPanel
p.setSize(200,100);
p.setBackground(Color.green);//设置JPanel背景
this.setContentPane(p);
b=new JButton("red");
p.add(b);
this.setSize(200,100);//设置窗体大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体
this.setVisible(true);
b.addActionListener(this);//
}
public static void main(String[] args){
new GUIProgram();
}
@Override
public void actionPerformed(ActionEvent e) {//事件监听器
if(e.getSource() == b){
p.setBackground(Color.red);
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
//import java.awt.event.*;
public class GUIProgram extends JFrame implements ActionListener{
JPanel p;
JButton b;
public GUIProgram(){
setTitle("testWindow");//设置窗体标题
// JFrame f=new JFrame("testWindow");//设置窗体标题
this.setLayout(new FlowLayout(FlowLayout.CENTER));//设置布局
p=new JPanel();//实例化JPanel
p.setSize(200,100);
p.setBackground(Color.green);//设置JPanel背景
this.setContentPane(p);
b=new JButton("red");
p.add(b);
this.setSize(200,100);//设置窗体大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体
this.setVisible(true);
b.addActionListener(this);//
}
public static void main(String[] args){
new GUIProgram();
}
@Override
public void actionPerformed(ActionEvent e) {//事件监听器
if(e.getSource() == b){
p.setBackground(Color.red);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询