我正在尝试使用HTML格式,以便在JLabel
上显示上标,但它似乎比标准字体占用更多空间,所以JLabel
下面的所有JLabel
s上标被推倒了。每个JTextField
都应该与import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
class JLabelExample extends JFrame {
private JPanel wrapper, configInputPanel;
private GridBagConstraints gbc;
JLabelExample() {
configInputPanel();
wrapper();
add(wrapper);
pack();
setVisible(true);
}
private void wrapper() {
wrapper = new JPanel(new GridBagLayout());
wrapper.setBorder(BorderFactory.createEmptyBorder(25, 25, 20, 25));
JPanel inner = new JPanel(new GridBagLayout());
inner.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Configuration"),
BorderFactory.createEmptyBorder(15, 15, 15, 15)));
gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
inner.add(configInputPanel, gbc);
gbc.gridx = 0;
wrapper.add(inner, gbc);
}
private void configInputPanel() {
configInputPanel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(0, 5, 0, 5);
gbc.gridx = 0;
configInputPanel.add(new JLabel("Fuselage Length (m)"), gbc);
configInputPanel.add(new JLabel("Fuselage Width (m)"), gbc);
configInputPanel.add(new JLabel("Fuselage Height (m)"), gbc);
configInputPanel.add(new JLabel("Wing Span (m)"), gbc);
configInputPanel.add(new JLabel("<html>Wing Area (m<sup>2</sup>)</html>"), gbc);
configInputPanel.add(new JLabel("Wing Sweep (degree)"), gbc);
configInputPanel.add(new JLabel("Nose Gear to Fuselage Tip (m)"), gbc);
configInputPanel.add(new JLabel("Main Gear to Nose Gear (m)"), gbc);
configInputPanel.add(new JLabel("Main Gear Separation (m)"), gbc);
configInputPanel.add(new JLabel("Body Gear to Nose Gear (m)"), gbc);
configInputPanel.add(new JLabel("Body Gear Separation (m)"), gbc);
configInputPanel.add(new JLabel("Engine to Centerline (m)"), gbc);
gbc.gridx = 1;
for (int i = 0; i < 12; i++) {
JTextField textfield = new JTextField(5);
textfield.setHorizontalAlignment(JTextField.RIGHT);
configInputPanel.add(textfield, gbc);
}
}
public static void main(String[] args) {
new JLabelExample();
}
}
对齐,这就是为什么这会使它看起来有些混乱。
我的描述可能有点复杂,但下面的代码可以编译,以便您可以看到我在说什么。第五个标签是带有上标的标签,导致其余标签脱节。
Const WM_SETTEXT As Long = &HC
Const GW_CHILD As Long = 5
Declare Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As Integer) As IntPtr
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr 'Int32
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
ByVal hWnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As String) As Int32
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hWnd1 As IntPtr = FindWindow("AIONClientWndClass1.0", "AION Client")
Dim hWndR2 As IntPtr = GetWindow(hWnd1, GW_CHILD)
SendMessage(hWndR2, WM_SETTEXT, 0, "h")
End Sub
答案 0 :(得分:1)
既然你正在寻找技巧,我允许自己提出这个建议。它不是一个上标,而是可读的。
private void configInputPanel() {
JPanel trickPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
JLabel label1 = new JLabel("Wing Area (m");
JLabel supLabel = new JLabel("2");
Font font = label1.getFont();
supLabel.setFont(font.deriveFont(font.getSize() / 1.5f));
trickPanel.add(label1);
trickPanel.add(supLabel);
trickPanel.add(new JLabel(")"));
configInputPanel = new JPanel(new GridBagLayout());
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(0, 5, 0, 5);
gbc.gridx = 0;
configInputPanel.add(new JLabel("Fuselage Length (m)"), gbc);
configInputPanel.add(new JLabel("Fuselage Width (m)"), gbc);
configInputPanel.add(new JLabel("Fuselage Height (m)"), gbc);
configInputPanel.add(new JLabel("Wing Span (m)"), gbc);
configInputPanel.add(trickPanel, gbc);
configInputPanel.add(new JLabel("Wing Sweep (degree)"), gbc);
configInputPanel.add(new JLabel("Nose Gear to Fuselage Tip (m)"), gbc);
configInputPanel.add(new JLabel("Main Gear to Nose Gear (m)"), gbc);
configInputPanel.add(new JLabel("Main Gear Separation (m)"), gbc);
configInputPanel.add(new JLabel("Body Gear to Nose Gear (m)"), gbc);
configInputPanel.add(new JLabel("Body Gear Separation (m)"), gbc);
configInputPanel.add(new JLabel("Engine to Centerline (m)"), gbc);
gbc.gridx = 1;
for (int i = 0; i < 12; i++) {
JTextField textfield = new JTextField(5);
textfield.setHorizontalAlignment(JTextField.RIGHT);
configInputPanel.add(textfield, gbc);
}
}
无间隙LEADING
- 对齐FlowLayout
给出了连续单个标签的印象。较小的字体大小是上标大小的近似值。对于不同的字体,它可能需要调整,但由于它是从其姐妹标签字体派生的,因此对于任何情况都应该是合理的。