1

我正在用 JTabbedPane 和 JButton 编写一个交互式程序。到目前为止,我已经做到了,所以当单击“预订航班”选项卡时,会显示此面板:

IMG

我想要一个在我单击任一按钮时也将使用 MouseListener 来显示的图像。那个代码会是什么样子?

这是我目前的方法:

显示图像:

protected JComponent makeImagePanel(String path) 
{
    try{
        JPanel panel = new JPanel(false);
        BufferedImage myPicture = ImageIO.read(new File(path));
        JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
        panel.add(picLabel);

        return panel;
    }catch (IOException e)
    {
        throw new IllegalArgumentException("Unable to load" + path);
    }

} 

和鼠标事件:

     protected JComponent FlightOptions()
   {
    JPanel panel= new JPanel(false);
    JButton international= new JButton("International Flights");
    JButton domestic= new JButton("Domestic Flights");
    international.setVerticalTextPosition (AbstractButton.CENTER);
    international.setHorizontalTextPosition(AbstractButton.LEADING);
    domestic.setVerticalTextPosition (AbstractButton.CENTER);
    domestic.setHorizontalTextPosition(AbstractButton.LEADING);
    international.setActionCommand("international");
    domestic.setActionCommand("domestic");
    international.setEnabled(true);
    MouseAdapter2 mouse= new MouseAdapter2();
    international.addMouseListener(mouse);
    domestic.addMouseListener(mouse);
    panel.setLayout(new GridLayout(1, 1));
    panel.add(international);
    panel.add(domestic);
    return panel;
}
class MouseAdapter2 extends MouseAdapter {

    public void mousePressed(MouseEvent e) {
        JButton buttonpressed = (JButton)e.getSource();
        if(buttonpressed==international)
        {
            reservation= makeImagePanel("international_1.gif");
            overview.addTab("International Flight", reservation);

        }
    }

谢谢!

4

1 回答 1

2

你可以通过使用

jButton2.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/newpackage/Water lilies.jpg")));

运行这段代码,看看它是如何工作的

更新 2

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Sample extends javax.swing.JFrame {
    public Sample() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel2.setLayout(new java.awt.GridLayout());

        jButton1.setText("jButton1");
        jButton1.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/newpackage/Blue hills.jpg"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);

        jButton2.setText("jButton1");
        jButton2.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/newpackage/Water lilies.jpg"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);

        jTabbedPane1.addTab("tab2", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    ImagePanel panel = new ImagePanel();
    // display the jpanel in a joptionpane dialog, using showMessageDialog
    JFrame frame = new JFrame("JOptionPane showMessageDialog component example");
    JOptionPane.showMessageDialog(frame, panel);
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    ImagePanel2 panel = new ImagePanel2();
    // display the jpanel in a joptionpane dialog, using showMessageDialog
    JFrame frame = new JFrame("JOptionPane showMessageDialog component example");
    JOptionPane.showMessageDialog(frame, panel);
    }

    public static void main(String args[]) {

        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Sample().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration
}

图像面板.java

public class ImagePanel extends javax.swing.JPanel {
    public ImagePanel() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/newpackage/Blue hills.jpg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 609, Short.MAX_VALUE)
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
}

ImagePanel2.java

public class ImagePanel2 extends javax.swing.JPanel {
    public ImagePanel2() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/newpackage/Water lilies.jpg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
}
于 2013-04-06T05:07:35.270 回答