【问题标题】:How to add an image to JPanel that is in a JPanel?如何将图像添加到 JPanel 中的 JPanel?
【发布时间】:2017-09-07 01:46:00
【问题描述】:

我总共有 7 个JPanel 容器。我想添加一个我生成的 png 图像,或者借助 JPanel(imagePan) 中的按钮(充电器图像)对其进行缓冲

到目前为止,我在 Swing 教程中看到的大多数示例都使用 ImageIcon

  1. 生成的图像为 326X254
  2. 如何正确地将图像添加到面板?

您将在此处找到生成以下窗口的代码:

   import java.awt.BorderLayout;
   import java.awt.Dimension;
   import java.awt.GridLayout;
   import java.io.IOException;
   import java.io.OutputStream;
   import java.io.PrintStream;

   import javax.swing.BorderFactory;
   import javax.swing.Box;
   import javax.swing.BoxLayout;
   import javax.swing.ImageIcon;
   import javax.swing.JButton;
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JPanel;
   import javax.swing.JTextArea;
   import javax.swing.SwingUtilities;
   import javax.swing.border.Border;

   public class View {
   private JFrame frame;
   private JPanel globalPan, firstHorisontalPan, secondhorisontalPan, 
   calibrationPan, imagePan, manipPan, solutionPan; // susp
   private JButton raproche, ecarter, sauvgarder, demarrer, stop, charger;
   private BorderLayout BorderGlobalePan, BorderSecondPane, BorderManipPane, 
   BorderFirstHorisontalPan, BorderResolPan, BorderCalibPan, 
   BorderChargerPan;
    private JTextArea console;
    private Box calibrationBox, solutionBox;


    public void init() {
    // declaration de JFrame
    frame = new JFrame("Rubi's Cube IHM");

    // JPanle
    globalPan = new JPanel();
    firstHorisontalPan = new JPanel();
    secondhorisontalPan = new JPanel();
    imagePan = new JPanel();
    manipPan = new JPanel();
    calibrationPan = new JPanel();
    solutionPan = new JPanel();

    //
    calibrationBox = Box.createVerticalBox();
    solutionBox = Box.createVerticalBox();

    // borderLayout
    BorderGlobalePan = new BorderLayout();
    BorderSecondPane = new BorderLayout();
    BorderManipPane = new BorderLayout();
    BorderFirstHorisontalPan = new BorderLayout();
    BorderResolPan = new BorderLayout();
    BorderCalibPan =new BorderLayout();
    BorderChargerPan = new BorderLayout();

    // JButton
    raproche = new JButton("raprocher");
    ecarter = new JButton("ecarter");
    sauvgarder = new JButton("sauvgarder");
    demarrer = new JButton("demarrer");
    stop = new JButton("stop");
    charger = new JButton("charger image");

    console = new JTextArea();

    //add  JPanel names
    firstHorisontalPan.setBorder(BorderFactory.createTitledBorder("Etat"));
    calibrationPan.setBorder(BorderFactory.createTitledBorder("calibration"));
    solutionPan.setBorder(BorderFactory.createTitledBorder("résolution & manipulation"));
    imagePan.setBorder(BorderFactory.createTitledBorder("visualisation"));

    // definition of JButton size
    raproche.setPreferredSize(new Dimension(200, 30));
    ecarter.setPreferredSize(new Dimension(200, 30));
    sauvgarder.setPreferredSize(new Dimension(200, 30));
    demarrer.setPreferredSize(new Dimension(200, 30));
    stop.setPreferredSize(new Dimension(200, 30));
    charger.setPreferredSize(new Dimension(200, 30));

    //definition of JPanel size
    globalPan.setPreferredSize(new Dimension(1024, 600));
    firstHorisontalPan.setPreferredSize(new Dimension(1024, 130));
    secondhorisontalPan.setPreferredSize(new Dimension(1024, 480));
    imagePan.setPreferredSize(new Dimension(850, 480));
    manipPan.setPreferredSize(new Dimension(150, 480));
    calibrationPan.setPreferredSize(new Dimension(200, 200));
    solutionPan.setPreferredSize(new Dimension(200, 100));

    calibrationBox.setPreferredSize(new Dimension(200, 200));
    solutionBox.setPreferredSize(new Dimension(200, 100));

    firstHorisontalPan.setLayout(BorderFirstHorisontalPan);
    firstHorisontalPan.add(console);
    //image



    // JPane calibration
    calibrationBox.add(Box.createVerticalStrut(10));
    calibrationBox.add(raproche);
    calibrationBox.add(Box.createVerticalStrut(10));
    calibrationBox.add(ecarter);
    calibrationBox.add(Box.createVerticalStrut(10));
    calibrationBox.add(sauvgarder);

    calibrationPan.setLayout(BorderCalibPan);
    calibrationPan.add(calibrationBox, BorderLayout.CENTER);

    // JPane resolution & manipulation
    solutionBox.add(Box.createVerticalStrut(10));
    solutionBox.add(demarrer);
    solutionBox.add(Box.createVerticalStrut(10));
    solutionBox.add(stop);

    solutionPan.setLayout(BorderResolPan);
    solutionPan.add(solutionBox, BorderLayout.CENTER);


    //JPane ManipPane
    manipPan.setLayout(BorderManipPane);
    manipPan.add(calibrationPan, BorderLayout.NORTH);
    BorderManipPane.setVgap(20);
    manipPan.add(solutionPan, BorderLayout.CENTER);

    //JPane secondPane
    secondhorisontalPan.setLayout(BorderSecondPane);
    secondhorisontalPan.add(manipPan, BorderLayout.WEST);
    BorderSecondPane.setHgap(7);
    secondhorisontalPan.add(imagePan, BorderLayout.CENTER);

    //JPane GlobalHorisontalPane
    globalPan.setLayout(BorderGlobalePan);
    globalPan.add(firstHorisontalPan, BorderLayout.NORTH);
    BorderGlobalePan.setVgap(10);
    globalPan.add(secondhorisontalPan, BorderLayout.CENTER);

    //Jpane imagePan
    BorderChargerPan.setVgap(10);
    imagePan.add(charger);

    // window
    frame.add(globalPan);
    frame.setSize(1024, 600);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setTitle("cubeBerry");
    frame.setResizable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

IHM

【问题讨论】:

    标签: java image swing jpanel


    【解决方案1】:

    如何正确地将图像添加到 Jpanel?

    1. 创建一个ImageIcon
    2. 将图标添加到JLabel
    3. 将标签添加到JPanel

    阅读 How to Use Icons 上的 Swing 教程部分,了解更多信息和工作示例。

    此外,从您发布的代码中删除所有 setPreferredSize() 语句。布局管理器将确定组件的首选大小。 Swing 旨在与布局管理器一起使用。让布局管理器完成它的工作。

    console = new JTextArea();
    

    创建 JTextArea 时,请执行以下操作:

    console = new JTextArea(5, 30);
    

    将建议大小应为 5 行和 30 列。现在布局管理器可以根据这些信息计算出首选尺寸。

    private BorderLayout BorderGlobalePan, BorderSecondPane, BorderManipPane, ...
    

    变量名称不应以大写字符开头。您的大多数变量都是正确的,但不是全部。保持一致!!!

    frame.setSize(1024, 600);
    

    不要硬编码大小。你不知道我电脑的分辨率是多少。而是使用pack() 方法,让布局管理器完成他们的工作。

    【讨论】:

      猜你喜欢
      • 2010-09-22
      • 2012-07-12
      • 1970-01-01
      • 2018-09-02
      • 2013-08-25
      • 2016-02-15
      相关资源
      最近更新 更多