java界面添加按钮
我只是想实现一个图片背景上有一个开始游戏的图片样式的按钮点击按钮进入下一个界面开始游戏我定义MyJPanel继承JPanel然后覆写paint方法添加背景在定义MyJBu...
我只是想实现一个图片背景上有一个开始游戏的图片样式的按钮 点击按钮 进入下一个界面 开始游戏
我定义MyJPanel继承JPanel 然后覆写paint方法 添加背景
在定义 MyJButton 继承JButton然后覆写paintComponent方法
背景可以添加成功 但是要显示按钮还要写一句frame.setLayout(null);
但是写上这一句 按钮可以显示出来 但是背景就又没有了
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrame18 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(0, 0, 300, 400);
frame.setTitle("飞翔大战");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyJPanel mp = new MyJPanel();
frame.add(mp);
// MyJButton button = new MyJButton();
//button.setBounds(100, 300, 100, 50);
//frame.add(button);
// MyJButton bp = new MyJButton();
// frame.add(bp);
// frame.setLayout(null);
frame.setVisible(true);
}
}
/*class MyJButton extends JButton{
public void paintComponent(Graphics f){
ImageIcon imgIcon = new ImageIcon("img/start.jpg");
Image img = imgIcon.getImage();
f.drawImage(img,0,0,100,50,null);
}
}*/
class MyJPanel extends JPanel{
public void paint(Graphics g){
super.paint(g);
ImageIcon imgIcon = new ImageIcon("img/bg6.jpg");
Image img = imgIcon.getImage();
g.drawImage(img,0,0,300,400,null);
}
} 展开
我定义MyJPanel继承JPanel 然后覆写paint方法 添加背景
在定义 MyJButton 继承JButton然后覆写paintComponent方法
背景可以添加成功 但是要显示按钮还要写一句frame.setLayout(null);
但是写上这一句 按钮可以显示出来 但是背景就又没有了
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrame18 {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(0, 0, 300, 400);
frame.setTitle("飞翔大战");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyJPanel mp = new MyJPanel();
frame.add(mp);
// MyJButton button = new MyJButton();
//button.setBounds(100, 300, 100, 50);
//frame.add(button);
// MyJButton bp = new MyJButton();
// frame.add(bp);
// frame.setLayout(null);
frame.setVisible(true);
}
}
/*class MyJButton extends JButton{
public void paintComponent(Graphics f){
ImageIcon imgIcon = new ImageIcon("img/start.jpg");
Image img = imgIcon.getImage();
f.drawImage(img,0,0,100,50,null);
}
}*/
class MyJPanel extends JPanel{
public void paint(Graphics g){
super.paint(g);
ImageIcon imgIcon = new ImageIcon("img/bg6.jpg");
Image img = imgIcon.getImage();
g.drawImage(img,0,0,300,400,null);
}
} 展开
2个回答
展开全部
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrame18
{
public static void main ( String[] args )
{
JFrame frame = new JFrame ();
frame.setBounds (0, 0, 300, 400);
frame.setTitle ("飞翔大战");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
MyJPanel mp = new MyJPanel ();
frame.add (mp);
MyJButton button = new MyJButton ();
button.setBounds (100, 300, 100, 50);
frame.add (button);
MyJPanel bp = new MyJPanel ();
frame.add (bp);
bp.setLayout (null);
frame.setVisible (true);
}
}
class MyJButton extends JButton
{
public void paintComponent ( Graphics f )
{
ImageIcon imgIcon = new ImageIcon ("img/start.jpg");
Image img = imgIcon.getImage ();
f.drawImage (img, 0, 0, 100, 50, null);
}
}
class MyJPanel extends JPanel
{
public void paint ( Graphics g )
{
super.paint (g);
ImageIcon imgIcon = new ImageIcon ("img/bg6.jpg");
Image img = imgIcon.getImage ();
g.drawImage (img, 0, 0, 300, 400, null);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询