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

java - JTabbedPane中的JButton-LMLPHP


我想要一个在单击任一按钮时也将使用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);

        }
    }


谢谢!

最佳答案

你可以通过使用

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
}


ImagePanel.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
}

关于java - JTabbedPane中的JButton,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15847156/

10-10 15:20