3
我再次更改了代碼。但是現在當我運行代碼時,文本框非常小。只有當我選擇左側,中間或右側對齊時,文本框大小纔會改變。 我在哪裏犯了這個錯誤。無法在運行時調整文本字段的大小
我寫了下面的程序。當我點擊左側,右側或中心按鈕時,程序還應該讀取列大小文本框中的值,然後自動調整消息文本框的大小。我似乎無法做到這一點。任何見解將不勝感激。包含workingwithtexttexts的工作組;
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()));
}
}
);
}
}
,我仍然有問題的代碼工作。有人可以分享一些新的見解。 – user2918968
有什麼問題?請記住,如果某些佈局管理器無法遵守首選大小,它將恢復使用字段最小大小......另外,不要忘記將「p1.revalidate」和「p1.repaint」添加到其他按鈕處理程序中。 。 – MadProgrammer