java-如何动态更改JTextbox中文本的大小
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何动态更改JTextbox中文本的大小,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6826字,纯文字阅读大概需要10分钟。
内容图文
![java-如何动态更改JTextbox中文本的大小](/upload/InfoBanner/zyjiaocheng/657/acca271dd5f04ba2beddec4340e14562.jpg)
我有以下运行良好的代码,但我想知道是否有任何方法可以更改字体大小以填充JTextBox中的最大区域.当我要编写三个不同主题的代码时,它给了我不同的结果.
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的大小应根据其中的文本动态变化.这样我们才能拥有一致的外观和感觉.
谢谢
Ashish Tyagi
解决方法:
>不要为formPanel使用GridLayout.而是使用GridBagLayout.
>或考虑使用第三方布局,例如MigLayout.
>不要设置GUI的界限.而是让布局管理器为您做到这一点.
>别忘了在顶层窗口中填充组件之后并在显示它们之前调用pack().
例如,
NewLoginBox.java:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
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.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
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;
public static void main(String[] args) {
try {
NewLoginBox dialog = new NewLoginBox();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public NewLoginBox() {
//setBounds(100, 100, 350, 220);
// getContentPane().setLayout(new BorderLayout());
TitledBorder titledBorder = new TitledBorder(null, "Sign In",
TitledBorder.LEADING, TitledBorder.TOP, null, null);
int ebGap = 10;
EmptyBorder emptyBorder = new EmptyBorder(ebGap, ebGap, ebGap, ebGap);
Border compoundBorder = new CompoundBorder(titledBorder, emptyBorder);
contentPanel.setBorder(compoundBorder );
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));
ebGap = 7;
formPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
formPanel.setLayout(new GridBagLayout());
JLabel lblUserId = new JLabel("User ID");
addWithGbc(formPanel, lblUserId, 0, 0);
userID = new JTextField("", 15);
addWithGbc(formPanel, userID, 1, 0);
userID.setColumns(10);
JLabel lblPassword = new JLabel("Password");
addWithGbc(formPanel, lblPassword, 0, 1);
passwordField = new JPasswordField();
addWithGbc(formPanel, passwordField, 1, 1);
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);
}
private void addWithGbc(Container container, JComponent component, int x,
int y) {
int hGap = 5;
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
if (x % 2 == 0) {
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(hGap, 0, hGap, 20); // magic number!
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
} else {
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(hGap, 0, hGap, 0);
gbc.weightx = 1.0;
gbc.weighty = 0.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
}
container.add(component, gbc);
}
}
内容总结
以上是互联网集市为您收集整理的java-如何动态更改JTextbox中文本的大小全部内容,希望文章能够帮你解决java-如何动态更改JTextbox中文本的大小所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。