Так что, если я добавлю фоновое изображение, то я нажимаю кнопку много раз, моя программа будет работать все медленнее и медленнее. Но если я не добавлю эту фоновую картинку, она не станет медленнее. Кто-нибудь может сказать мне, почему? Спасибо! вот мой класс JFrame:
public class test extends JFrame implements ActionListener {
private Container contentPane;
public test() throws FileNotFoundException {
// set the position of the GUI related with screen size
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
this.setBounds((int) width / 4, (int) height / 4, (int) width / 2, (int) height / 2);
//set background
ImageIcon img = new ImageIcon("background.jpeg");
// get the container
contentPane = this.getContentPane();
//contentPane.setLayout(new GridLayout(3, 1));
/*
*background panel
*/
JPanel backgroundPanel = new JPanel(){
{
this.setOpaque(false);
}
public void paintComponent(Graphics g) {
img.setImage(img.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_AREA_AVERAGING));
g.drawImage(img.getImage(), 0, 0, this);
super.paintComponent(g);
}
};
contentPane.add(backgroundPanel);
backgroundPanel.setLayout(new GridLayout(3, 1));
/*
* second area
*/
JPanel secondPanel = new JPanel();
secondPanel.setOpaque(false);
// display button
makeButton(secondPanel, "Display", this);
backgroundPanel.add(secondPanel);
}
private void makeButton(JPanel p, String name, ActionListener target) {
JButton b = new JButton(name);
b.setFont(new Font(null, Font.PLAIN, 20));
// add it to the specified JPanel and button group and make the JPanel listen
p.add(b);
b.addActionListener(target);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String command = e.getActionCommand();
// Load button
// Display button
if (command.equals("Display")) {
// check is there any document has been loaded
// pop-up window
JOptionPane.showMessageDialog(null, "Please load a document first!", "Error",JOptionPane.PLAIN_MESSAGE);
}
// show states button
}
}
вот основной метод:
public class DocumentViewer {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
JFrame frm = new test();
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
}