你好 StackOverflow 的好人。我遇到了一个我似乎无法解决的问题。在我开始之前,我应该警告一下,虽然我不是初学者,但我也不是非常先进,所以如果你能恰当地说出你的答案,那就太好了。
我无法控制 JTextFields 的尺寸,而且我已经使用了我所知道的所有方法。这是代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class ShapeCalculatorView extends JFrame {
public static void main(String[] args) {
}
public ShapeCalculatorView() {
init();
}
public void init() {
setVisible(true);
setTitle("Shape Calculator");
setSize(500, 500);
//
GridLayout bodyLayout = new GridLayout(1, 2);
GridLayout answerLayout = new GridLayout(2, 2);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createLineBorder(Color.red));
//
JPanel bodyPanel = new JPanel();
bodyPanel.setLayout(bodyLayout);
bodyPanel.setBorder(BorderFactory.createLineBorder(Color.blue));
mainPanel.add(bodyPanel, BorderLayout.PAGE_START);
JPanel resultPanel = new JPanel();
resultPanel.setLayout(bodyLayout);
mainPanel.add(resultPanel, BorderLayout.PAGE_END);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
bodyPanel.add(buttonPanel);
JRadioButton buttonOne = new JRadioButton("Button 1");
JRadioButton buttonTwo = new JRadioButton("Button 2");
JRadioButton buttonThree = new JRadioButton("Button 3");
JRadioButton buttonFour = new JRadioButton("Button 4");
JRadioButton buttonFive = new JRadioButton("Button 5");
buttonPanel.add(buttonOne);
buttonPanel.add(buttonTwo);
buttonPanel.add(buttonThree);
buttonPanel.add(buttonFour);
buttonPanel.add(buttonFive);
String textLength = "Length: ";
String textWidth = "Width: ";
JPanel inputPanel = new JPanel();
inputPanel.setLayout(answerLayout);
bodyPanel.add(inputPanel);
JTextField fieldOne = new JTextField();
JTextField fieldTwo = new JTextField();
fieldOne.setPreferredSize(new Dimension(100, 2));
inputPanel.add(new JLabel(textLength));
inputPanel.add(fieldOne);
inputPanel.add(new JLabel(textWidth));
inputPanel.add(fieldTwo);
JPanel calcPanel = new JPanel();
resultPanel.add(calcPanel);
JButton calcButton = new JButton("CALCULATE");
calcPanel.add(calcButton);
JPanel answerPanel = new JPanel();
answerPanel.setLayout(answerLayout);
resultPanel.add(answerPanel);
answerPanel.add(new JLabel("Perimeter: "));
answerPanel.add(new JLabel("Area: "));
long perimeter = 5;
long area;
add(mainPanel);
pack();
}
}
public class ShapeCalculatorApp {
public static void main(String[] args) {
ShapeCalculatorView view = new ShapeCalculatorView();
}
}
如果您要运行它,您会看到一切都很好,除了文本字段非常高,我希望它们更小一些。我已经尝试过 setPreferredSize 并且没有用,我怀疑这可能是因为我使用的是 GridLayout 并且因此两个文本字段占用了所有空间,所以 setPreferredSize 不起作用。我还认为也许我可以将行数设置为 3,然后在第二行中放置 2 个空 JLabel,但这似乎有点原始的处理方式。有没有一种有效的方法来做到这一点?
编辑:如果重要,我可以使用 setPreferredSize 控制文本字段的宽度,而不是高度。