Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在窗口中移动图像?_Java_Swing_Jpanel_Layout Manager - Fatal编程技术网

Java 如何在窗口中移动图像?

Java 如何在窗口中移动图像?,java,swing,jpanel,layout-manager,Java,Swing,Jpanel,Layout Manager,我很难想出如何移动和图像到窗口中的不同位置。我读过关于BorderLayout,但我不知道如何实现它。我想把车放在文本区域上方,所以我会以某种方式使用BorderLayout.NORTH b3.addActionListener(new ActionListener() { /** * Displays the arraylist. */ public void actionPerformed(ActionEvent e) {

我很难想出如何移动和图像到窗口中的不同位置。我读过关于
BorderLayout
,但我不知道如何实现它。我想把车放在文本区域上方,所以我会以某种方式使用
BorderLayout.NORTH

b3.addActionListener(new ActionListener() {
        /**
         * Displays the arraylist.
         */
        public void actionPerformed(ActionEvent e) {

            if (cars.size()>0){

                ImageIcon icon = new ImageIcon(Window.class.getResource("/car.png"));
                StringBuilder sb = new StringBuilder();


                for(int i=0; i < cars.size(); i++) {
                    sb.append("Car " + (i+1) + ": " + cars.get(i) + "\n");
                }

                Font font = new Font("Times New Roman", Font.PLAIN, 14);
                JTextArea textArea = new JTextArea(sb.toString());
                JScrollPane scrollPane = new JScrollPane(textArea); 
                textArea.setFont(font);
                textArea.setForeground(Color.BLACK);
                textArea.setLineWrap(true);
                textArea.setEditable(false);
                textArea.setWrapStyleWord(true); 
                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                scrollPane.setPreferredSize(new Dimension( 100, 125 ));
                JOptionPane.showMessageDialog(null, scrollPane, "Inventory", JOptionPane.PLAIN_MESS![enter image description here][2]AGE, icon);
            }
            else {
                JOptionPane.showMessageDialog(null, "No cars in inventory", "Error", JOptionPane.ERROR_MESSAGE);
            }


        }

    });
b3.addActionListener(新ActionListener(){
/**
*显示arraylist。
*/
已执行的公共无效操作(操作事件e){
如果(cars.size()>0){
ImageIcon=newImageIcon(Window.class.getResource(“/car.png”);
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
您声明:

我很难想出如何移动和图像到窗口中的不同位置

你会想告诉我们你问题的细节。你现在的形象在哪里?您希望您的GUI看起来像什么?细节很重要

我读过BorderLayout,但不知道如何实现它

这本教程让你困惑的是什么

我不会把车放在文本区域上方,所以我会以某种方式使用BorderLayout.NORTH吗

b3.addActionListener(new ActionListener() {
        /**
         * Displays the arraylist.
         */
        public void actionPerformed(ActionEvent e) {

            if (cars.size()>0){

                ImageIcon icon = new ImageIcon(Window.class.getResource("/car.png"));
                StringBuilder sb = new StringBuilder();


                for(int i=0; i < cars.size(); i++) {
                    sb.append("Car " + (i+1) + ": " + cars.get(i) + "\n");
                }

                Font font = new Font("Times New Roman", Font.PLAIN, 14);
                JTextArea textArea = new JTextArea(sb.toString());
                JScrollPane scrollPane = new JScrollPane(textArea); 
                textArea.setFont(font);
                textArea.setForeground(Color.BLACK);
                textArea.setLineWrap(true);
                textArea.setEditable(false);
                textArea.setWrapStyleWord(true); 
                scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                scrollPane.setPreferredSize(new Dimension( 100, 125 ));
                JOptionPane.showMessageDialog(null, scrollPane, "Inventory", JOptionPane.PLAIN_MESS![enter image description here][2]AGE, icon);
            }
            else {
                JOptionPane.showMessageDialog(null, "No cars in inventory", "Error", JOptionPane.ERROR_MESSAGE);
            }


        }

    });
在使用容器向BorderLayout添加组件时,通常使用BorderLayout常量,例如
BorderLayout.NORTH
。通过“添加”,我的意思是调用容器的
add(…)
方法,在此方法中,首先将要添加到容器中的组件传递到该方法中,然后是告诉BorderLayout布局管理器要将其添加到何处的常量

e、 g

但是,关于详细信息,您需要阅读教程。链接:

    • 您声明:

      我很难想出如何移动和图像到窗口中的不同位置

      你会想告诉我们你问题的细节。你现在的形象在哪里?您希望您的GUI看起来像什么?细节很重要

      我读过BorderLayout,但不知道如何实现它

      这本教程让你困惑的是什么

      我不会把车放在文本区域上方,所以我会以某种方式使用BorderLayout.NORTH吗

      b3.addActionListener(new ActionListener() {
              /**
               * Displays the arraylist.
               */
              public void actionPerformed(ActionEvent e) {
      
                  if (cars.size()>0){
      
                      ImageIcon icon = new ImageIcon(Window.class.getResource("/car.png"));
                      StringBuilder sb = new StringBuilder();
      
      
                      for(int i=0; i < cars.size(); i++) {
                          sb.append("Car " + (i+1) + ": " + cars.get(i) + "\n");
                      }
      
                      Font font = new Font("Times New Roman", Font.PLAIN, 14);
                      JTextArea textArea = new JTextArea(sb.toString());
                      JScrollPane scrollPane = new JScrollPane(textArea); 
                      textArea.setFont(font);
                      textArea.setForeground(Color.BLACK);
                      textArea.setLineWrap(true);
                      textArea.setEditable(false);
                      textArea.setWrapStyleWord(true); 
                      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                      scrollPane.setPreferredSize(new Dimension( 100, 125 ));
                      JOptionPane.showMessageDialog(null, scrollPane, "Inventory", JOptionPane.PLAIN_MESS![enter image description here][2]AGE, icon);
                  }
                  else {
                      JOptionPane.showMessageDialog(null, "No cars in inventory", "Error", JOptionPane.ERROR_MESSAGE);
                  }
      
      
              }
      
          });
      
      在使用容器向BorderLayout添加组件时,通常使用BorderLayout常量,例如
      BorderLayout.NORTH
      。通过“添加”,我的意思是调用容器的
      add(…)
      方法,在此方法中,首先将要添加到容器中的组件传递到该方法中,然后是告诉BorderLayout布局管理器要将其添加到何处的常量

      e、 g

      但是,关于详细信息,您需要阅读教程。链接:


      如果要将图片放置在不同的部分,则需要使用边框布局功能。你可以有中心、北、南、西、东等

      以下是您将如何实施它:

      setLayout(new BorderLayout());
      JPanel southPanel = new JPanel();
      southPanel.add(PICTUREGOESHERE);
      add(southPanel, BorderLayout.SOUTH);
      
      首先,将布局设置为边框布局。 你可以创建另一个JPanel。 将组件添加到JPanel。
      您可以将JPanel添加到主JFrame中。

      如果要将图片放置在不同的部分中,则需要使用边框布局功能。你可以有中心、北、南、西、东等

      以下是您将如何实施它:

      setLayout(new BorderLayout());
      JPanel southPanel = new JPanel();
      southPanel.add(PICTUREGOESHERE);
      add(southPanel, BorderLayout.SOUTH);
      
      首先,将布局设置为边框布局。 你可以创建另一个JPanel。 将组件添加到JPanel。
      您可以将JPanel添加到主JFrame。

      我以为图像已经添加,但我想没有。我把它编辑成中文。我希望图像位于文本区域上方,但我不知道如何在对话框中进行。@Eric:图像应该在您的问题中,而不是在我的答案中。我以为图像是添加的,但我想不是。我把它编辑成中文。我希望图像位于文本区域上方,但我不知道如何在对话框中进行。@Eric:图像应该在您的问题中,而不是我的答案中。