次のコードは問題なく動作していますが、フォント サイズを変更して JTextBox の最大領域を埋める方法があるかどうかを知りたいです。3 つの異なるテーマでコードを実行すると、異なる結果が得られます。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.SwingConstants;
import javax.swing.JPasswordField;
import java.awt.Component;
public class NewLoginBox extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
private final JPanel contentPanel = new JPanel();
private JTextField userID;
private JPasswordField passwordField;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
NewLoginBox dialog = new NewLoginBox();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public NewLoginBox() {
setBounds(100, 100, 350, 220);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new TitledBorder(null, "Sign In", TitledBorder.LEADING, TitledBorder.TOP, null, null));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
{
JPanel titlePanel = new JPanel();
contentPanel.add(titlePanel);
titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.Y_AXIS));
{
JLabel lblPleaseEnterYour = new JLabel("Welcome to Application. Please Sign In ");
lblPleaseEnterYour.setAlignmentX(Component.CENTER_ALIGNMENT);
lblPleaseEnterYour.setAlignmentY(Component.BOTTOM_ALIGNMENT);
lblPleaseEnterYour.setHorizontalAlignment(SwingConstants.LEFT);
titlePanel.add(lblPleaseEnterYour);
titlePanel.add(Box.createVerticalStrut(15));
}
}
{
JPanel formPanel = new JPanel();
contentPanel.add(formPanel);
formPanel.setLayout(new GridLayout(0, 2, 5, 5));
{
JLabel lblUserId = new JLabel("User ID");
formPanel.add(lblUserId);
}
{
userID = new JTextField("", 15);
formPanel.add(userID);
userID.setColumns(10);
}
{
JLabel lblPassword = new JLabel("Password");
formPanel.add(lblPassword);
}
{
passwordField = new JPasswordField();
formPanel.add(passwordField);
}
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
{
JButton btnHelp = new JButton("Help");
buttonPane.add(btnHelp);
}
}
}
}
- 最高のルック アンド フィールです。
Jtextbox のサイズは、その中のテキストに応じて動的に変更する必要があります。一貫したルックアンドフィールを持つことができます。
ありがとうアシシュ・ティアギ