如何在点击按钮边框油漆图象? [英] how to paint images on clicking button in frame?
本文介绍了如何在点击按钮边框油漆图象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做的。我架两个按钮,想知道如何在点击不同的按钮显示不同的图像?
有另一种方式,或我必须做出面板?我在初学阶段
包PRAC;
进口java.awt中的*。
java.awt.event中导入*。公共类B扩展框架实现的ActionListener {
弦乐味精;
按钮一,二;B()
{的setSize(1000,500);
调用setVisible(真);
的setLayout(新的FlowLayout(FlowLayout.LEFT));
1 =新按钮(1);
2 =新按钮(2); 加(之一);
加入(二); one.addActionListener(本);
two.addActionListener(本);}
公共无效的actionPerformed(ActionEvent的五)
{
味精= e.getActionCommand();
如果(msg.equals(1))
{
味精=pressed 1;
}
其他
味精=pressed 2;
重绘();
}公共无效漆(图形G)
{
g.drawString(味精,100,300);
}
公共静态无效的主要(一个String [])
{
新B();
}
}
解决方案
使用的JLabel
并更改图标按钮被点击时。
几点:
- 通话
调用setVisible(真)
在加入所有组件后结束。 - 使用
的JFrame#包()
方法,可以自动适应的组件基于组件的$ P $的JFrame
pferred而不是调用的JFrame#的setSize()
方法维度。
样code:
最后的JLabel JLabel的=新的JLabel();
添加(JLabel的);最终图像此搜索= ImageIO.read(新文件(资源/ 1.png));
最终图像IMAGE2 = ImageIO.read(新文件(资源/ 2.png));的JPanel面板=新JPanel();
一个JButton的jButton1 =的新的JButton(显示第一幅图像);
jbutton1.addActionListener(新的ActionListener(){ @覆盖
公共无效的actionPerformed(ActionEvent的为arg0){
jlabel.setIcon(新的ImageIcon(此搜索));
}
});一个JButton =将jButton2 JButton的新(显示第二图像);
jbutton2.addActionListener(新的ActionListener(){ @覆盖
公共无效的actionPerformed(ActionEvent的为arg0){
jlabel.setIcon(新的ImageIcon(IMAGE2));
}
});panel.add(的jButton1);
panel.add(将jButton2);
加(面板,BorderLayout.NORTH);
I've made two buttons in frame .I want to know how to display different images on clicking different buttons? is there another way out or i have to make panel?I am at beginner stage
package prac;
import java.awt.*;
import java.awt.event.*;
public class b extends Frame implements ActionListener{
String msg;
Button one,two;
b()
{ setSize(1000,500);
setVisible(true);
setLayout(new FlowLayout(FlowLayout.LEFT));
one=new Button("1");
two=new Button("2");
add(one);
add(two);
one.addActionListener(this);
two.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
msg=e.getActionCommand();
if(msg.equals("1"))
{
msg="Pressed 1";
}
else
msg="pressed 2";
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,100,300);
}
public static void main(String s[])
{
new b();
}
}
解决方案
Use JLabel
and change the icon when button is clicked.
Some points:
- call
setVisible(true)
in the end after adding all the component. - use
JFrame#pack()
method that automatically fit the components in theJFrame
based on component's preferred dimension instead of callingJFrame#setSize()
method.
sample code:
final JLabel jlabel = new JLabel();
add(jlabel);
final Image image1 = ImageIO.read(new File("resources/1.png"));
final Image image2 = ImageIO.read(new File("resources/2.png"));
JPanel panel = new JPanel();
JButton jbutton1 = new JButton("Show first image");
jbutton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jlabel.setIcon(new ImageIcon(image1));
}
});
JButton jbutton2 = new JButton("Show second image");
jbutton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
jlabel.setIcon(new ImageIcon(image2));
}
});
panel.add(jbutton1);
panel.add(jbutton2);
add(panel, BorderLayout.NORTH);
这篇关于如何在点击按钮边框油漆图象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文