Имеется следующий код:
package com.company;
import javax.swing.*;
import java.awt.*;
class MyFrame extends JFrame {
private static JLabel portText = new JLabel("Port:");
public static String getPortValue() {
return port.getText();
}
public static String getBrokerAddressValue() {
return brokerAddress.getText();
}
public static String getTopicValue() {
return topic.getText();
}
public static String getMessageValue() {
return message.getText();
}
public static String getUsernameValue() {
return username.getText();
}
private static JLabel brokerAddressText = new JLabel("Broker ip address:");
private static JLabel topicText = new JLabel("Topic");
private static JLabel messageText = new JLabel("Message:");
private static JLabel usernameText = new JLabel("Username:");
private static JTextField port = new JTextField(4);
private static JTextField brokerAddress = new JTextField(10);
private static JTextField topic = new JTextField(10);
private static JTextField message = new JTextField(20);
private static JTextField username = new JTextField(10);
private static JButton connect = new JButton("Connect");
private static JButton disconnect = new JButton("Disconnect");
private static JButton subscribe = new JButton("Subscribe");
private static JButton unsubscribe = new JButton("Unsubscribe");
private static JButton publish = new JButton("Publish");
MyFrame(){
super("MQTT client");
JPanel jPanel = new JPanel();
getContentPane().add(jPanel);
jPanel.setLayout(new GridBagLayout());
GridBagConstraints constraints1 = new GridBagConstraints();
constraints1.weightx = 0;
constraints1.weighty = 0;
constraints1.gridx = 0;
constraints1.gridy = 0;
constraints1.gridheight = 1;
constraints1.gridwidth = 1;
constraints1.insets.right = 10;
constraints1.insets.bottom = 10;
jPanel.add(portText, constraints1);
GridBagConstraints constraints2 = new GridBagConstraints();
constraints2.weightx = 0;
constraints2.weighty = 0;
constraints2.gridx = 0;
constraints2.gridy = 1;
constraints2.gridheight = 1;
constraints2.gridwidth = 1;
constraints2.insets.right = 10;
constraints2.insets.bottom = 10;
jPanel.add(port, constraints2);
GridBagConstraints constraints3 = new GridBagConstraints();
constraints3.weightx = 0;
constraints3.weighty = 0;
constraints3.gridx = 1;
constraints3.gridy = 0;
constraints3.gridheight = 1;
constraints3.gridwidth = 1;
constraints3.insets.right = 10;
constraints3.insets.bottom = 10;
jPanel.add(brokerAddressText, constraints3);
GridBagConstraints constraints4 = new GridBagConstraints();
constraints4.weightx = 0;
constraints4.weighty = 0;
constraints4.gridx = 1;
constraints4.gridy = 1;
constraints4.gridheight = 1;
constraints4.gridwidth = 1;
constraints4.insets.right = 10;
constraints4.insets.bottom = 10;
jPanel.add(brokerAddress, constraints4);
GridBagConstraints constraints5 = new GridBagConstraints();
constraints5.weightx = 0;
constraints5.weighty = 0;
constraints5.gridx = 2;
constraints5.gridy = 0;
constraints5.gridheight = 1;
constraints5.gridwidth = 1;
constraints5.insets.right = 10;
constraints5.insets.bottom = 10;
jPanel.add(connect, constraints5);
GridBagConstraints constraints6 = new GridBagConstraints();
constraints6.weightx = 0;
constraints6.weighty = 0;
constraints6.gridx = 2;
constraints6.gridy = 1;
constraints6.gridheight = 1;
constraints6.gridwidth = 1;
constraints6.insets.right = 10;
constraints6.insets.bottom = 10;
jPanel.add(disconnect, constraints6);
GridBagConstraints constraints7 = new GridBagConstraints();
constraints7.weightx = 0;
constraints7.weighty = 0;
constraints7.gridx = 0;
constraints7.gridy = 3;
constraints7.gridheight = 1;
constraints7.gridwidth = 1;
constraints7.insets.right = 10;
constraints7.insets.bottom = 10;
jPanel.add(topicText, constraints7);
GridBagConstraints constraints8 = new GridBagConstraints();
constraints8.weightx = 0;
constraints8.weighty = 0;
constraints8.gridx = 0;
constraints8.gridy = 4;
constraints8.gridheight = 1;
constraints8.gridwidth = 1;
constraints8.insets.right = 10;
constraints8.insets.bottom = 10;
jPanel.add(topic, constraints8);
GridBagConstraints constraints9 = new GridBagConstraints();
constraints9.weightx = 0;
constraints9.weighty = 0;
constraints9.gridx = 1;
constraints9.gridy = 3;
constraints9.gridheight = 1;
constraints9.gridwidth = 1;
constraints9.insets.right = 10;
constraints9.insets.bottom = 10;
jPanel.add(subscribe, constraints9);
GridBagConstraints constraints10 = new GridBagConstraints();
constraints10.weightx = 0;
constraints10.weighty = 0;
constraints10.gridx = 1;
constraints10.gridy = 4;
constraints10.gridheight = 1;
constraints10.gridwidth = 1;
constraints10.insets.right = 10;
constraints10.insets.bottom = 10;
jPanel.add(unsubscribe, constraints10);
GridBagConstraints constraints11 = new GridBagConstraints();
constraints11.weightx = 0;
constraints11.weighty = 0;
constraints11.gridx = 0;
constraints11.gridy = 6;
constraints11.gridheight = 1;
constraints11.gridwidth = 1;
constraints11.insets.right = 10;
constraints11.insets.bottom = 10;
jPanel.add(messageText, constraints11);
GridBagConstraints constraints12 = new GridBagConstraints();
constraints12.weightx = 0;
constraints12.weighty = 0;
constraints12.gridx = 0;
constraints12.gridy = 7;
constraints12.gridheight = 1;
constraints12.gridwidth = 2;
constraints12.insets.right = 10;
constraints12.insets.bottom = 10;
jPanel.add(message, constraints12);
GridBagConstraints constraints13 = new GridBagConstraints();
constraints13.weightx = 0;
constraints13.weighty = 0;
constraints13.gridx = 1;
constraints13.gridy = 6;
constraints13.gridheight = 1;
constraints13.gridwidth = 1;
constraints13.insets.right = 10;
constraints13.insets.bottom = 10;
jPanel.add(publish, constraints13);
GridBagConstraints constraints14 = new GridBagConstraints();
constraints14.weightx = 0;
constraints14.weighty = 0;
constraints14.gridx = 2;
constraints14.gridy = 3;
constraints14.gridheight = 1;
constraints14.gridwidth = 1;
constraints14.insets.right = 10;
constraints14.insets.bottom = 10;
jPanel.add(usernameText, constraints14);
GridBagConstraints constraints15 = new GridBagConstraints();
constraints15.weightx = 0;
constraints15.weighty = 0;
constraints15.gridx = 2;
constraints15.gridy = 4;
constraints15.gridheight = 1;
constraints15.gridwidth = 1;
constraints15.insets.right = 10;
constraints15.insets.bottom = 10;
jPanel.add(username, constraints15);
jPanel.revalidate();
setBounds(750,250,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
}
И вроде как некоторые элементы находятся через строку друг от друга, например
constraintsN.gridx = 2;
constraintsN.gridx = 0;
Но расстояние между ними такое же, как и между двумя соседними рядами. Скорее всего, как я думаю, это из-за того, что ряды между ними (в данном случае 1) не заполнены ничем, поэтому имеют высоту 0. Но как это исправить, ну или как-то по другому задать отступы. Или только отступ снизу от элемента? И еще вопрос, как растянуть например кнопку по ширине всей клетки?
Вот тут подробно расписано https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
А это пример с комментариями:
package com.company;
import javax.swing.*;
import java.awt.*;
import static java.awt.GridBagConstraints.*;
class MyFrame extends JFrame {
private static JLabel portText = new JLabel("Port:");
public static String getPortValue() {
return port.getText();
}
public static String getBrokerAddressValue() {
return brokerAddress.getText();
}
public static String getTopicValue() {
return topic.getText();
}
public static String getMessageValue() {
return message.getText();
}
public static String getUsernameValue() {
return username.getText();
}
private static JLabel brokerAddressText = new JLabel("Broker ip address:");
private static JLabel topicText = new JLabel("Topic");
private static JLabel messageText = new JLabel("Message:");
private static JLabel usernameText = new JLabel("Username:");
private static JTextField port = new JTextField(4);
private static JTextField brokerAddress = new JTextField(10);
private static JTextField topic = new JTextField(10);
private static JTextField message = new JTextField(20);
private static JTextField username = new JTextField(10);
private static JButton connect = new JButton("Connect");
private static JButton disconnect = new JButton("Disconnect");
private static JButton subscribe = new JButton("Subscribe");
private static JButton unsubscribe = new JButton("Unsubscribe");
private static JButton publish = new JButton("Publish");
private static JButton publish1 = new JButton("Publish");
private static JButton publish2 = new JButton("Publish");
private static GridBagConstraints costs(int location, int fill) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.weighty = 1.0;
constraints.weightx = 1.0;
constraints.fill = fill;
constraints.anchor = location;
return constraints;
}
private static GridBagConstraints costs(int location, int fill,
int gx, int gy) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.weighty = 1.0;
constraints.weightx = 1.0;
constraints.gridx = gx;
constraints.gridy = gy;
constraints.fill = fill;
constraints.anchor = location;
return constraints;
}
private static GridBagConstraints costs(int location, int fill,
int gx, int gy,
int gw, int gh) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.weighty = 1.0;
constraints.weightx = 1.0;
constraints.gridx = gx;
constraints.gridy = gy;
constraints.gridwidth = gw;
constraints.gridheight = gh;
constraints.fill = fill;
constraints.anchor = location;
return constraints;
}
private static GridBagConstraints costs(int location, int fill,
int gx, int gy,
int gw, int gh,
int top, int left, int bottom, int right) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.weighty = 1.0;
constraints.weightx = 1.0;
constraints.gridx = gx;
constraints.gridy = gy;
constraints.gridwidth = gw;
constraints.gridheight = gh;
constraints.fill = fill;
constraints.anchor = location;
constraints.insets = new Insets(top, left, bottom, right);
return constraints;
}
MyFrame() {
super("MQTT client");
JPanel jPanel = new JPanel();
add(jPanel);
jPanel.setLayout(new GridBagLayout());
//constraints.anchor: положение (внутри ячейки) элемента относительно сторон (вверх,право,центр,углы и тд)
//сonstraints.fill: заполнение (внутри ячейки) элемента (растягивание)
//constraints.gridx: положение элемента по x (номер ячейки)
//constraints.gridy: положение элемента по y (номер ячейки)
//constraints.gridwidth: длина элемента по x (количество ячеек)
//constraints.gridheight: ширина элемента по y (количество ячеек)
//
//
//constraints.insets: поля в пикселях вокруг элемента
//у метода costs есть перегрузка поэтому вводить не обязательно всё
// (это сильно экономит место и улучшает читабельность кода)
JTabbedPane jTabbedPane = new JTabbedPane();
jPanel.add(jTabbedPane,
//constraints.anchor, сonstraints.fill,
costs(LAST_LINE_END, BOTH,
//constraints.gridx, constraints.gridy
0, 0,
//constraints.gridwidth, constraints.gridheight
0, 0,
//constraints.insets
//top,left,bottom,right
10, 10, 10, 10));
JPanel page1 = new JPanel();
page1.setName("PAGE 1");
JPanel page2 = new JPanel();
page2.setName("PAGE 2");
JPanel page3 = new JPanel();
page3.setName("PAGE 3");
jTabbedPane.add(page1);
jTabbedPane.add(page2);
jTabbedPane.add(page3);
page1.setLayout(new GridBagLayout());
page2.setLayout(new GridBagLayout());
page3.setLayout(new GridBagLayout());
page1.add(portText, costs(PAGE_START, NONE, 0, 0));
page1.add(port, costs(PAGE_START, HORIZONTAL, 1, 0, 2, 1));
page1.add(brokerAddressText, costs(PAGE_START, NONE, 0, 1));
page1.add(brokerAddress, costs(PAGE_START, HORIZONTAL, 1, 1, 2, 1));
page1.add(connect, costs(LAST_LINE_START, NONE, 0, 2));
page1.add(publish, costs(PAGE_END, NONE, 1, 2));
page1.add(disconnect, costs(LAST_LINE_END, NONE, 2, 2));
page2.add(topicText, costs(LINE_START, NONE, 0, 0));
page2.add(topic, costs(CENTER, BOTH, 1, 0, 2, 1));
page2.add(subscribe, costs(LAST_LINE_START, NONE, 0, 2));
page2.add(publish1, costs(PAGE_END, NONE, 1, 2));
page2.add(unsubscribe, costs(LAST_LINE_END, NONE, 2, 2));
page3.add(messageText, costs(LINE_START, NONE, 0, 0));
page3.add(message, costs(CENTER, BOTH, 1, 0, 2, 1));
page3.add(publish2, costs(PAGE_END, NONE, 1, 2));
//set design style, depends on OS:
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(jPanel);
} catch (Exception ignored) {
}
jPanel.revalidate();
setBounds(750, 250, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new MyFrame();
}
}
Виртуальный выделенный сервер (VDS) становится отличным выбором
Я импортирую unity3d проект как aar библиотеку в android проект
Я хочу создать pdf файл с использованием iText 7, но что-то не так: