Java Swing - кнопки на новой строке - PullRequest
0 голосов
/ 14 апреля 2019

enter code here У меня есть пользовательский интерфейс с панелью инструментов для применения различных фильтров к изображению, и изображение отображается справа (панель инструментов находится слева) Я добавляю новые кнопки, но панель инструментов теперь очень длинная и я хочу задать предел, чтобы, когда он достигал 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 голосов
/ 14 апреля 2019

... поэтому, когда он достигает 1/5 от общего кадра, я хочу отобразить кнопки в новой строке.

Не используйте setLayout(new FlowLayout());, поскольку это не «умный» макет, которым вы можете легко управлять. Вместо этого используйте другой макет, возможно GridLayout(0, 5) - один с переменным числом строк и с 5 столбцами.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...