试图布局JPanels。每次运行时Java程序布局都会更改

时间:2014-05-27 14:57:55

标签: java swing layout jpanel

第一次发帖对我很轻松。

我是Java新手,我正试图让3个JPanels相互排列。第一个图像是我想要它看起来的样子,它有时在我运行程序时有效,但正如你可以看到的其他图像,它不会在每次运行时排队。有时甚至没有显示一些图像/组件。

那么如何让三个JPanels一个接一个地垂直排列?

import java.awt.event.ActionListener;
import java.awt.*;

import javax.swing.*;

public class FrameMain {

    static final int MY_MINIMUM = 0;

    static final int MY_MAXIMUM = 100;

        public static void main(String[] args) {


          JFrame frame1 = new JFrame("Harvest Frame Test");
          frame1.setVisible(true);
          frame1.setSize(800,700);
          frame1.setResizable(false);
          frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          //Container Panel

          JPanel container = new JPanel();
          container.setSize(800,700);
          container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
          frame1.add(container);



          //First Panel

          JPanel panel1 = new JPanel();
          panel1.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0



          container.add(panel1);

          JButton button1 = new JButton("Add Water");
          panel1.add(button1);
          JButton button2 = new JButton("Add Food");
          panel1.add(button2);
          JButton button3 = new JButton("Add Medicine");
          panel1.add(button3);

          ImageIcon image = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
          JLabel imagelabel = new JLabel(image);
          panel1.add(imagelabel);

          JProgressBar pbar = new JProgressBar();
          pbar.setMinimum(MY_MINIMUM);
          pbar.setMaximum(MY_MAXIMUM);
          // add to JPanel
          panel1.add(pbar);

          // Second Panel

          JPanel panel2 = new JPanel();
          panel2.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0


          container.add(panel2);

          JButton button4 = new JButton("Add Water");
          panel2.add(button4);
          JButton button5 = new JButton("Add Food");
          panel2.add(button5);
          JButton button6 = new JButton("Add Medicine");
          panel2.add(button6);

          ImageIcon image1 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
          JLabel imagelabel1 = new JLabel(image1);
          panel2.add(imagelabel1);

          JProgressBar pbar1 = new JProgressBar();
          pbar1.setMinimum(MY_MINIMUM);
          pbar1.setMaximum(MY_MAXIMUM);
          // add to JPanel
          panel2.add(pbar1);

          // Third Panel

          JPanel panel3 = new JPanel();
          panel3.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0

          container.add(panel3);

          JButton button7 = new JButton("Add Water");
          panel3.add(button7);
          JButton button8 = new JButton("Add Food");
          panel3.add(button8);
          JButton button9 = new JButton("Add Medicine");
          panel3.add(button9);

          ImageIcon image2 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
          JLabel imagelabel2 = new JLabel(image2);
          panel3.add(imagelabel2);

          JProgressBar pbar2 = new JProgressBar();
          pbar2.setMinimum(MY_MINIMUM);
          pbar2.setMaximum(MY_MAXIMUM);
          // add to JPanel
          panel3.add(pbar2);

    }

    //static class Action implements ActionListener {

        //public void actionPerformed (ActionEvent e){



        //}

        //}

    }

1 个答案:

答案 0 :(得分:2)

frame1.setVisible(true);一直移到底部。更改已经可见的框架上的组件可能会导致问题。