Однако JPanel занимает слишком много вертикального пространства. Я хочу только тонкую горизонтальную полосу. Я использую Gridlayout (3, 0) на кадре и добавляю три панели (заголовок, контент, нижний колонтитул). < /P>
Вот соответствующая часть: < /p>
import javax.swing.*;
public class Main {
public static void main(String[] args) {
ConverterView view = new ConverterView();
}
}
< /code>
public enum Units {
METRE, CENTIMETRE, DECIMETRE, KILOMETRE
}
< /code>
public enum UnitCategory {
LENGTH, WEIGHT, TEMPERATURE, TIME, VOLUME, AREA, SPEED, ENERGY
}
< /code>
import javax.swing.*;
import java.awt.*;
public class ConverterView {
private JLabel titleLabel;
private JLabel categoryLabel;
private JLabel categoryUnit;
private JLabel inputUnitLabel;
private JLabel outputUnitLabel;
private JLabel outcomeLabel;
private JLabel valueLabel;
private JComboBox categoryBox;
private JComboBox inputBoxUnit;
private JComboBox outputBoxUnit;
private JTextField sourceValue;
private JTextField targetValue;
private JButton convertButton;
private JPanel upperPanel;
private JPanel middlePanel;
private JPanel lowerPanel;
private JFrame frame;
private JLabel resultLabel;
private JTextField resultTextField;
public ConverterView() {
createComponents();
layoutComponents();
frame.pack();
frame.setVisible(true);
}
private void createComponents() {
titleLabel = new JLabel("Unit Converter");
titleLabel.setFont(new Font("Calibri", Font.BOLD, 20));
frame = new JFrame("Unit Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(350, 350));
frame.setLayout(new GridLayout(3, 0));
frame.setLocationRelativeTo(null);
categoryLabel = new JLabel("Category");
inputUnitLabel = new JLabel("Input Unit");
outputUnitLabel = new JLabel("Output Unit");
valueLabel = new JLabel("Value");
outcomeLabel = new JLabel("Outcome");
resultLabel = new JLabel("Result");
resultTextField = new JTextField();
convertButton = new JButton("Convert");
convertButton.setPreferredSize(new Dimension(250, 30));
inputBoxUnit = new JComboBox(Units.values());
outputBoxUnit = new JComboBox(Units.values());
categoryBox = new JComboBox(UnitCategory.values());
upperPanel = new JPanel();
upperPanel.setBackground(Color.red);
upperPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
middlePanel = new JPanel();
middlePanel.setBackground(Color.yellow);
middlePanel.setLayout(new GridLayout(3, 2));
lowerPanel = new JPanel();
lowerPanel.setBackground(Color.blue);
lowerPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
}
private void layoutComponents() {
upperPanel.add(titleLabel);
middlePanel.add(categoryLabel);
middlePanel.add(categoryBox);
middlePanel.add(inputUnitLabel);
middlePanel.add(inputBoxUnit);
middlePanel.add(valueLabel);
middlePanel.add(resultLabel);
middlePanel.add(resultTextField);
lowerPanel.add(convertButton);
frame.add(upperPanel);
frame.add(middlePanel);
frame.add(lowerPanel);
}
}
< /code>
Is there a way to limit the height of upperPanel so it just fits the title label and doesn’t stretch?


Подробнее здесь: https://stackoverflow.com/questions/796 ... java-swing