问题描述
- java中怎样设置图片的大小
-
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class ButtonFrame extends JFrame
{
private JButton plainJButton;//button with just text
private JButton fancyJButton;//button with icons//ButtonFrame adds JButtons to JFrame public ButtonFrame() { super("Testing Buttons"); setLayout(new FlowLayout());//set frame layout plainJButton=new JButton("Plain Button");//button with text add(plainJButton);//add plainJButton to JFrame Icon bug1=new ImageIcon(getClass().getResource("t013971ecacef25bf25.jpg")); Icon bug2=new ImageIcon(getClass().getResource("t019bbb4841bb28712f.jpg")); fancyJButton=new JButton("Fancy Button",bug1);//set image fancyJButton.setRolloverIcon(bug2); add(fancyJButton);//add fancyJButton to JFrame //create new ButtonHandler for button event handling ButtonHandler handler=new ButtonHandler(); fancyJButton.addActionListener(handler); plainJButton.addActionListener(handler); }//end buttonFrame constructor //inner class for button event handling private class ButtonHandler implements ActionListener { //handle button event public void actionPerformed(ActionEvent event) { JOptionPane.showMessageDialog(ButtonFrame.this, String.format("You pressed:%s", event.getActionCommand())); }//end method actionPerformed }//end private inner class buttonHandler
}//end class ButtonFrame
根据书上的代码加入了图片,但是图片总是太大,不满足我的需求,请问怎样或者说用什么函数才能调整图片的大小呢?请大神指教,帮我修改一下上面的代码
时间: 2025-01-20 07:41:06