我无法解决一些问题:
我有一个JPanel
的程序使用BoxLayout
,X_AXIS
和JLabel (setAignmentX(Component.CENTER_ALIGNMENT))
内部对齐,JTextArea
和1 JButton(setAlignmentX(Component.CENTER_ALIGNMENT))
当我按下按钮并改变按钮时,文本的按钮也会改变对齐标签和按钮(不仅仅是尺寸)。如果它常常使眼睛非常紧张。 setHorizontalAlignment
和SwingConstants
不起作用。
位GUI设计:
frame = new JFrame("TestIOapp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBackground(Color.decode("#e2dad4"));
JLabel qLabel = new JLabel("Question");
JLabel aLabel = new JLabel("Answer");
qLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
aLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
button = new JButton("Next");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.setPreferredSize(new Dimension(100,40));
button.addActionListener(new NextButtonListener());
question = new JTextArea(10,20);
question.setWrapStyleWord(true);
question.setLineWrap(true);
answer = new JTextArea(10,20);
answer.setWrapStyleWord(true);
answer.setLineWrap(true);
JScrollPane qScroll = new JScrollPane(question);
qScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
qScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
JScrollPane aScroll = new JScrollPane(answer);
aScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
aScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(qLabel);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(qScroll);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(aLabel);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(aScroll);
panel.add(Box.createRigidArea(new Dimension(0,10)));
panel.add(button);
panel.add(Box.createRigidArea(new Dimension(0,30)));
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.setVisible(true);
frame.setSize(500,500);