Java Swing-新行上的按钮

时间:2019-04-14 16:59:40

标签: java swing

enter code here我有一个带有工具栏的UI,可以在图像上应用不同的滤镜,并且图像显示在右侧(工具栏在左侧),我要添加新按钮,但是工具栏现在真的很长,我想给一个限制,所以一旦达到总帧数的1/5,我想在新行上显示按钮。

public class Toolbar extends JPanel {
  private JButton filterOne;
  private JButton filterTwo;
  private JButton filterThree;
  private JButton loadImage;
  private JLabel picture;


  Toolbar(){
    filterOne = new JButton("filterOne");
    filterTwo = new JButton("filterTwo");
    filterThree = new JButton("filterThree");
    loadImage = new JButton("loadImage");
    picture = new JLabel();

    setLayout(new FlowLayout());

    add(loadImage);

    loadImage.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) { 
         ImageIcon pic = new ImageIcon("ImageOne.png");
         picture.setIcon(pic);

      }
    });
    add(filterOne);
    add(filterTwo);
    add(filterThree);
    add(picture);
  }
}



public class MainFrame extends JFrame {

  private JTextArea textArea;
  private Toolbar toolbar;
  private ImagePannel imagePannel;

  MainFrame() {
    // Generating a new
    setLayout(new BorderLayout());

    // Generating a new toolbar that contains all the buttons to generate new images.
    toolbar = new Toolbar();

    imagePannel = new ImagePannel();

    add(imagePannel,BorderLayout.CENTER);

    // Position of the toolbar within the upper side of the pannel.
    add(toolbar, BorderLayout.WEST);

    //This is the name of the window
    JFrame frame = new JFrame("My view");

    //Initialize the size of the window
    setSize(1200, 800);

    //Makes the window close when we are crossing the window
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Makes the window visible
    setVisible(true);
  }
}

1 个答案:

答案 0 :(得分:0)

  

...所以一旦达到总帧数的1/5,我想在新行上显示按钮。

请勿使用setLayout(new FlowLayout());,因为这不是一种“智能”布局,您可以轻松控制它。取而代之的是使用另一种布局,也许是GridLayout(0, 5) -一种行数可变且有5列的布局。