gpt4 book ai didi

java - 无法在运行时调整文本字段的大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:06:45 25 4
gpt4 key购买 nike

我又改了代码。但是现在当我运行代码时,文本框非常小。只有当我选择左对齐、居中对齐或右对齐时,文本框的大小才会改变。我哪里弄错了。

我写了下面的程序。当我单击左、右或中心按钮时,程序还应读取列大小文本框中的值,然后自动调整消息文本字段的大小。但是我似乎做不到。任何见解将不胜感激。包装 workingwithjtextfields;

   package workingwithjtextfields;

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.border.*;

public class WorkingwithJTextFields extends JFrame
{
// private int size = 100;
private JTextField jtfMessage = new JTextField(100);

private JTextField jtfColumn = new JTextField(5);
private JRadioButton jrbLeft,jrbCenter,jrbRight;

public static void main(String[] args) //Main program begins here.
{
JFrame frame = new WorkingwithJTextFields();//Instantiating an object.
frame.setTitle("Exercise 17.11");//Setting the frame title.
frame.setSize(470,110);//Setting the size.
frame.setLocationRelativeTo(null);//Setting the location.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Default closing options.
frame.setVisible(true);//Setting visibility to true.
}//End of main program

public WorkingwithJTextFields()
{

// jtfMessage.setColumns(100);
final JPanel parent = new JPanel();
parent.setLayout(new GridLayout(2,1,3,3));

final JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT,30,0));

p1.add(new JLabel("TextField",SwingConstants.CENTER));

jtfMessage= new JTextField("Type anything",SwingConstants.RIGHT);
jtfMessage.setHorizontalAlignment(SwingConstants.CENTER);
p1.add(jtfMessage);

parent.add(p1);

JPanel jpRadioButtons = new JPanel();
jpRadioButtons.setLayout(new GridLayout(1,3));
jpRadioButtons.add(jrbLeft= new JRadioButton("Left"));
jpRadioButtons.add(jrbCenter = new JRadioButton("Center"));
jpRadioButtons.add(jrbRight = new JRadioButton("Right"));
jpRadioButtons.setBorder(new TitledBorder("Horizontal Border"));

final JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,2,1,1));

p2.add(jpRadioButtons);

JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1,1,1,1));
p3.add(new JLabel("Column Size"));

jtfColumn= new JTextField("60",SwingConstants.RIGHT);
jtfColumn.setHorizontalAlignment(SwingConstants.CENTER);

p3.add(jtfColumn);
Border lineBorder = new LineBorder(Color.LIGHT_GRAY,1);
p3.setBorder(lineBorder);
p2.add(p3);
parent.add(p2);
add(parent);



jrbLeft.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtfMessage.setHorizontalAlignment(SwingConstants.LEFT);
jrbCenter.setSelected(false);
jrbRight.setSelected(false);
jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
// p1.revalidate();
// p1.repaint();
}
}
);

jrbCenter.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtfMessage.setHorizontalAlignment(SwingConstants.CENTER);
jrbLeft.setSelected(false);
jrbRight.setSelected(false);
jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
}
}
);

jrbRight.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jtfMessage.setHorizontalAlignment(SwingConstants.RIGHT);
jrbCenter.setSelected(false);
jrbLeft.setSelected(false);
jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText()));
}

}
);

}

}

最佳答案

主要问题是 GridLayout

GridLayout 在设计上根据父容器的可用空间为 column/width 中的组件提供相等的宽度和高度。

相反,尝试使用类似 FlowLayoutGridBagLayout 的东西,它们与组件的首选大小一起工作

关于java - 无法在运行时调整文本字段的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20483530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com