我有以下程序。当我更改列大小,并单击任何中心,左或右对齐按钮时,如果列大小太高,则一切都会更改。我希望窗口的尺寸增大,并且面板的排列没有任何变化。 有人可以提出一些见解。
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 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.pack();
frame.setTitle("Exercise 17.11");//Setting the frame title.
frame.setSize(500, 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.CENTER, 30, 0));
p1.add(new JLabel("TextField", SwingConstants.CENTER));
jtfMessage = new JTextField("Type anything", SwingConstants.RIGHT);
jtfMessage.setHorizontalAlignment(SwingConstants.CENTER);
jtfMessage.setColumns(30);
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()));
}
}
);
}
}
答案 0 :(得分:0)
Java应用程序使用LayoutManagers来控制它,如果你想100%控制它,你将不得不使用像Netbeans的AbsoluteLayout这样的非标准组件,但是你必须在你的应用程序中包含它的.jar。
答案 1 :(得分:0)
当您更改字段的列大小时,您需要在字段窗口中调用pack
以使其将窗口大小调整为其包含的组件的首选大小
例如......
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();
SwingUtilities.getWindowAncestor(p1).pack();
SwingUtilities.getWindowAncestor(p1).setLocationRelativeTo(null);
}
}
);