用java创建一个带按钮对象的窗口的程序,帮我看看哪里错了?
我编写的程序如下:importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassJBext...
我编写的程序如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JB extends JFrame
{
private JButton button1,button2;
public JB(JFrame f,JPanel p){
f.add(p);
button1=new JButton("按钮1");
button1.setFont(new Font("Serif",Font.PLAIN,20));
ImageIcon img1=new ImageIcon("图片/1.gif");/*此处的图片都事先放在同一文件夹下了*/
ImageIcon img2=new ImageIcon("图片/2.gif");
button2=new JButton("按钮2",img2);
button2.setRolloverIcon(img1);
button2.setFont(new Font("Serif",Font.PLAIN,14));
BHandler h=new BHandler();
button1.addActionListener(h);
button2.addActionListener(h);
p.add(button1);
p.add(button2);
}
public static void main(String [] args){
JB f1=new JB(new JF0("添加按钮的窗口"),new JPanel());
}
protect class JF0 extends JFrame{
Container c;
protect JF0(){
super("图形用户界面"); //调用父类JFrame的构造方法,给窗口命名
setBounds(200,200,500,400);//设置窗口位置、窗口大小
setVisible(true); //设置窗口是否为可见
/* try{//设置外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}*/
c=getContentPane();
c.setBackground(Color.yellow);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
JF0 d = new JF0();
}
}
private class BHandler implements ActionListener
{
public void actionPerfomed(ActionEvent event){
JOptionPane.showMessageDialog(JB.this,"你按了:"+event.getActionCommand());
}
}
};
本人初学,请各位高手帮忙检查一下~~~ 展开
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JB extends JFrame
{
private JButton button1,button2;
public JB(JFrame f,JPanel p){
f.add(p);
button1=new JButton("按钮1");
button1.setFont(new Font("Serif",Font.PLAIN,20));
ImageIcon img1=new ImageIcon("图片/1.gif");/*此处的图片都事先放在同一文件夹下了*/
ImageIcon img2=new ImageIcon("图片/2.gif");
button2=new JButton("按钮2",img2);
button2.setRolloverIcon(img1);
button2.setFont(new Font("Serif",Font.PLAIN,14));
BHandler h=new BHandler();
button1.addActionListener(h);
button2.addActionListener(h);
p.add(button1);
p.add(button2);
}
public static void main(String [] args){
JB f1=new JB(new JF0("添加按钮的窗口"),new JPanel());
}
protect class JF0 extends JFrame{
Container c;
protect JF0(){
super("图形用户界面"); //调用父类JFrame的构造方法,给窗口命名
setBounds(200,200,500,400);//设置窗口位置、窗口大小
setVisible(true); //设置窗口是否为可见
/* try{//设置外观
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}*/
c=getContentPane();
c.setBackground(Color.yellow);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
JF0 d = new JF0();
}
}
private class BHandler implements ActionListener
{
public void actionPerfomed(ActionEvent event){
JOptionPane.showMessageDialog(JB.this,"你按了:"+event.getActionCommand());
}
}
};
本人初学,请各位高手帮忙检查一下~~~ 展开
2个回答
2009-06-09
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JB extends JFrame {
public JB() {
super("图形用户界面"); // 调用父类JFrame的构造方法,给窗口命名
setBounds(200, 200, 500, 400);// 设置窗口位置、窗口大小
/*
* try{//设置外观
* UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
* }catch(Exception e){}
*/
Container c = getContentPane();
c.add(new MyPanel()); //添加Panel
c.setBackground(Color.yellow);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // 设置窗口是否为可见
}
private class MyPanel extends JPanel {
private JButton button1, button2;
public MyPanel() {
button1 = new JButton("按钮1");
button1.setFont(new Font("Serif", Font.PLAIN, 20));
ImageIcon img1 = new ImageIcon("图片/1.gif");/* 此处的图片都事先放在同一文件夹下了 */
ImageIcon img2 = new ImageIcon("图片/2.gif");
button2 = new JButton("按钮2", img2);
button2.setRolloverIcon(img1);
button2.setFont(new Font("Serif", Font.PLAIN, 14));
BHandler h = new BHandler();
button1.addActionListener(h);
button2.addActionListener(h);
add(button1);
add(button2);
}
}
private class BHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(JB.this, "你按了:"
+ event.getActionCommand());
}
}
public static void main(String[] args) {
JB f1 = new JB();
}
}
import java.awt.*;
import java.awt.event.*;
public class JB extends JFrame {
public JB() {
super("图形用户界面"); // 调用父类JFrame的构造方法,给窗口命名
setBounds(200, 200, 500, 400);// 设置窗口位置、窗口大小
/*
* try{//设置外观
* UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
* }catch(Exception e){}
*/
Container c = getContentPane();
c.add(new MyPanel()); //添加Panel
c.setBackground(Color.yellow);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // 设置窗口是否为可见
}
private class MyPanel extends JPanel {
private JButton button1, button2;
public MyPanel() {
button1 = new JButton("按钮1");
button1.setFont(new Font("Serif", Font.PLAIN, 20));
ImageIcon img1 = new ImageIcon("图片/1.gif");/* 此处的图片都事先放在同一文件夹下了 */
ImageIcon img2 = new ImageIcon("图片/2.gif");
button2 = new JButton("按钮2", img2);
button2.setRolloverIcon(img1);
button2.setFont(new Font("Serif", Font.PLAIN, 14));
BHandler h = new BHandler();
button1.addActionListener(h);
button2.addActionListener(h);
add(button1);
add(button2);
}
}
private class BHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
JOptionPane.showMessageDialog(JB.this, "你按了:"
+ event.getActionCommand());
}
}
public static void main(String[] args) {
JB f1 = new JB();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询