如何显示带描述的图像

时间:2017-11-16 06:08:15

标签: java

我需要帮助的人。我有一个页面/框架显示第一个面板。第一个面板,它将按钮与数据库中的图像。单击按钮时,将删除第一个面板并显示第二个面板。第二个面板显示数据库中的图像及其描述。问题是我想在第二个面板上显示一个特定的图像及其描述。这是我的代码。我希望你能帮助我。这是我的代码,随时可以测试它。

头等舱:

package state_switcher;

    /**
     * @author Ron Brash (ron.brash@gmail.com)
     * 
     *         The concept of this enum is to contain the states
     *         to render the correct button/panels.
     */
    public enum ViewState {
        START_STATE, NEXT_STATE;
    }

第二课:

package state_switcher;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import state_switcher.ViewState;

/**
 * @author Ron Brash (ron.brash@gmail.com)
 * 
 *         The concept of this class is to create a panel which
 *         contains a JButton which has an inline actionListener.
 *         
 *         The action listener will change the state using a static
 *         function from the class Main.
 */
@SuppressWarnings("serial")
public class Panel1 extends JPanel {
    static int xy;
    public Panel1() {
        JPanel panel1 = new JPanel();

        String url  = "jdbc:mysql://localhost:3306/";
        String user = "root";
        String pass = "";

        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection(url, user, pass);

            Statement stt = con.createStatement();

            //Get people with surname 
            ResultSet res = stt.executeQuery("SELECT * FROM products");
            setLayout(null);
            int i = 1, x = 10, y = 1, z = 10;
            while(res.next())
            {
                String val = Integer.toString(i);
                String fileName = "D:\\" + res.getString("image");

                ImageIcon imageIcon = new ImageIcon(fileName);
                JButton btn = new JButton(resize(imageIcon, 100, 200));
                btn.setName(val);

                btn.addActionListener(new ActionListener()
                {
                      public void actionPerformed(ActionEvent e)
                      {
                        // display the second panel when pressed.

                          Main.changeState(ViewState.NEXT_STATE);
                      }
                }); //add an actionListener right away

                if(y <= 3)
                {
                    add(btn).setBounds(x, z, 100, 200);
                }

                else
                {
                    y = 1;
                    x = 10;
                    z += 220;
                    add(btn).setBounds(x, z, 100, 200);
                }

                x+=130;
                y++;
                i++;
            }


            //Free resources
            res.close();
            stt.close();
            con.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        this.add(panel1);
    }

    public static ImageIcon resize(ImageIcon imageIcon, int width, int height)
    {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(imageIcon.getImage(), 0, 0, width, height, null);
        g2d.dispose();
        return new ImageIcon(bi);
    }
}

第三类:

package state_switcher;


import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * @author Ron Brash (ron.brash@gmail.com)
 * 
 *         The concept of this class is to create a panel which
 *         contains a JButton which has an inline actionListener.
 *         
 *         The action listener will change the state using a static
 *         function from the class Main.
 */
@SuppressWarnings("serial")
public class Panel2 extends JPanel {

    public Panel2() {
        JPanel  panel2 = new JPanel();
        JButton button = new JButton("sweet");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Main.changeState(ViewState.START_STATE);

            }

        });
        String url  = "jdbc:mysql://localhost:3306/";
        String user = "root";
        String pass = "";

        try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection(url, user, pass);

            Statement stt = con.createStatement();

            //Create and select database
            stt.execute("CREATE DATABASE IF NOT EXISTS test");
            stt.execute("USE gamingplace");


            int ID = 0;
            ResultSet res = stt.executeQuery("SELECT * FROM products WHERE id = " + ID);
            setLayout(null);

            while(res.next())
            {
                ImageIcon imageIcon = new ImageIcon("D:\\" + res.getString("image")); 
                JButton button1 = new JButton(imageIcon);
                add(button1).setBounds(10, 10, 300, 400);
                button1.addActionListener(new ActionListener()
                {
                      public void actionPerformed(ActionEvent e)
                      {
                        // display back the first panel when pressed
                          Main.changeState(ViewState.START_STATE);
                      }
                }); //add an actionListener right away
            }

            //Free resources
            res.close();
            stt.close();
            con.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        this.add(panel2);
    }

    public static ImageIcon resize(ImageIcon imageIcon, int width, int height)
    {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(imageIcon.getImage(), 0, 0, width, height, null);
        g2d.dispose();
        return new ImageIcon(bi);
    }
}

第四级和主要级别:

package state_switcher;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author Ron Brash (ron.brash@gmail.com)
 * 
 *         The concept of this class is to create a JFrame which contains three
 *         panels:
 * 
 *         > mpanel is the master panel in which all of the other panel related
 *         content will be added to it
 * 
 *         > panel1 is the first panel to be displayed
 * 
 *         > panel2 is the second panel to be displayed
 * 
 *         Both panel1 and panel2 contain JButtons which will alter the enum
 *         viewState and depending on the state (which is determined by button
 *         click), the correct panel will be rendered.
 * 
 *         If you do not use a master panel, removeAll() will cause logic errors
 *         for whatever reason.
 */
public class Main {

    private static ViewState viewState;
    private static JPanel mpanel;
    private static JPanel panel1;
    private static JPanel panel2;
    private static JFrame frame;
    static JButton button = new JButton("2");

    public Main() {

        frame = new JFrame();
        mpanel = new JPanel();
        panel1 = new Panel1();
        panel2 = new Panel2();
        button.setName("2");
        // Sets default state
        changeState(ViewState.START_STATE);

        frame.setSize(1200, 1000);
        frame.add(mpanel);
        frame.setVisible(true);

    }

    /**
     * changeState(ViewState state)
     * 
     * Publicly available state changer function
     * 
     * @param state
     * @return void
     */
    public static void changeState(ViewState state) {
        viewState = state;
        System.out.println("change state: " + viewState);

        switch (state) {
        case START_STATE:
            mpanel.setLayout(null);
            mpanel.removeAll();
            mpanel.add(panel1).setBounds(10, 10, 1000, 850);;
            mpanel.revalidate();
            mpanel.repaint();
            break;
        case NEXT_STATE:
            mpanel.setLayout(null);
            mpanel.removeAll();
            mpanel.add(panel2).setBounds(10, 10, 1000, 850);
            mpanel.revalidate();
            mpanel.repaint();
            break;
        default:
            System.out.println("UNKNOWN STATE!");
            break;
        }
    }

    /**
     * main(String[] args)
     * 
     * Like an opinion, everyone has one.
     * 
     * @param args
     * @return void
     */
    public static void main(String[] args) {
        @SuppressWarnings("unused")
        Main n = new Main();

    }

}

0 个答案:

没有答案